[关闭]
@nextleaf 2018-08-22T02:12:18.000000Z 字数 15655 阅读 778

2018-08-21 工作日志

工作日志 Java IO


Java IO流

Java IO类的继承结构

Java IO流图

四个抽象类

字符流:Reader、Writer
字节流:InputStream、OutputStream

File类常用方法

createNewFile()
mkdir()
boolean mkdirs()
exists()
delete()
getName()
getAbsoluteFile() 等

FileInputStream和 FileOutputStream

这两类都是用于处理字节流(文件)

OutputStreamWriter和IntputStreamReader

这两类都是用于处理字符流(转换流)

FileReader类和FileWriter类

这两类都是用于处理字符流(文件)

BufferedWriter(继承于Writer)的write方法:

将给定数组的字符存入此流的缓冲区中,根据需要将缓冲区(默认大小可能是8192字节)的内容刷新到底层流。但是,如果请求的长度≥此缓冲区大小,则此方法将刷新该缓冲区并将各个字符直接写入底层流。
bufferedWriter.write(chars);//将char数组写入缓冲区(如果读取的字符少于数组长度,则以'\u0000'填充。)

BufferedReader(继承于Reader)的read(char[] cbuf,int off,int len)方法:

如果第一次对底层流调用 read返回-1(指示文件末尾,即空文件),则此方法返回-1,否则此方法返回实际读取的字符数。

flush()

将缓冲区的内容写入文件并刷新该流的缓冲区

close()

关闭此流,但会先执行flush()。

处理流和节点流

通常,处理流套着节点流,节点流里套着文件

  1. ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));

字节流与字符流的区别

字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件。
字节流在操作文件时,即使不关闭资源(close方法),文件也能输出,但是如果字符流不使用close或flush方法的话,则不会输出任何内容。
字符流只能处理字符文件,字节流则可以处理字节文件和字符文件。

序列化

■所谓对象序列化就是将对象的状态转换成字节流,保存在文件虫,以后可以通过读取文件再生成相同状态的对象。
■序列化分为两大部分:序列化和反序列化。序列
化是这个过程的第一部分,将数据分解成字节流,
以便存储在文件中或在网络上传输。反序列化就是
打开字节流并重构对象.

JAVA对象序列化

Java序列化比较简单,只需实现Java.Io.Serializable接口的类对象就可以转换成字节流或从字节流恢复,不需要在类中增加任何代码。
java.Io包有两个序列化对象的类。
Objectoutputstream负责将对象写入字节流
Objectlnputstream从字节流重构对象。


