【Java】IO流相关操作

2023-12-22 21:53:49

目录

常见的文件操作

创建文件

得到文件信息

目录操作

IO流

FileInputStream?

FileOutputStream

FileReader

FileWriter

BufferedReader

BufferedWriter

ObjectOutputStream

ObjectInputStream

InputStreamReader

OutputStreamReader

PrintStream

PrintWriter

Properties

properties读文件

properties写文件

注意


常见的文件操作

创建文件

//根据路径创建一个File对象
public File(String pathname)
//根据 父目录 + 子路径 创建一个File对象
public File(String parent, String child)
//根据 父目录文件 + 子路径 创建一个File对象
public File(File parent, String child)
//创建新文件
public boolean createNewFile()

得到文件信息

//得到文件名
System.out.println(file.getName());
//绝对路径
System.out.println(file.getAbsolutePath());
//父目录
System.out.println(file.getParent());
//文件大小 单位:字节
System.out.println(file.length());
//文件是否存在
System.out.println(file.exists());
//是不是一个文件
System.out.println(file.isFile());
//是不是一个目录
System.out.println(file.isDirectory());

目录操作

//创建一级文件目录
public boolean mkdir()
//示例
String filePath = "E:\\test";
File file = new File(filePath);
file1.mkdir()

//创建多级文件目录
public boolean mkdirs()
//示例
String filePath1 = "E:\\test\\demo\\a";
File file1 = new File(filePath1);
file1.mkdirs()

IO流

FileInputStream?

创建流

public FileInputStream(String name)
//示例
String filePath = "E:\\hello.txt";
FileInputStream fileInputStream = new FileInputStream(filePath);

读取数据常用api

//单个字节读取,返回下一个字节数据,读完返回-1
public int read()
//示例
int readData = 0;
while((readData = fileInputStream.read()) != -1){
      System.out.print((char)readData);
}
//字节数组读取,字节读取到数组,返回读取的字节个数,读完返回-1
public int read(byte b[])
//示例
int readLength;
byte[] buf = new byte[8];
while((readLength = fileInputStream.read(buf)) != -1){
      System.out.print(new String(buf,0,readLength));
}

FileOutputStream

创建流

//如果叠加可以将append参数设置为true,false则覆盖原文件
public FileOutputStream(String name, boolean append)

输出数据常用api

//单个字节输出
public void write(int b) 
//数组输出
public void write(byte b[])
public void write(byte b[], int off, int len)

FileReader

创建流

public FileReader(String fileName)
//示例
String filePath = "E:\\story.txt";
FileReader fileReader = = new FileReader(filePath);

读取数据常用api

//读取单个字符
public int read()
//示例
while((data = fileReader.read()) != -1) {
       ystem.out.print((char)data);
}

//字符数组读取
public int read(char cbuf[], int offset, int length) 
//示例
char[] temp = new char[100];
int readLen = 0;
while((readLen = fileReader.read(temp)) != -1) {
      System.out.print(new String(temp,0,readLen));
}

FileWriter

创建流

public FileWriter(String fileName)
//示例
String filePath = "E:\\note.txt";
FileWriter fileWriter = new FileWriter(filePath);

?写数据常用api

//写字符串
public void write(String str)
//写字符数组
public void write(char cbuf[])

BufferedReader

创建流

//包装流
public BufferedReader(Reader in)
//示例
String filePath = "E:\\story.txt";
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));

读取数据常用api

//按行读取
public String readLine()
//示例
String s = "";
while((s = bufferedReader.readLine()) != null){
    System.out.println(s);
}

BufferedWriter

创建流

public BufferedWriter(Writer out)
//示例
String filePath = "E:\\BufferedWriterDemo.txt";
//在源文件上追加的话,在被包装的流设置append参数
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));

写数据常用api

//写字符串
public void write(String str)
//换行
public void newLine()

ObjectOutputStream

创建流

public ObjectOutputStream(OutputStream out)
//示例
String filePath = "E:\\data.txt";
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filePath));

序列化对象常用api

//序列化数据到文件中
out.writeInt(100);//int -> Integer自动装箱,Integer实现了Serializable接口
out.writeBoolean(true);
out.writeChar('g');
out.writeDouble(1001);
out.writeUTF("放入字符串");//String
out.writeObject(new Dog("菲",23));

ObjectInputStream

创建流

public ObjectInputStream(InputStream in)
//示例
String filePath = "E:\\data.txt";
ObjectInputStream in = new ObjectInputStream(new FileInputStream(filePath));

?反序列化对象常用api

//想要使用其反序列化的对象的方法,需要向下转型
//读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致
System.out.println(in.readInt());
System.out.println(in.readBoolean());
System.out.println(in.readChar());
System.out.println(in.readDouble());
System.out.println(in.readUTF());
Object dog = in.readObject();
System.out.println("运行类型:" + dog.getClass());
System.out.println(dog);
Dog temp = (Dog)dog;
System.out.println(temp.getName());

InputStreamReader

创建流

public InputStreamReader(InputStream in, String charsetName)
//示例
String filePath = "E:\\story.txt";
//FileInputStream 包装成 InputStreamReader
InputStreamReader in = new InputStreamReader(new FileInputStream(filePath),"gbk");
BufferedReader br = new BufferedReader(in);

字符转换常用api

public String readLine()
//示例
String s = "";
while((s = br.readLine()) != null){
    System.out.println(s);
}

OutputStreamReader

创建流

public OutputStreamWriter(OutputStream out, String charsetName)
//示例
String filePath = "E:\\trans.txt";
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath),"gbk"));

字符转换常用api

//字符输出
public void write(String str)

PrintStream

创建流

public PrintStream(OutputStream out)
//示例
//在默认情况下PrintStream输出数据的位置时 标准输出 即显示器
PrintStream out = new PrintStream(System.out);

常用api?

//输出字符串
public void print(String s)

//注:可以调用System的setOut方法修改打印流输出的位置或设备
System.setOut(new PrintStream("E:\\f1.txt"));

PrintWriter

创建流

public PrintWriter(OutputStream out)
//示例
PrintWriter out =  new PrintWriter(System.out);
PrintWriter o2 = new PrintWriter(new FileWriter("E:\\f2.txt"));

常用api

//写出一个字符串
public void write(String s)

Properties

properties读文件

创建

public Properties()
//示例:创建对象并加载配置文件
Properties properties = new Properties();
properties.load(new FileReader("src\\a.properties"));

常用api?

//获取文件中的所有信息
properties.list(System.out);
//根据key获取对应的值
System.out.println(properties.getProperty("username"));
System.out.println(properties.getProperty("password"));

properties写文件

创建

Properties properties = new Properties();

常用api

//如果有key则修改,如果没有则添加
properties.setProperty("charset","utf-8");
properties.setProperty("user","dd");
properties.setProperty("password","123456");
//写入信息,传入一个输出流和注释(可不写=>null)
properties.store(new FileWriter("src\\a2.properties",true),"test");

注意

1、再用io流写文件时,写完之后要刷新流或者关闭流,要不然无法写入

2、包装流使用到了装饰器设计模式

3、对象序列化和反序列化时,序列化和反序列化的对象应该来自同一个位置的同一个类

4、字节流更适合来处理二进制文件

文章来源:https://blog.csdn.net/weixin_63144583/article/details/135142418
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。