字符缓冲流
原理:底层自带了长度为8192的缓冲区提高性能。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BufferedStringdemo01 {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("F:\\javaEE\\src\\MyCharset\\a.txt"));
/*String s = br.readLine();
System.out.println(s);*/
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
package MyCharset;
import java.io.*;
public class BufferedStringdemo02 {
public static void main(String[] args) throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("F:\\javaEE\\src\\MyCharset\\a.txt"));
bw.write("hhhhhhhh");
bw.newLine();
bw.write("aaaaaaa");
bw.newLine();
bw.close();
}
}
转换流
是字符流和字节流之间的桥梁。
package myConvertstream;
import java.io.*;
public class mydemo01 {
public static void main(String[] args) throws IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("myio\\gbkfile.txt"),"GBK") ;
int ch;
while((ch=isr.read())!=-1){
System.out.println((char)ch);
}
isr.close();
}
}
package myConvertstream;
import java.io.*;
import java.nio.charset.Charset;
public class mydemo01 {
public static void main(String[] args) throws IOException {
/* int ch;
while((ch=isr.read())!=-1){
System.out.println((char)ch);
}
isr.close();*/
FileReader fr=new FileReader("myio\\gbkfile.txt", Charset.forName("GBK"));
int ch;
while((ch=fr.read())!=-1){
System.out.println((char)ch);
}
fr.close();
}
}
package myConvertstream;
import java.io.*;
public class mcdemo01 {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("myio\\b.txt"),"GBK");
osw.write("hellohello");
osw.close();
}
}
package myConvertstream;
import java.io.*;
public class mcdemo02 {
public static void main(String[] args) throws IOException {
/*将本地GBK文件转换成UTF-8*/
//jdk11以前的方案
InputStreamReader isr=new InputStreamReader(new FileInputStream("myio\\b.txt"),"GBK");
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("myio\\d.txt"),"UTF-8");
int b;
while((b=isr.read())!=-1){
osw.write(b);
}
osw.close();
isr.close();
}
}
package myConvertstream;
import java.io.*;
public class mcdemo03 {
public static void main(String[] args) throws IOException {
/*利用字节流读取文件中的数据,每次读一整行,而且不能出现乱码*/
FileInputStream fis=new FileInputStream("myio\\a.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String s = br.readLine();
System.out.println(s);
br.close();
}
}
序列化流
可以把JAVA中的对象写到本地文件中。
import java.io.Serializable;
public class Student implements Serializable {
private int age;
private String name;
public Student() {
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class odemo01 {
public static void main(String[] args) throws IOException {
Student stu=new Student(24,"karry");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("myio\\a.txt"));
oos.writeObject(stu);
oos.close();
}
}
反序列化流
可以把序列化到本地文件中的对象,读取到程序中来。
package myConvertstream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class odemo02 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("myio\\a.txt"));
Object o = ois.readObject();
System.out.println(o);
ois.close();
}
}
序列化流和反序列化流的细节
1.使用序列化流将对象写到文件中时,需要让Javabean类实现Serializable接口,否则会出现NotSerializableException异常。
2.序列化流写到文件中的数据是不能修改的,一旦修改就无法再次读回来了。
3.序列化对象后,修改了JAVABean类,在次反序列化,会出现版本号异常。
4.给成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程。
打印流
分类:打印流一般是指:PrintStream、PrintWriter两个类
特点:
1.打印流只操作文件目的地,不操作数据源
2.特有的写出方法可以实现,数据原样写出
3.特有的写出方法,可以实现自动刷新,自动换行