IO- /*
- 练习:读取一个java文件,输出到控制台
- */
- import java.io.*;
- class FileReaderTest
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr = new FileReader("IO.java");
- FileWriter fw = new FileWriter("1.txt");
- char[] charBuf = new char[8];
- int num = 0;
- int count = 0;
- while((num = fr.read(charBuf)) != -1){
- // fw.write(charBuf);
- fw.write(new String(charBuf,0,num));//写内容到流
- System.out.print(new String(charBuf,0,num));
- count = count + num;
- }
- fr.close();//关闭流
- fw.flush();//关闭前 刷新流 将内容写入文件
- fw.close();
- // fw.close();
- System.out.println();
- System.out.println("读取的字符数count = "+count);
- System.out.println("Hello World!");
- }
- }
复制代码
|
|