输入和输出处理(二)
一、字符流读写文本文件
1.使用FileReader读取文件
(1)引入相关的类
(2)构造FileReader对象
(3)读取文本文件的数据
(4)关闭相关的流对象
public static String read(String path) throws IOException {
FileReader fr=new FileReader(path);
int tmp=0;
StringBuilder sb=new StringBuilder();
while (-1!=(tmp=fr.read())) {
sb.append((char)tmp);
}
fr.close();
return sb.toString();
}
public static void main(String[] args) {
try {
String s = read("a.txt");
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
2.使用FileWriter写文件
(1)引入相关的类
(2)创建FileWriter对象
(3)写文本文件
(4)关闭相关的流对象
public static void write(String path,String str,boolean isAppend)throws IOException{
FileWriter fw=new FileWriter(path,isAppend);
fw.write(str);
fw.close();
}
public static void main(String[] args) {
try {
String s = read("a.txt");
System.out.println(s);
write("a.txt",s,true);
} catch (IOException e) {
e.printStackTrace();
}
}
二、缓冲流读写文本文件
1.使用BufferReader读取文件
(1)引入相关的类
(2)构造BufferReader对象和FileReader对象
(3)调用readLine()方法读取数据
(4)关闭文件流对象
public static String readBuffer(String path) throws IOException {
FileReader fr=new FileReader(path);
BufferedReader br=new BufferedReader(fr);
String tmp;
StringBuffer sb=new StringBuffer();
while (null!=(tmp=br.readLine())){
sb.append(tmp+"\n");
}
br.close();//后开先关
fr.close();//先开后关
return sb.toString();
}
public static void main(String[] args) {
try {
String s = readBuffer("pet.txt");
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
2.使用BufferWriter读取文件
(1)引入相关的类
(2)构造BufferWriter对象和FileWriter对象
(3)调用writer()方法写数据
(4)关闭文件流对象
public static void writeBuffer(String path,String rst,boolean isAppend)throws IOException{
FileWriter fw=new FileWriter(path,isAppend);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(rst);
bw.close();
fw.close();
}
public static void main(String[] args) {
try {
String s = readBuffer("pet.txt");
System.out.println(s);
writeBuffer("pet.txt",s,true);
} catch (IOException e) {
e.printStackTrace();
}
}
三、转换流
转换流解决了字节流读取文本文件时可能出现的中文乱码问题,有了转换流,就可以处理.doc文档了,不管.doc文件是否有图片或文字,都可以使用转换流进行读取、写出。
public static String readCharset(String path) throws IOException {
FileInputStream fis=new FileInputStream(path);
InputStreamReader isr=new InputStreamReader(fis,"GBK");
BufferedReader fr=new BufferedReader(isr);
String tmp;
StringBuffer sb=new StringBuffer();
while (null!=(tmp=fr.readLine())){
sb.append(tmp+"\n");
}
fis.close();
isr.close();
fr.close();
return sb.toString();
}
public static void main(String[] args) {
try {
String s=readCharset("");
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
四、二进制流读写文本文件
1.使用DataInputStream读二进制文件
(1)引入相关的类
(2)构造数据输入流对象
(3)调用read()方法读取二进制数据
(4)关闭数据输入流
2.使用DataOutputStream读二进制文件
(1)引入相关的类
(2)构造数据输出流对象
(3)调用write()方法写二进制文件的数据
(4)关闭数据输出流
public static void copyDataFile(String inPath,String outPath)throws IOException {
FileInputStream fis=new FileInputStream(inPath);
DataInputStream dis=new DataInputStream(fis);
byte[] b=new byte[dis.available()];
dis.read(b);
// System.out.println(new String(b));
FileOutputStream fos=new FileOutputStream(outPath);
DataOutputStream dos=new DataOutputStream(fos);
dos.write(b);
dos.close();
fos.close();
dis.close();
fis.close();
}
public static void main(String[] args) throws IOException{
copyDataFile("C:\\Users\\pc\\Desktop\\1.jpg","2.jpg");
}
四、对象流、序列化和反序列化
1.序列化和反序列化
场景一:内存对象需要在其他环境下使用
两个进程间进行网络通信时,无论是发送何种类型的数据,均需以二进制序列形式进行传送
(1)发送方必须将数据对象(比如Java对象)转化为字节序列
(2)接收方则需要将接收到的字节序列再还原成Java对象
场景二:内存对象需要在将来某个时间使用
将内存中的数据对象永久存储在磁盘中(持久化)
2.序列化和反序列化的过程
序列化是将对象的状态写入到特定的流中的过程
反序列化则是从特定的流中获取数据重新构建对象的过程
3.实现序列化的步骤
(1)实现Serializable接口
(2)创建对象输出流
(3)调用writeObject()方法将对象写入文件
(4)关闭对象输出流
4.实现反序列化的步骤
(1)实现Serializable接口
(2)创建对象输入流
(3)调用readObject()方法读取对象
(4)关闭对象输入流
public class Student implements Serializable {//序列化接口
private int id;
private String name;
private double score;
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public Student(){}
}
public static void writeObject(String path,boolean isAppend,Object obj) throws IOException {
FileOutputStream fis=new FileOutputStream(path);
ObjectOutputStream oos=new ObjectOutputStream(fis);
oos.writeObject(obj);
oos.close();
fis.close();
}
public static Object readObject(String path) throws Exception{
FileInputStream fis=new FileInputStream(path);
ObjectInputStream ois=new ObjectInputStream(fis);
Object o=ois.readObject();
ois.close();
fis.close();
return o;
}
public static void main(String[] args) {
Student s=new Student(45,"飞跃",89.22);
try {
writeObject("obj.txt",true,s);
Object o=readObject("obj.txt");
if (o instanceof Student){
Student stu=(Student) o;
System.out.println(o);
}
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}