-
方法名 说明 int read() 一次读一个字符数据 int read(char[] cbuf) 一次读一个字符数组数据 -
package demo14; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class _IO_Stream_Demo_03 { public static void main(String[] args) { InputStreamReader isr = null; try { isr = new InputStreamReader(new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo14\book_01.txt")); if (isr != null) { int ch; // int read(char[] cbuf) 一次读一个字符数组数据 char[] arr = new char[1024]; while ((ch = isr.read(arr)) != -1) { System.out.print(new String(arr,0,ch)); } } } catch (IOException e) { e.printStackTrace(); } finally { try { isr.close(); } catch (IOException e) { e.printStackTrace(); } } } } ============================================== 声表面波(SAW,Surface Acoustic Wave)是沿物体表面传播的一种弹性波。 声表面波是英国物理学家瑞利(Rayleigh)在19世纪80 年代研究地震波的过程中偶尔发现的一种能量集中于地表面传播的声波。 1965年,美国的怀特(R.M.White)和沃尔特默(F.W.Voltmer)发表题为“一种新型声表面波声——电转化器”的论文, 取得了声表面波技术的关键性突破,能在压电材料表面激励声表面波的金属叉指换能器 IDT的发明,大大加速了声表面波技术的发展, 使这门年轻的学科逐步发展成为一门新兴的、声学和电子学相结合的边缘学科。 Process finished with exit code 0
- 分析:
- 转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,所以,为了简化书写,转换流提供了对应的子类
- FileReader:用于读取字符文件的便捷类:FileReader(String fileNme)
- FileWriter:用于写入字符文件的便捷类:FileWriter(String fileNme)
- BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区的大小,或者是可以接受默认大小。默认值足够大,可用于大多数用途
- BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的搞笑读取,可以指定缓冲区的大小,或者是可以使用默认大小。默认值足够大,可用于大多数用途
-
思路:
- 根据数据源创建字符缓冲流输入对象
- 根据目的地创建字符缓冲输出流对象
- 读写数据,复制文件
- 释放资源
-
package demo14; import java.io.*; public class _IO_Stream_Demo_04 { public static void main(String[] args) { BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\_IO_Buffered_02.java")); bw = new BufferedWriter(new FileWriter("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo14\_IO_Buffered_02.java")); if (br != null) { char[] ch = new char[1024]; int len; while ((len = br.read(ch)) != -1) { bw.write(ch,0,len); } } } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } ========================================== Process finished with exit code 0 ****************************************** package demo13; import java.io.*; public class _IO_Buffered_02 { public static void main(String[] args) { // 记录开始时间 long startTime = System.currentTimeMillis(); // run_01(); // 程序运行耗时:59099毫秒 // run_02(); // 程序运行耗时:101毫秒 // run_03(); // 程序运行耗时:258毫秒 run_04(); // 程序运行耗时:37毫秒 // 记录程序结束时间 long endTime = System.currentTimeMillis(); System.out.println("程序运行耗时:"+(endTime-startTime)+"毫秒"); } // 单独使用FileOut/InStream,一次读取一个字节 public static void run_01() { FileOutputStream fos = null; FileInputStream fis = null; try { fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); if (fis != null) { int read; while ((read = fis.read()) != -1) { fos.write(read); } } } catch (IOException e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } // 单独使用FileOut/InStream,一次读取byte[1024] public static void run_02() { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); if (fis != null) { int len; byte[] bytes = new byte[1024]; while ((len = fis.read(bytes)) != -1) { fos.write(bytes,0,len); } } } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } // 使用字节缓冲流复制视频,但一次只读取一个字节 public static void run_03() { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { FileInputStream fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); FileOutputStream fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); if (bis != null) { int read; while ((read = bis.read()) != -1) { bos.write(read); } } } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } // 使用字节缓冲流复制视频,一次读取byte[1024] public static void run_04() { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { FileInputStream fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); FileOutputStream fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); if (bis != null) { int len; byte[] bytes = new byte[1024]; while ((len = bis.read(bytes)) != -1) { bos.write(bytes,0,len); } } } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } ***************************************** package demo13; import java.io.*; public class _IO_Buffered_02 { public static void main(String[] args) { // 记录开始时间 long startTime = System.currentTimeMillis(); // run_01(); // 程序运行耗时:59099毫秒 // run_02(); // 程序运行耗时:101毫秒 // run_03(); // 程序运行耗时:258毫秒 run_04(); // 程序运行耗时:37毫秒 // 记录程序结束时间 long endTime = System.currentTimeMillis(); System.out.println("程序运行耗时:"+(endTime-startTime)+"毫秒"); } // 单独使用FileOut/InStream,一次读取一个字节 public static void run_01() { FileOutputStream fos = null; FileInputStream fis = null; try { fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); if (fis != null) { int read; while ((read = fis.read()) != -1) { fos.write(read); } } } catch (IOException e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } // 单独使用FileOut/InStream,一次读取byte[1024] public static void run_02() { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); if (fis != null) { int len; byte[] bytes = new byte[1024]; while ((len = fis.read(bytes)) != -1) { fos.write(bytes,0,len); } } } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } // 使用字节缓冲流复制视频,但一次只读取一个字节 public static void run_03() { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { FileInputStream fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); FileOutputStream fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); if (bis != null) { int read; while ((read = bis.read()) != -1) { bos.write(read); } } } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } // 使用字节缓冲流复制视频,一次读取byte[1024] public static void run_04() { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { FileInputStream fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); FileOutputStream fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); if (bis != null) { int len; byte[] bytes = new byte[1024]; while ((len = bis.read(bytes)) != -1) { bos.write(bytes,0,len); } } } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } bos = null; try { FileInputStream fis = new FileInputStream("C:\Users\Alvord\Desktop\markdown学习\截图\一斗.mp4"); FileOutputStream fos = new FileOutputStream("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\一斗.mp4"); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); if (bis != null) { int len; byte[] bytes = new byte[1024]; while ((len = bis.read(bytes)) != -1) { bos.write(bytes,0,len); } } } catch (IOException e) { e.printStackTrace(); } finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); }
-
BufferedWriter:
- void newLine() :写一行行分隔符,行分隔符字符由系统属性定义
-
BufferedReader:
- public String readLine() :读一行文字。结果包含的内容的字符串,不包括任何终止字符,如果流的结尾已经到达,则为null
-
package demo14; import java.io.*; public class _IO_Stream_Demo_05 { public static void main(String[] args) { BufferedWriter bw = null; BufferedReader br = null; try { bw = new BufferedWriter(new FileWriter("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo14\_Copy_IO_Buffered_02.java")); br = new BufferedReader(new FileReader("C:\Users\Alvord\Desktop\markdown学习\code\studyProject\src\demo13\_IO_Buffered_02.java")); if (br != null) { String len; while ((len = br.readLine()) != null) { bw.write(len); bw.newLine(); bw.flush(); } } } catch (IOException e) { e.printStackTrace(); } finally { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } ================================= Process finished with exit code 0



