2020.8.4输入和输出(二)

1.字符输入流:

2.处理中文乱码问题:
原因:idea中文件格式与文档格式不相匹配
方法一:
更改文档格式或者更改idea文件格式
方法二:创建Reader类,调用子类InputStreamReader子类
将idea文件格式更改为与文档格式相同

3.BufferReader类

多个流关闭的时候,遵循先开后关
4.字符输出流:OutputStreamWriter-FileWriter
public class FileWriterDemo {
public static void main(String[] args) {
Writer fw=null;
try {
System.out.println(System.getProperty(“file.encoding”));
fw=new FileWriter(“D:/s.txt/test.txt”,true);
String s=“hello writer”;
fw.write(s);
fw.write(s,0,2);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
解决写入时,中文乱码问题:
public class OutputStreamWriterDemo {
public static void main(String[] args) {
Writer fw=null;
OutputStreamWriter osw=null;
FileOutputStream fos=null;
try {
System.out.println(System.getProperty(“file.encoding”));
fos=new FileOutputStream(“D:/s.txt/test.txt”,true);
osw=new OutputStreamWriter(fos,“UTF-8”);
String s=“hello writer”;
osw.write(s);
osw.write(s,0,2);
osw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.BufferedWriter写入文件:

bw.newLine():换行
6.读写二进制文件(一般用于图片):

public class CopyPic {
public static void main(String[] args) {
DataInputStream dis=null;
FileInputStream fis=null;
DataOutputStream dos=null;
FileOutputStream fos=null;
try {
//输入流
fis=new FileInputStream(“D:/123.jpg”);
dis=new DataInputStream(fis);
//输出流
fos=new FileOutputStream(“D:/s.txt/myCoder.jpg”);
dos=new DataOutputStream(fos);
//输入流地区二进制文件的同时,输出流写入二进制文件
int temp;
while((temp=dis.read())!=-1){
dos.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
dos.close();
fos.close();
dis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
7.序列化和反序列化(创建的类一定要引用接口Serializable)(一般用于类和对象)

序列化是将对象的状态写入到特定的流中的过程。

注:使用集合保存对象,可以将集合中的所有对象序列化
反序列化则是从特定的流中获取数据重新构建对象的过程

public class Student implements Serializable {
private String name;
private String sex;
//避免密码字段被序列化和反序列化
private transient String password;

public Student() {}
public Student(String name, String sex, String password) {
this.name = name;
this.sex = sex;
this.password = password;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
public class SeriaStu {
public static void main(String[] args) {
Student student=new Student(“迪迦”,“男”,“123456”);
//对象输出流
ObjectOutput oos=null;
FileOutputStream fos=null;
ObjectInputStream ois=null;
FileInputStream fis=null;
try {//构建对象输出流,为序列化做准备
fos=new FileOutputStream(“D:/s.txt/student.txt”);
oos=new ObjectOutputStream(fos);
//构建对象输出流,为反序列化做准备
fis=new FileInputStream(“D:/s.txt/student.txt”);
ois=new ObjectInputStream(fis);
//实现对象序列化
oos.writeObject(student);
//实现对象反序列化
Student student1=(Student)ois.readObject();
System.out.println(“反序列化出来的学生信息:”+student1.getName()+student1.getSex()+student1.getPassword());

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
oos.close();
fos.close();
ois.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}
}

总结:
■字符输入流
■Reader-InputStreamReader- FileReader- BufferedReader
■字符输出流
■Writer-OutputStreamWriter-FileWriter- BufferedWriter
■会使用字节流读写二进制文件
■DataOutputStream、 DataInputStream
■使用序列化和反 序列化保持和恢复对象信息
■ObjectOutputStream-序列化
■ObjectInputStream-反序列化

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页