java按照读写的数据单位将流划分为两大类
字节流,字符流
字节流是以字节为单位读写数据的流,由于计算机底层都是2进制,所以字节流可以读写任意数据
字符流是以字符为单位读写数据的流,实际底层还是读写字节
但是字符流会自动将字节与字符进行对应的转换工作,
因此字符流天生具备字符编解码能力,对此字符流也仅适用于读写文本数据
java.io.Reader,java.io.Writer他们是所有字符输入流
与字符输出流的超类,规定了读写字符的相关方法
转换流
java.io.InputStreamReader,java.io.OutputStreamWriter
它们是字符流常用的一堆实现类,同时也是一对高级流.在流链接中
起到非常重要的一环,承上启下,衔接字节流与其他字符流使用
转换流是唯一一个可以与字节流连接 的字符流
(因为低级流通常都是字节流,而读写字符时我们会使用功能更多的字符高级流,而字符流通常只能连接其他字符流)
OutputStreamWriter演示
public class OSWDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("OSW.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
osw.write("兮兮");
osw.write("黼黻");
System.out.println("写出完毕");
osw.close();
}
}
InputStreamReader演示
public class ISRDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("osw.txt");
InputStreamReader isr = new InputStreamReader(fis,"gbk");
/**
* 字符流提供的读取字符操作
* int read()
* 该方法会一次读取一个字符,具体实际读取了多少字节取决于指定的字符集以及对应的字符(字符流会自行判定)
* 读取字符后还是以int形式返回,若该值为-1依然表示流读取到了末尾.否则该数据表示的就是读取到的对应字符,转换为char即可
*/
int d = -1;
while((d = isr.read())!=-1){
char c = (char)d;
System.out.print(c);
}
isr.close();
}
}
java.io.PrintWriter
具有自动行刷新的缓冲字符输出流,
特点是可以按行写出字符串,并且可以自动行刷新
注:
java.io.BufferedWriter是缓冲字符输出流,内部有缓冲区
可以进行块写操作提高效率,而PrintWriter就是通过连接它实现的缓冲功能(PW的很多构造方法内自动连接它)
PrintWriter演示
public class PWDemo {
public static void main(String[] args) throws FileNotFoundException, IOException {
/*
* PW支持两个直接对文件写操作的构造方法:
* PrintWriter(File f)
* PrintWriter(String path)
*
* 以上两种构造方法都支持一个重载,就是再传入一个参数允许我们指定写出字符时的字符集
*/
PrintWriter pw = new PrintWriter("pw.txt","GBK");
pw.println("A");
pw.println("B");
System.out.println("写入完毕");
pw.close();
}
}
在流链接中使用PW
public class PWDemo2 {
public static void main(String[] args) throws IOException {
/*
*使用流链接形式向文件中 写出字符串
*/
FileOutputStream fos = new FileOutputStream("pw2.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
/* PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(FileOutputStream("pw2.txt"),"gbk")));*/
pw.println("a");
pw.println("b");
System.out.println("over");
pw.close();
}
}
java.io.BufferedReader
缓冲字符输入流
特点:可以按行读取字符串
BufferedReader演示
public class BRDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("src/io/BRDemo.java");
InputStreamReader isr = new InputStreamReader(fis,"GBK");
BufferedReader br = new BufferedReader(isr);
/*
* String readLine()
* 连续读取若干字符,知道读取到了换行符为止,然后将换行符之前的所有字符组成一个字符串返回
* 注意:返回的字符串中不包含最后读取到的换行符
* 当读到了末尾,则返回null.
*/
String line = null;
while((line = br.readLine())!= null){
System.out.println(line);
}
br.close();
}
}
完成简易记事本功能
public class Note {
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入文件名:");
String fileName = sc.nextLine();
/*
* 当创建PW时第一个参数为一个流时,那么就可以再传入一个boolean值类型参数
* 若该值为true.那么当前PW就具有了自动行刷新功能,即:
* 每当使用println方法写出一行字符串后就会自动flush
*
* 注:使用自动行刷新可以提高写出数据的即时性,但是由于会提高写出次数,必然导致写效率降低
*
*/
PrintWriter pw = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(fileName),"GBK"
)
),true //打开自动行刷新功能 不需要之后使用flush方法
);
System.out.println("请输入内容(键入exit程序退出):");
String str;
while(true){
str = sc.nextLine();
if("exit".equals(str.toLowerCase())){
break;
}
pw.println(str);
// pw.flush();
}
System.out.println("再见");
sc.close();
pw.close();
}
}