- 概述
IO流用来处理设备之间的数据传输
流按照操作数据分为两种:字节流与字符流
Java对数据的操作是通过流的当时
Java用于操作的对象都在IO包中 - IO 流常用基类
字节流的抽象基类: InputStream, OutputStream.
字符流的抽象类: Reader,Writer
这四个类派生出来的子类名称都是以其父类名作为子类名的后缀
如:InputStream的子类FileInputStream,Reader的子类FileReader - 字符流的学习
字符流简单操作的例子:
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("demo.txt");
fw.write("Cyberooike");
fw.close();
}
}
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo2 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter(" demo.txt");
fw.write(" haha");
} catch (IOException e) {
System.out.println(e.toString());
} finally {
try {
if(fw!=null)
fw.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo3 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("demo.txt",true);
fw.write("nihao,");
fw.write("zahngsan\r\nxiexie");
}
}