`

字符流

    博客分类:
  • java
阅读更多
package com.bjsxt.reader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import static com.itcase.Print.*;

public class TestFileReader {

	public static void main(String[] args) {
		
		FileReader fr = null;
		int c = 0;
		
		try {		
			fr = new FileReader("C:\\test.txt");
			while((c = fr.read())!=-1) {
				System.out.print((char)c);
			}
			fr.close();
		} catch (FileNotFoundException e) {
			print("找不到指定的文件");
			System.exit(-1);
		} catch (IOException ex) {
			print("读取发生错误");
		}
	}
}


package com.bjsxt.writer;

import java.io.FileWriter;
import java.io.IOException;
import static com.itcase.Print.*;

public class TestFileWriter {

	public static void main(String[] args) {		
		FileWriter fw = null;
		try {
			fw = new FileWriter("c:\\unicode.dat");
			for(int c = 0; c <= 50000; c++) {
				fw.write(c);  //此c为表示字符的ASCII码
			}
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
			print("文件写入错误!!!");
			System.exit(-1);
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics