字节流:FileInputStream和FileOutputStream的使用
2021-06-13 09:05
1226 查看
/**
* 测试FileInputStream和FileOutputStream的使用
*
* 结论:
* 1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
* 2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理
* @author CH
* @create 2021 下午 2:13
*/
//使用字节流FileInputStream处理文本文件,可能出现乱码。 @Test public void testFileInputStream() { FileInputStream fis = null; try { //1. 造文件 File file = new File("hello.txt"); //2.造流 fis = new FileInputStream(file); //3.读数据 byte[] buffer = new byte[5]; int len;//记录每次读取的字节的个数 while((len = fis.read(buffer)) != -1){ String str = new String(buffer,0,len); System.out.print(str); } } catch (IOException e) { e.printStackTrace(); } finally { if(fis != null){ //4.关闭资源 try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
分析:输出流buffer五个字节,国字正好夹在中间,所以没有乱码
/* 实现对图片的复制操作 */ @Test public void testFileInputOutputStream() { FileInputStream fis = null; FileOutputStream fos = null; try { // File srcFile = new File("爱情与友情.jpg"); File destFile = new File("爱情与友情2.jpg"); // fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //复制的过程 byte[] buffer = new byte[5]; int len; while((len = fis.read(buffer)) != -1){ fos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { if(fos != null){ // try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
//指定路径下文件的复制 public void copyFile(String srcPath,String destPath){ FileInputStream fis = null; FileOutputStream fos = null; try { // File srcFile = new File(srcPath); File destFile = new File(destPath); // fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //复制的过程 byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){ fos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { if(fos != null){ // try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
@Test public void testCopyFile(){ long start = System.currentTimeMillis(); String srcPath = "C:\\Users\\Administrator\\Desktop\\01.jvm结构_1.avi"; String destPath = "C:\\Users\\Administrator\\Desktop\\0111.jvm结构_1.avi"; // String srcPath = "hello.txt"; // String destPath = "hello3.txt"; copyFile(srcPath,destPath); long end = System.currentTimeMillis(); System.out.println("复制操作花费的时间为:" + (end - start));//618 }
相关文章推荐
- (使用字节流实现:FileInputStream和ByteArrayOutputStream)
- 使用字节流输入对象 FileInputStream 进行本地文件的读取
- Java字节流输入流FileInputStream的简单使用
- Java的 字节流 InputStream 类及其子类 FileInputStream 的使用
- Java基础之IO流,以字节流的方式操作读写文件FileOutputStream和FileInputStream的使用
- 黑马程序员_Java基础_IO流_字节流,字节流操作文件,缓冲区字节流,流的使用规律,异常记录原理
- File,FileInputStream,FileReader,InputStreamReader,BufferedReader 的使用和区别
- 使用字节流、客户端读取图片发送至服务端保存
- 字符流和字节流效率问题及原理,以及开发中使用
- java4Android(14)I/O流 、分类、目的、字节流重要类及方法FileInputStream
- 《Java基础》___使用字节流复制文件
- 字节流-FileInputStream_FileOutputStream_BufferedInputStream_BufferedOutputStream
- 【Java】使用字节流复制文件
- 由FileInputStream的read()、read(byte[])两个函数的使用,重新认识ascii编码、Unicode编码有什么用?
- 字节流在基础部分使用中的常遇的小问题,以及字节流和字符流的对比.
- java_字节流、字符流的使用方法
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- File,FileInputStream,FileReader,InputStreamReader,BufferedReader的使用和区别
- Java File类,IO流,字节输入流InputStream,FileInputStream学习使用
- 跳过N字节阅读文件,使用FileInputStream