有没有大佬告诉我这个不要了的代码插入区(就现在这句话的区域)怎么删掉。。。。。。。
//一个字节一个字节的复制 public static void fun() throws IOException { FileInputStream fis = new FileInputStream("F:/abc.txt"); FileOutputStream fos = new FileOutputStream("F:/字节流复制(一个字节一个字节).txt"); int by = 0; while ((by=fis.read()) != -1) { fos.write(by); } fis.close(); fos.close(); } //1024字节数组复制(加入数组缓冲区) public static void fun1() throws IOException { FileInputStream fis = new FileInputStream("F:/abc.txt"); FileOutputStream fos = new FileOutputStream("F:/字节流复制(1024字节数组).txt"); int len = 0; byte[] bytes =new byte[1024]; while ((len=fis.read(bytes)) != -1) { fos.write(bytes,0,len); } fis.close(); fos.close(); } // 一个字节一个字节复制并用了缓冲流 public static void fun2() throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:/abc.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:/字节缓冲流复制(一个字节一个字节).txt")); int by = 0; while ((by=bis.read()) != -1) { bos.write(by); } bos.close(); bis.close(); } // 1024字节数组复制并用了缓冲流 (加入数组缓冲区) public static void fun3() throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:/abc.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:/字节缓冲流复制(1024字节数组).txt")); int len = 0; byte[] bytes =new byte[1024]; while ((len=bis.read(bytes)) != -1) { bos.write(bytes,0,len); } bos.close(); bis.close(); } //字符缓冲流复制文(一行一行可保留格式) public static void fun4() throws IOException { FileInputStream fileInputStream = new FileInputStream("F:/abc.txt"); //出现乱码问题的原因:文件的编码,系统的编码,java的默认编码有冲突。 //假如我们用FileReader这些类来读取系统文件,它调用的字符编码是java默认的UTF-8,但是一般WINDOW的TXT默认是ANSI,而自己本机的中文编码是GBK InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"GBK"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); FileOutputStream fileOutputStream = new FileOutputStream("F:/字符缓冲流复制文件.txt"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK"); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); String line = null; while (null != (line = bufferedReader.readLine())) { bufferedWriter.write(line); bufferedWriter.newLine(); } if (bufferedWriter != null || outputStreamWriter != null || fileOutputStream != null) { bufferedWriter.close(); outputStreamWriter.close(); fileOutputStream.close(); } if (bufferedReader != null || inputStreamReader != null || fileInputStream != null) { bufferedReader.close(); inputStreamReader.close(); fileInputStream.close(); } } //字节流(ByteArrayInputStream)当你资源不足够用时,选择BufferedOutputStream是最佳的选择, 当你选择快速完成一个作业时,可以选择ByteArrayOutputStream之类的输出流https://www.cnblogs.com/yixiu868/p/8144670.html public static void fun5() throws IOException { FileInputStream fileInputStream = new FileInputStream("F:/abc.txt"); byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((len = fileInputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); byte[] bytes = bos.toByteArray(); FileOutputStream fileOutputStream = new FileOutputStream("F:/字节流(ByteArrayInputStream).txt"); fileOutputStream.write(bytes); if (fileOutputStream != null) { fileOutputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } public static void main(String[] arg) throws IOException{ long start,end; start = System.currentTimeMillis(); fun(); end = System.currentTimeMillis(); System.out.println("一个字节一个字节的复制(字节流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis(); fun1(); end = System.currentTimeMillis(); System.out.println("1024字节数组复制(字节流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis(); fun2(); end = System.currentTimeMillis(); System.out.println("一个字节一个字节的复制(缓冲流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis(); fun3(); end = System.currentTimeMillis(); System.out.println("1024字节数组复制(缓冲流)花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis(); fun4(); end = System.currentTimeMillis(); System.out.println("字符缓冲流复制文件花费时间:" + (end - start) + "ms"); start = System.currentTimeMillis(); fun5(); end = System.currentTimeMillis(); System.out.println("字节流(ByteArrayInputStream)花费时间:" + (end - start) + "ms"); }
执行结果:
下图为IO中的字节流和字符流,每一种又分为相应的输入流和输出流。需了解的基础概念:字符流(Reader,Writer),字节流,缓冲流(Buffered);使用时按照各自需求去搜寻对应相关资料即可。