基本介绍
区别和联系
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReader_ {
public static void main(String[] args) throws IOException {
String filePath = "E:\\Java\\javaIO\\a.txt";
//创建对象
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
//读取
String line;//按行读取,效率高
//bufferedReader.readLine()是按行读取文件
//当返回null时,表示文件读取完毕
while(( line = bufferedReader.readLine()) != null){
System.out.println(line);
}
//关闭流 只需要关闭BufferedReader 因为底层会自动关闭节点流
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriter_ {
public static void main(String[] args) throws IOException {
String filePath = "E:\\Java\\javaIO\\a.txt";
//创建对象
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write("hello");
bufferedWriter.write("hello");
//插入一个与系统相关的换行
bufferedWriter.newLine();
bufferedWriter.write("hello");
//关闭外层流即可,传入的new FileWriter(filePath)会在底层关闭
bufferedWriter.close();
}
}
import java.io.*;
public class BufferedCopy {
public static void main(String[] args) {
String srcFilePath = "E:\\Java\\javaIO\\a.txt";
String destFilePath = "E:\\Java\\javaIO\\b.txt";
BufferedReader br = null;
BufferedWriter bw = null;
String line;
try{
br = new BufferedReader(new FileReader(srcFilePath));
bw = new BufferedWriter(new FileWriter(destFilePath));
//说明:readLine方法读取一行内容,但是没有换行
while (( line = br.readLine()) != null) {
//每读取一行,就写入
bw.write(line);
//插入一个换行符
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
BufferedReader、BufferedWriter是按照字符操作
不要去操作二进制文件,可能会导致文件损坏
import java.io.*;
public class BufferedCopy2 {
public static void main(String[] args) {
String srcFilePath = "E:\\Java\\javaIO\\a.txt";
String destFilePath = "E:\\Java\\javaIO\\b.txt";
//创建对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(srcFilePath));
bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
//循环读取文件,并写入到destFilePath
byte[] buff = new byte[1024];
int readLine = 0;
//当返回-1时,表示文件读取完毕
while ((readLine = bis.read(buff)) != -1) {
bos.write(buff, 0, readLine);
}
System.out.println("文件拷贝完毕");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流,关闭外层的处理流即可,底层会失去关闭节点流
try{
if(bis != null){
bis.close();
}
if(bos != null){
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
序列化和反序列化
功能:提供了对基本类型或对象类型的序列化和反序列化的方法
序列化存储数据
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectOutStream {
public static void main(String[] args) throws IOException {
//序列化后,保存的文件格式不是纯文本,而是按照它的格式来保存
String srcFilePath = "E:\\Java\\javaIO\\a.txt";
//创建对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(srcFilePath));
//序列化数据到目录下
oos.writeInt(100);//int-->Integer 实现了Serializable
oos.writeBoolean(true);//boolean-->Boolean 实现了Serializable
oos.writeChar('a');//char-->Character 实现了Serializable
oos.writeDouble(9.5);//double-->Double 实现了Serializable
oos.writeUTF("啦啦啦");//String
//保存一个dog对象
oos.writeObject(new dog("旺财",10));
oos.close();
System.out.println("数据保存完毕(序列化形式)");
}
}
//如果需要序列化某个类的对象,必须实现了Serializable接口
class dog implements Serializable {
private String name;
private int age;
public dog(String name, int age){
this.name = name;
this.age = age;
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ObjectInput {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//指定反序列化的文件
String filePath = "E:\\Java\\javaIO\\a.txt";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
//读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致,否则会出现异常
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readChar());
System.out.println(ois.readDouble());
System.out.println(ois.readUTF());
Object o = ois.readObject();
System.out.println("运行类型=" + o.getClass());
System.out.println("dog信息=" + o);//底层 Object-->dog
//关闭流,关闭外层流即可,底层会关闭FileInputStream类
ois.close();
}
}
类型 | 默认设备 | |
---|---|---|
System.in标准输入 | InputStream | 键盘 |
System.out标准输出 | PrintStream | 显示器 |
public class InputAndOutput {
public static void main(String[] args) {
//System类 public static InputStream in = null;
//System.in的编译类型 InputStream
//System.in 运行类型 BufferedInputStream
System.out.println(System.in.getClass());
//System类 public static final PrintStream out = null;
//编译类型 PrintStream
//运行类型 PrintStream
//表示标准输出 显示器
System.out.println(System.out.getClass());
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputReader {
/**
* 使用转换流解决中文乱码问题
* 将字节流FileInputStream转成InputStreamReader字符流
* @param args
*/
public static void main(String[] args) throws IOException {
String filePath = "E:\\Java\\javaIO\\a.txt";
InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
System.out.println("读取内容=" + s);
//关闭外层流
br.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class OutputWriter {
/**
* 演示OutputStreamWriter使用
* 把FileOutputStream字节流转成字符流OutputStreamWriter
* 指定处理的编码
* @param args
*/
public static void main(String[] args) throws IOException {
String filePath = "E:\\Java\\javaIO\\b.txt";
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath),"gbk");
osw.write("hello");
osw.close();
System.out.println("按照gbk保存文件");
}
}
import java.io.IOException;
import java.io.PrintStream;
public class Print {
public static void main(String[] args) throws IOException {
PrintStream out = System.out;
//默认情况下,PrintStream输出数据的位置是标准输出即显示器
out.println("hello");
//因为println底层使用的是write,所以我们也可以直接调用write方法进行输入/输出
out.write("hello".getBytes());
out.close();
}
}
PrintWriter pw = new PrintWriter(System.out);
pw.print("hello");
pw.close();
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Properties01 {
public static void main(String[] args) throws IOException {
//创建对象
Properties properties = new Properties();
//加载指定配置文件
properties.load(new FileReader("E:\\Java\\javaIO\\a.txt"));
//把键值对显示到控制台
properties.list(System.out);
//根据key获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名= " + user);
System.out.println("密码= " + pwd);
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Properties02 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//创建文件
//如果该文件没有key,就是创建
//如果该文件有key,就是修改
properties.setProperty("charset","utf8");
properties.setProperty("user","tom");
properties.setProperty("pwd","abc123");
//将键值对存储到文件里
properties.store(new FileOutputStream("E:\\Java\\javaIO\\a.txt"),null);
System.out.println("保存配置文件成功~");
}
}