文件复制,FileInputStream访问文件,字节流

  1. package com.nl.sx820.io.stream;
  2. import java.io.*;
  3. /**
  4. * Created with IntelliJ IDEA 2018.
  5. * Description: FileInputStream 文件拷贝
  6. *
  7. * @author: 黄昭鸿
  8. * @date: 2018-08-21
  9. * Time: 10:05
  10. */
  11. public class FileInputStreamDemo {
  12. public static void main(String[] args) {
  13. //源文件路径
  14. String source = "E:" + File.separator + "Downloads" + File.separator + "a1.png";
  15. //目标路径
  16. String target = "E:" + File.separator + "Downloads" + File.separator + "a2.png";
  17. copyFile(source, target);
  18. }
  19. public static void copyFile(String src, String target) {
  20. /**
  21. * Description: 创建文件副本
  22. * @author 黄昭鸿
  23. * @date 2018/8/21 12:06
  24. * @param [src, target]
  25. * @return void
  26. */
  27. //源文件路径
  28. File file = new File(src);
  29. byte[] bufs = readOldFile(file);
  30. //若文件存在
  31. if (bufs != null) {
  32. System.out.println(aBooleanNewFile(new File(target), file.getName(), bufs));
  33. }
  34. }
  35. public static byte[] readOldFile(File file) {
  36. if (file.exists()) {
  37. try {
  38. byte[] buf;
  39. InputStream in = new FileInputStream(file);
  40. if (file.isFile()) {
  41. buf = new byte[(int) file.length()];
  42. in.read(buf);
  43. /* byte[] buff=new byte[1024];
  44. int temp;
  45. while ((temp=in.read(buff))!=-1){
  46. outputStream.write(buff);
  47. }*/
  48. //System.out.println(new String(buf));
  49. in.close();
  50. return buf;
  51. } else {
  52. in.close();
  53. System.out.println("不是文件");
  54. return null;
  55. }
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. } else {
  60. System.out.println("路径不存在");
  61. }
  62. return null;
  63. }
  64. public static boolean aBooleanNewFile(File newfile, String name, byte[] bytes) {
  65. if (newfile.exists()) {
  66. if (newfile.isFile()) {
  67. System.out.println("覆写");
  68. return writefile(newfile, bytes);
  69. } else if (newfile.isDirectory()) {
  70. newfile = new File(newfile, File.separator + name);
  71. System.out.println("新建文件" + newfile.getAbsolutePath());
  72. return writefile(newfile, bytes);
  73. } else {
  74. System.out.println("!");
  75. return false;
  76. }
  77. } else {
  78. //file.createNewFile();
  79. System.out.println("新建副本" + newfile.getAbsolutePath());
  80. return writefile(newfile, bytes);
  81. }
  82. }
  83. public static boolean writefile(File newfile, byte[] bytes) {
  84. try {
  85. OutputStream outputStream = new FileOutputStream(newfile);
  86. outputStream.write(bytes);
  87. outputStream.flush();
  88. System.out.println("文件已写入");
  89. outputStream.close();
  90. } catch (FileNotFoundException e) {
  91. e.printStackTrace();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. return true;
  96. }
  97. }

字节流与字符流

  1. package com.nl.sx820.io.stream;
  2. import java.io.*;
  3. import java.nio.charset.StandardCharsets;
  4. /**
  5. * Created with IntelliJ IDEA 2018.
  6. * Description: 文件内容读取和写入、字节流和字符流
  7. * 字节流与字符流的区别:
  8. * 字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件。
  9. * 字节流在操作文件时,即使不关闭资源(close方法),文件也能输出,但是如果字符流不使用close或flush方法的话,则不会输出任何内容。
  10. * 字符流只能处理字符文件,字节流则可以处理字节文件和字符文件
  11. * @author: 黄昭鸿
  12. * @date: 2018-08-20
  13. * Time: 17:52
  14. */
  15. public class StreamDemo {
  16. public static void main(String[] args) {
  17. //转换流,字符流,InputStreamReader、OutputStreamWriter
  18. System.out.println("-----InputStreamReader、OutputStreamWriter-转换流,字符流---------------");
  19. sssssssssss();
  20. System.out.println("-------字符流——访问文件---FileReader、FileWriter-------");
  21. File file = new File("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.txt");
  22. File newFile = new File("E:" + File.separator + "Downloads" + File.separator + "NewNextleafwin.txt");
  23. rf(file);
  24. //如果文件不存在,FileWriter的write()方法会自动新建文件
  25. wf(newFile, "Java.io 包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。一个流可以理解为一个数据的序列。");
  26. System.out.println();
  27. //字节流:DataInputStream、DataOutputStream
  28. System.out.println("------特殊字节流-----DataInputStream、DataOutputStream----------");
  29. aVoid();
  30. }
  31. /**
  32. * Description:转换流——字符输入输出流(InputStreamReader、OutputStreamWriter)
  33. *
  34. * @return void
  35. * @author 黄昭鸿
  36. * @date 2018/8/21 14:20
  37. */
  38. private static void sssssssssss() {
  39. StringBuffer stringBuffer = new StringBuffer();
  40. try {
  41. //创建一个输入流对象来读取文件:(还可以使用一个文件对象来创建一个输入流对象来读取文件)
  42. InputStream fileInputStream = new FileInputStream("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.txt");
  43. //读取到StringBuffer中
  44. InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  45. while (reader.ready()) {
  46. // 转成char加到StringBuffer对象中
  47. stringBuffer.append((char) reader.read());
  48. }
  49. System.out.println("啦啦啦" + stringBuffer.toString());
  50. //也可以
  51. /*File file=new File("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.txt");
  52. byte[] buf=new byte[(int)file.length()];
  53. InputStream in=new FileInputStream(file);
  54. in.read(buf);
  55. System.out.println(new String(buf));*/
  56. //关闭读取流
  57. reader.close();
  58. //关闭输入流,释放系统资源
  59. fileInputStream.close();
  60. /*
  61. 这里的代码也可以实现文件读取,但会影响上面的读取流代码,只能选其一使用
  62. //从文件输入流一次读取一个字节
  63. System.out.println("从文件输入流一次读取一个字节");
  64. byte byteData;
  65. while ((byteData = (byte) fileInputStream.read()) != -1) {
  66. System.out.print((char) byteData);
  67. }
  68. System.out.println();*/
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. //使用字符串类型的文件名来创建一个输出流对象(也可以使用一个文件对象来创建一个输出流来写文件)
  73. try {
  74. //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("your output file path"));
  75. //如果文件不存在会自动新建
  76. OutputStream fileOutputStream = new FileOutputStream("E:" + File.separator + "Downloads" + File.separator + "新nextleafwin.txt");
  77. //默认为操作系统默认编码,windows上是gbk
  78. OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
  79. //使用line.separator系统变量来换行,如下
  80. String lineSeparator = System.getProperty("line.separator");
  81. fileOutputStream.write(lineSeparator.getBytes());
  82. //字符串转为字节数组,写入file
  83. fileOutputStream.write("字符串转为字节数组".getBytes());
  84. // 写入到缓冲区
  85. writer.append("中文输入");
  86. writer.append(stringBuffer);
  87. //System.out.println(stringBuffer);
  88. //换行
  89. writer.append("\r\n");
  90. // 刷新缓存冲,写入到文件,直接close也会写入
  91. writer.flush();
  92. // 关闭写入流,同时会把缓冲区内容写入文件
  93. writer.close();
  94. // 关闭输出流,释放系统资源
  95. fileOutputStream.close();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. //文件内容读取
  101. public static void rf(File file) {
  102. if (file.isFile()) {
  103. try {
  104. // 创建 FileReader 对象
  105. FileReader fileReader = new FileReader(file);
  106. System.out.println("读取的内容为:");
  107. /* byte byteData;
  108. while ((byteData = (byte) fileReader.read()) != -1) {
  109. System.out.print((char) byteData);
  110. }
  111. System.out.println();*/
  112. char[] a = new char[(int) file.length()];
  113. // 读取数组中的内容
  114. fileReader.read(a);
  115. for (char c : a) {
  116. System.out.print(c);
  117. }
  118. fileReader.close();
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. }
  122. } else {
  123. System.out.println("不是一个文件,读取??");
  124. }
  125. }
  126. //文件内容写入
  127. //如果文件不存在,FileWriter的write()方法会自动新建文件
  128. public static void wf(File file, String writeString) {
  129. try {
  130. if (file.exists()) {
  131. if (file.isDirectory()) {
  132. file = new File(file, File.separator + "aNewFile.txt");
  133. file.createNewFile();
  134. // creates a FileWriter Object
  135. FileWriter writer = new FileWriter(file);
  136. // 向文件写入内容
  137. System.out.println("开始写入到" + file.getAbsolutePath());
  138. writer.write(writeString);
  139. writer.flush();
  140. writer.close();
  141. } else if (file.isFile()) {
  142. //覆写
  143. System.out.println("覆写!");
  144. file.createNewFile();
  145. FileWriter writer = new FileWriter(file);
  146. System.out.println("开始写入到" + file.getAbsolutePath());
  147. writer.write(writeString);
  148. writer.flush();
  149. writer.close();
  150. }
  151. } else {
  152. //新建
  153. System.out.println("新建" + file.getAbsolutePath());
  154. file.createNewFile();
  155. FileWriter writer = new FileWriter(file);
  156. writer.write(writeString);
  157. writer.flush();
  158. writer.close();
  159. }
  160. } catch (IOException e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. public static void writing(File file, String writeString) {
  165. FileWriter writer;
  166. try {
  167. writer = new FileWriter(file);
  168. writer.write(writeString);
  169. writer.flush();
  170. writer.close();
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. }
  174. }
  175. //特殊流--字节流(DataInputStream、DataOutputStream)
  176. public static void aVoid() {
  177. File newfile = new File("E:" + File.separator + "Downloads" + File.separator + "特殊字节流.txt");
  178. DataInputStream dataInputStream = null;
  179. DataOutputStream dataOutputStream = null;
  180. try {
  181. dataInputStream = new DataInputStream(new FileInputStream(newfile));
  182. dataOutputStream = new DataOutputStream(new FileOutputStream(newfile));
  183. //写入Java类型
  184. dataOutputStream.writeBoolean(true);
  185. dataOutputStream.writeChar('和');
  186. dataOutputStream.writeInt(1);
  187. dataOutputStream.writeDouble(3.141592653);
  188. dataOutputStream.close();
  189. //读出文件内容
  190. System.out.println(dataInputStream.readBoolean());
  191. System.out.println( dataInputStream.readChar());
  192. System.out.println(dataInputStream.readInt());
  193. System.out.println(dataInputStream.readDouble());
  194. dataInputStream.close();
  195. } catch (IOException e) {
  196. e.printStackTrace();
  197. }finally {
  198. try {
  199. if (dataInputStream!=null){
  200. dataInputStream.close();
  201. }
  202. if (dataOutputStream!=null){
  203. dataOutputStream.close();
  204. }
  205. } catch (IOException e) {
  206. e.printStackTrace();
  207. }
  208. }
  209. }
  210. }

文件,File类及其常用方法

  1. package com.nl.sx820.io;
  2. import java.io.File;
  3. import java.io.IOException;
  4. /**
  5. * Created with IntelliJ IDEA 2018.
  6. * Description:文件,File类及其常用方法
  7. * 创建目录
  8. * 创建文件
  9. * 获取文件信息
  10. * @author: 黄昭鸿
  11. * @date: 2018-08-20
  12. * Time: 14:53
  13. */
  14. public class FileDemo {
  15. public static void main(String[] args) {
  16. //创建目录
  17. File parent = new File("E:" + File.separator + "Downloads" + File.separator + "java创建的文件夹");
  18. parent.mkdir();
  19. //创建文件
  20. try {
  21. new File(parent + File.separator + "通用分隔符(File.separator).txt").createNewFile();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. createNewFileTest();
  26. //复制文件
  27. //FileInputStreamDemo.copyFile("","");
  28. }
  29. public static void createNewFileTest() {
  30. //在工作中用到通用分隔符File.separator
  31. File parent2 = new File("E:" + File.separator + "Downloads" + File.separator + "java创建的文件夹2Test" + File.separator + "子文件夹");
  32. File child = new File(parent2, "子File.txt");
  33. try {
  34. if (!parent2.exists()) {
  35. //parent.mkdir()只创建一个文件夹,parent.mkdirs()可创建多个文件夹
  36. parent2.mkdirs();
  37. child.createNewFile();
  38. System.out.println("文件目录及文件创建成功");
  39. } else {
  40. child.createNewFile();
  41. System.out.println("文件创建成功");
  42. }
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. showInfo(parent2);
  47. showListFiles(parent2);
  48. showInfo(child);
  49. showListFiles(child);
  50. //1秒后删除
  51. //timing(child);
  52. //重命名
  53. reName(child, "E:\\Downloads\\java创建的文件夹2Test\\子文件夹\\新的名字.txt");
  54. }
  55. public static void timing(File child) {
  56. for (int i = 0; i < 10; i++) {
  57. try {
  58. //线程休眠
  59. Thread.sleep(1600);
  60. } catch (InterruptedException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. child.delete();
  65. }
  66. public static void showInfo(File f) {
  67. System.out.println("\n属性:");
  68. System.out.println("文件绝对路径:" + f.getAbsolutePath());
  69. System.out.println("文件名:" + f.getName());
  70. System.out.println("父目录的路径:" + f.getParent());
  71. System.out.println("是否时文件夹:" + f.isDirectory());
  72. System.out.println("是否时文件:" + f.isFile());
  73. System.out.println("文件长度(字节):" + f.length());
  74. System.out.println("是否隐藏:" + f.isHidden());
  75. System.out.println();
  76. }
  77. public static void showListFiles(File f) {
  78. if (f.exists()) {
  79. if (f.isDirectory()) {
  80. //System.out.println("包含的文件名列表:");
  81. //Console.log(f.list());
  82. System.out.println("[" + f.getAbsolutePath() + "]文件列表:");
  83. char str = ' ';
  84. System.out.println("文件名\t\t\t大小\t\t\t隐藏");
  85. for (File file : f.listFiles()) {
  86. if (file.isHidden()) {
  87. str = '✔';
  88. } else {
  89. str = ' ';
  90. }
  91. System.out.println(file.getName() + "\t\t" + file.length() + "字节\t\t" + str);
  92. }
  93. } else {
  94. System.out.println("不是一个目录...");
  95. }
  96. } else {
  97. System.out.println("路径不存在");
  98. }
  99. }
  100. /**
  101. * Description:
  102. * 重命名
  103. *
  104. * @param file,name
  105. * @return boolean
  106. * @author 黄昭鸿
  107. * @date 2018/8/20 18:30
  108. */
  109. public static boolean reName(File file, String name) {
  110. if (file.exists()) {
  111. if (file.isFile()) {
  112. System.out.println("新名:" + name);
  113. return file.renameTo(new File(name));
  114. } else {
  115. System.out.println("不是一个文件");
  116. }
  117. } else {
  118. System.out.println("路径不存在");
  119. return false;
  120. }
  121. return false;
  122. }
  123. }

RandomAccessFile类支持随机访问

  1. package com.nl.sx820.io;
  2. import org.junit.Test;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.RandomAccessFile;
  7. /**
  8. * Created with IntelliJ IDEA 2018.
  9. * Description:RandomAccessFile类支持随机访问,既可当作输入流也可当作输出流
  10. * 支持从文件任意位置读取和写入
  11. *
  12. * @author: 黄昭鸿
  13. * @date: 2018-08-21
  14. * Time: 15:16
  15. */
  16. public class RandomAccessFileDemo {
  17. /**
  18. * Description:RandomAccessFile类
  19. * 从文件任意位置读取和写入
  20. * @author 黄昭鸿
  21. * @date 2018/8/21 16:27
  22. * @param
  23. * @return void
  24. */
  25. @Test
  26. public void test01() {
  27. File file = new File("E:" + File.separator + "Downloads" + File.separator + "RandomAccessFile随机访问.mydata");
  28. File newfile = new File("E:" + File.separator + "Downloads" + File.separator + "RandomAccessFile2.mydata");
  29. RandomAccessFile randomAccessFile = null;
  30. RandomAccessFile newRandomAccessFile = null;
  31. try {
  32. //one of {@code "r"}, {@code "rw"}, {@code "rws"}, or{@code "rwd"}
  33. //r:只读,rw:读写
  34. randomAccessFile = new RandomAccessFile(file, "r");
  35. newRandomAccessFile = new RandomAccessFile(newfile, "rw");
  36. //byte[] bytes=new byte[(int) file.length()];
  37. byte[] bytes = new byte[20];
  38. int len;
  39. while ((len = randomAccessFile.read(bytes)) != -1) {
  40. //字节数据,写入的开始位置,长度(字节数)
  41. newRandomAccessFile.write(bytes, 0, len);
  42. }
  43. } catch (FileNotFoundException e) {
  44. e.printStackTrace();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. } finally {
  48. try {
  49. if (randomAccessFile != null) {
  50. randomAccessFile.close();
  51. }
  52. if (newRandomAccessFile != null) {
  53. newRandomAccessFile.close();
  54. }
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. /**
  61. * Description: 从文件任意位置读取和写入
  62. *
  63. * @author 黄昭鸿
  64. * @date 2018/8/21 16:26
  65. * @param
  66. * @return void
  67. */
  68. @Test
  69. public void test02() {
  70. File file = new File("E:" + File.separator + "Downloads" + File.separator + "RandomAccessFile随机访问.mydata");
  71. RandomAccessFile randomAccessFile = null;
  72. try {
  73. //one of {@code "r"}, {@code "rw"}, {@code "rws"}, or{@code "rwd"}
  74. randomAccessFile = new RandomAccessFile(file, "rw");
  75. //将文件记录指针定位到pos位置,0123456...
  76. randomAccessFile.seek(4);
  77. System.out.println(randomAccessFile.getFilePointer());
  78. /*从该文件读取下一行文本。此方法从文件中读取字节,从当前文件指针开始,直到到达行终止符或文件结束。每个字节通过将字符的低位八位的字节值设置为字符,并将字符的高八位设置为零来转换成字符。因此,此方法不支持完整的Unicode字符集。*/
  79. String str=randomAccessFile.readLine();
  80. System.out.println(randomAccessFile.getFilePointer());
  81. randomAccessFile.seek(4);
  82. System.out.println(randomAccessFile.getFilePointer());
  83. randomAccessFile.write("abcd".getBytes());
  84. System.out.println(randomAccessFile.getFilePointer());
  85. randomAccessFile.write(str.getBytes());
  86. System.out.println(randomAccessFile.getFilePointer());
  87. } catch (FileNotFoundException e) {
  88. e.printStackTrace();
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. } finally {
  92. try {
  93. if (randomAccessFile != null) {
  94. randomAccessFile.close();
  95. }
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. }
  101. }

FileReader和FileWriter(处理字符流)

  1. package com.nl.sx820.io.stream;
  2. import org.junit.Test;
  3. import java.io.*;
  4. /**
  5. * Created with IntelliJ IDEA 2018.
  6. * Description:字符流 以字符为单位,只可处理字符文件
  7. * Reader类和Writer类是所有字符输入流和输出流超类(抽象类),需要定义子类来实例化。
  8. *
  9. * @author: 黄昭鸿
  10. * @date: 2018-08-21
  11. * Time: 14:15
  12. */
  13. public class FileReaderAndFileWriter {
  14. @Test
  15. public void testFileReader() {
  16. //只可处理字符文件
  17. File file = new File("E:" + File.separator + "Downloads" + File.separator + "testFileReader.txt");
  18. FileReader fileReader = null;
  19. try {
  20. fileReader = new FileReader(file);
  21. char[] chars = new char[20];
  22. int len;
  23. while ((len = fileReader.read(chars)) != -1) {
  24. System.out.println(new String(chars, 0, len));
  25. }
  26. } catch (FileNotFoundException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } finally {
  31. if (fileReader != null) {
  32. try {
  33. fileReader.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. }
  40. @Test
  41. public void testBufferedReader() {
  42. File sourcefile = new File("E:" + File.separator + "Downloads" + File.separator + "testFileReader.txt");
  43. File targetfile = new File("E:" + File.separator + "Downloads" + File.separator + "newFileReader.txt");
  44. BufferedReader bufferedReader = null;
  45. BufferedWriter bufferedWriter = null;
  46. try {
  47. bufferedReader = new BufferedReader(new FileReader(sourcefile));
  48. bufferedWriter = new BufferedWriter(new FileWriter(targetfile));
  49. char[] chars = new char[1024];
  50. int len;
  51. //read(char[] cbuf,int off,int len)如果第一次对底层流调用 read 返回 -1(指示文件末尾,即空文件),则此方法返回 -1,否则此方法返回实际读取的字符数。
  52. while ((len=bufferedReader.read(chars)) != -1) {
  53. //write()将给定数组的字符存入此流的缓冲区中,根据需要将缓冲区(默认大小可能是8192字节)的内容刷新到底层流。
  54. // 但是,如果请求的长度≥此缓冲区大小,则此方法将刷新该缓冲区并将各个字符直接写入底层流,此处缓冲区指定为1024字节。
  55. bufferedWriter.write(chars,0,len);
  56. //bufferedWriter.write(chars);将char数组写入缓冲区(如果读取的字符少于数组长度,则以'\u0000'填充。)
  57. //flush(),将缓冲区的内容写入文件并刷新该流的缓冲区
  58. //bufferedWriter.flush();
  59. //close()关闭此流,但会先执行flush()。
  60. }
  61. } catch (FileNotFoundException e) {
  62. e.printStackTrace();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. } finally {
  66. try {
  67. if (bufferedReader != null) {
  68. bufferedReader.close();
  69. }
  70. if (bufferedWriter != null) {
  71. bufferedWriter.close();
  72. }
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注