目录
字节缓冲流
序列化
使用对象流实现序列化
标准输入输出流
转换流
打印流
PrintStream
PrintWriter
properties读写文件
字节缓冲流
字节流
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCopy {
public static void main(String[] args) {
//文件拷贝
FileInputStream fileInputStream=null;//输入流
FileOutputStream fileOutputStream=null;//输出流
//源文件和目标文件
String srcpath="C:\\Users\\Elysia\\Pictures\\Saved Pictures\\illust_86465238_20210124_003335.jpg";
String destpath="D:\\javacode\\IO\\src\\pic.jpg";
try {
fileOutputStream=new FileOutputStream(destpath,true);
fileInputStream=new FileInputStream(srcpath);
byte[] buf=new byte[1024];
int len;
while((len=fileInputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
fileInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
序列化
将java对象转化成字节序列,可以保存或传输,以备以后重新恢复。
意义:使得对象可以脱离程序,独立存在
前提:该对象的类支持序列化,必须实现两个接口之一
- Serializable 只是一个标记接口,无需实现任何方法
- Externalizble
#所有可能网络传输的对象,都应该是可序列化的类
使用对象流实现序列化
字节流,处理流,用于保存对象,传输对象。
步骤:
- 创建一个ObjectOutputStream,处理流,须建立在其他节点流之上
- 使用writeObject() 将对象序列化输出
反序列化
- 创建一个ObjectInputStream
- 调用readObject()方法读取流中的对象,若知道对象类型,可强转
package pack;
import java.io.Serializable;
//实现Serializable
public class MidObj implements Serializable {
private String name;
private int data;
public MidObj(String name, int data) {
this.name = name;
this.data = data;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
package Serialize;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import pack.MidObj;//导包很重要
public class Serialize {
public static void main(String[] args) throws Exception{
String path="D:\\javacode\\IO\\src\\Serialize\\obj.txt";
MidObj obj=new MidObj("Serialize系列化",114);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream(path));
objectOutputStream.writeObject(obj);
objectOutputStream.close();
}
}
package Deserialize;
import pack.MidObj;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class Deserialize {
public static void main(String[] args) throws Exception{
String path="D:\\javacode\\IO\\src\\Serialize\\obj.txt";
ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(path));
MidObj midObj=(MidObj) objectInputStream.readObject();
System.out.println(midObj.getName()+midObj.getData());
objectInputStream.close();
}
}
细节:
- static和transient修饰的成员不会被序列化
- 建议添加 序列版本号 SerialVersionUID,提高版本兼容性
- 要求里面属性的类型也是实现序列化的
- 序列化具备可继承性,父类实现序列化,其所有子类也默认实现了
标准输入输出流
system.in 编译类型 InputStream
运行类型 BufferedInputStream
System.out 编译类型 PrintStream
运行类型 PrintStream
转换流
由于编码方式多种,容易导致乱码问题,因此需要转换流
Reader的子类
Charset 编码方式
Writer的子类
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReader_ {
public static void main(String[] args) throws Exception{
String path="D:\\javacode\\IO\\src\\test.txt";
String Charset="UTF-8";
InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(path),Charset);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
String line;
while((line=bufferedReader.readLine())!=null){
System.out.println(line);
}
System.out.println("按照"+Charset+"读取文本");
bufferedReader.close();
}
}
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class OutputStreamWriter_ {
public static void main(String[] args) throws Exception{
String path="D:\\javacode\\IO\\src\\test.txt";
String Charset="UTF-8";
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(path),Charset);
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
bufferedWriter.write("按照UTF-8写入");
bufferedWriter.close();//一定要关
}
}
一定要关闭流
打印流
PrintStream
字节流,只有输出流
System.out就是字节打印流
import java.io.PrintStream;
public class PrintStream_ {
public static void main(String[] args) throws Exception{
PrintStream out =System.out;
out.println("666");
out.write("hello".getBytes());//print底层就是write
PrintStream ps=new PrintStream("D:\\javacode\\IO\\src\\test.txt");//输出到指定位置
ps.println("Never can we suspend the time");
ps.close();
}
}
PrintWriter
字符流,只有输出流
import java.io.FileWriter;
import java.io.PrintWriter;
public class PrintWriter_ {
public static void main(String[] args) throws Exception{
PrintWriter out=new PrintWriter(System.out);
out.println("hello");
out.close();//注意close
PrintWriter pw=new PrintWriter(new FileWriter("D:\\javacode\\IO\\src\\test.txt",true));
pw.println("成功写入");
pw.close();
}
}
properties读写文件
集合类,底层为hashTable
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Properties;
public class properties_ {
// public static void main(String[] args) throws Exception{
// String path="D:\\javacode\\IO\\src\\druid.properties";
// BufferedReader bufferedReader=new BufferedReader(new FileReader(path));
// String line;
// while((line=bufferedReader.readLine())!=null){
// String[] split=line.split("=");
// System.out.println(split[0]+"都值为"+split[1]);
// }
// bufferedReader.close();
// }
public static void main(String[] args) throws Exception{
Properties properties=new Properties();
String path="D:\\javacode\\IO\\src\\druid.properties";
String path0="D:\\javacode\\IO\\src\\druid0.txt";
properties.load(new FileReader(path));
properties.list(System.out);//打印流 字节打印流
PrintWriter pw=new PrintWriter(new FileWriter(path0));
properties.list(pw);//打印流
pw.close();
properties.setProperty("username","root1");
System.out.println("\nuser:"+properties.getProperty("username")+"\npwd:"+properties.getProperty("password"));
}
}
import java.io.FileOutputStream;
import java.util.Properties;
public class properties_1 {
public static void main(String[] args) throws Exception{
Properties properties=new Properties();
properties.setProperty("username","root");
properties.setProperty("password","111111");
properties.setProperty("charset","UTF-8");
String path="D:\\javacode\\IO\\src\\druid.properties";
properties.store(new FileOutputStream(path),null);
}
}