Java提供文件读写操作有很多种,这里介绍几种供参考
字节流
FileInputStream读取文件
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
File file = new File("d:/index.sh");
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
FileInputStream fis = new FileInputStream(file);
int len;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
fis.close();
}
}
FileOutputStream写文件
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
File file = new File("d://1.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
try (fileOutputStream) {
String str = "hello world";
for (int i = 0; i < str.length(); i++) {
int b = str.charAt(i);
fileOutputStream.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
缓冲流使用
BufferedOutputStream
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
fileWriter();
}
public static void fileWriter() throws IOException {
FileOutputStream fos = new FileOutputStream("d:/1.txt", true);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);
try (bufferedOutputStream) {
byte[] bytes = "hello world".getBytes();
bufferedOutputStream.write(bytes);
bufferedOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedOutputStream读取文件
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
fileRead();
}
private static void fileRead() throws IOException {
FileInputStream fis = new FileInputStream("d:/1.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
try (bis) {
byte[] buffer = new byte[10240];
int flag;
while ((flag = bis.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, flag));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
字符缓冲输出流
BufferedWriter写文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriteDemo {
public static void main(String[] args) throws IOException {
method();
}
private static void method() throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("d:/2.txt"));
try (bw) {
bw.write("hello world");
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedReader读取文件
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
method();
}
private static void method() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("d:/2.txt"));
String line;
try (br) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
对象流
ObjectOutputStream读取文件
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person implements Serializable {
private String id;
private String name;
private Integer age;
}
import java.io.*;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String fileName3 = "d:/1.txt";
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName3))) {
oos.writeObject(new Person("11", "hello", 24));
}
}
}
ObjectInputStream写文件
import java.io.*;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String fileName3 = "d:/1.txt";
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName3))) {
Object res = ois.readObject();
System.out.println(res);
Object res2 = ois.readObject();
System.out.println(res2);
}
}
}
总结
文件流的读写,看个人擅长以及读写效率,最重要的是,读写完毕之后,记得关闭文件流