A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

IO
  1. /*
  2. 练习:读取一个java文件,输出到控制台
  3. */
  4. import java.io.*;
  5. class FileReaderTest
  6. {
  7.         public static void main(String[] args) throws IOException
  8.         {
  9.                 FileReader fr = new FileReader("IO.java");
  10.                 FileWriter fw = new FileWriter("1.txt");
  11.                 char[] charBuf = new char[8];
  12.                 int num = 0;
  13.                 int count = 0;
  14.                 while((num = fr.read(charBuf)) != -1){
  15. //                        fw.write(charBuf);
  16.                         fw.write(new String(charBuf,0,num));//写内容到流
  17.                         System.out.print(new String(charBuf,0,num));
  18.                         count = count + num;
  19.                 }
  20.                 fr.close();//关闭流
  21.                 fw.flush();//关闭前 刷新流 将内容写入文件
  22.                 fw.close();
  23. //                fw.close();
  24.                 System.out.println();
  25.                 System.out.println("读取的字符数count = "+count);
  26.                 System.out.println("Hello World!");
  27.         }
  28. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马