字符流
public static void demo1() throws FileNotFoundException, IOException { FileReader fr = new FileReader("xxx.txt"); int x = fr.read(); System.out.println(x); char c = (char)x;//读的是一个字符,所以也得转为字符 System.out.println(c); fr.close(); }
private static void demo2() throws FileNotFoundException, IOException { FileReader fr = new FileReader("xxx.txt"); int c; while((c = fr.read()) != -1) { //通过项目默认的码表一次读取一个字符 System.out.print((char)c); } fr.close(); }
public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("yyy.txt"); fw.write("大家好,基础班快接近尾声了,大家要努力,要坚持ddd!!!"); fw.write(97);//输出字母"a" fw.close(); }
//其实底层也是通过码表的作用,把字符变成字节再输出
/* * 字符流拷贝纯字符文件 */ public static void demo1() throws FileNotFoundException, IOException { FileReader fr = new FileReader("xxx.txt"); FileWriter fw = new FileWriter("zzz.txt"); int c; while((c = fr.read()) != -1) { fw.write(c); } //Writer类中有一个2k的小缓冲区,如果不关流,就会将内容写到缓冲区里,关流会将缓冲区内容刷新,再关闭 fr.close(); fw.close(); }
###21.04_IO流(什么情况下使用字符流)
* 字符流也可以拷贝文本文件, 但不推荐使用. 因为读取时会把字节转为字符, 写出时还要把字符转回字节.(因为中间有个转换动作,但是字节流就没有这个转换)
* 程序需要读取一段文本, 或者需要写出一段文本的时候可以使用字符流
* 读取的时候是按照字符的大小读取的,不会出现半个中文
* 写出的时候可以直接将字符串写出,不用转换为字节数组
###21.05_IO流(字符流是否可以拷贝非纯文本的文件)
* 不可以拷贝非纯文本的文件
* 因为在读的时候会将字节转换为字符,在转换过程中,可能找不到对应的字符,就会用?代替,写出的时候会将字符转换成字节写出去
* 如果是?,直接写出,这样写出之后的文件就乱了,看不了了
private static void demo4() throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader("xxx.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt")); int c; while((c = br.read()) != -1) { bw.write(c); } br.close(); bw.close(); }
/** * Reads a line of text. A line is considered to be terminated by any one * of a line feed ('\n'), a carriage return ('\r'), or a carriage return * followed immediately by a linefeed. * * @return A String containing the contents of the line, not including * any line-termination characters, or null if the end of the * stream has been reached * * @exception IOException If an I/O error occurs * * @see java.nio.file.Files#readAllLines */ public String readLine() throws IOException { return readLine(false); }
public static void demo1() throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader("zzz.txt")); String line; while((line = br.readLine()) != null) { System.out.println(line); } br.close(); }
/** * @param args * 带缓冲区的流中的特殊方法 * readLine() * newLine(); * * newLine()与\r\n(换行,以及回车)的区别 * newLine()是跨平台的方法:好波,也就是这个好咯 * \r\n只支持的是windows系统 * @throws IOException */ private static void demo2() throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader("zzz.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("aaa.txt")); String line; while((line = br.readLine()) != null) { bw.write(line); //bw.newLine(); //写出回车换行符 //bw.write("\r\n"); //和上面表达的意思都一样 } br.close(); bw.close(); }
public class LineNumberReader extends BufferedReader; //主要是设置获取行号 public static void main(String[] args) throws IOException { LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt")); String line; lnr.setLineNumber(100);//从101行开始读取,如果不设置的话就从第一行开始读 while((line = lnr.readLine()) != null) { System.out.println(lnr.getLineNumber() + ":" + line);//读取行号 } lnr.close(); }
afasfsafa
asvasbn