package com.dragon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @Description:
* @Version:
* @Time: 10:32 2024/08/20
* @Author: zbl
*/
public class FileApp {
private static final Logger log = LoggerFactory.getLogger(FileApp.class);
private static final String path = System.getProperty("user.dir") + File.separator + "file-stream" + File.separator + "hello.txt";
private static final String pathByObject = System.getProperty("user.dir") + File.separator + "file-stream" + File.separator + "person.txt";
public static void main(String[] args) {
writeFile();
System.out.println("==============================================");
readFile();
System.out.println("======================序列化操作========================");
writeObjectFile();
readObjectFile();
}
/**
* 读取文件
*/
public static void readFile(){
log.info("文件地址:{}",path);
File file = new File(path);
try (
FileInputStream fileInputStream = new FileInputStream(file); // 创建文件输入流
InputStreamReader isr = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8); // 创建输入流读取器
BufferedReader br = new BufferedReader(isr) // 创建缓冲读取器
){
String line;
while ((line = br.readLine()) != null){
log.info(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 写入文件
*/
public static void writeFile(){
File file = new File(path);
StringBuilder builder = new StringBuilder();
try (
FileOutputStream fileOutputStream = new FileOutputStream(file); // 创建文件输出流
OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8); // 创建输出流写入器
BufferedWriter bw = new BufferedWriter(osw) // 创建缓冲写入器
){
builder.append("现在是").append(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append("写下《出师表》");
builder.append(System.lineSeparator());
builder.append("先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。");
builder.append(System.lineSeparator());
builder.append("宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。");
builder.append(System.lineSeparator());
builder.append("侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。");
bw.write(builder.toString());
bw.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
log.info("文件写入成功");
}
}
/**
* 序列化文件读取和写入
*/
public static void writeObjectFile(){
File file = new File(pathByObject);
try (
FileOutputStream fileOutputStream = new FileOutputStream(file); // 创建文件输出流
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream) // 创建对象输出流
){
oos.writeObject(new Person("zbl", 18));
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
log.info("文件写入成功");
}
}
public static void readObjectFile(){
File file = new File(pathByObject);
try {
FileInputStream fileInputStream = new FileInputStream(file); // 创建文件输入流
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); // 创建对象输入流
Person o = (Person) objectInputStream.readObject();
log.info(o.toString());
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
log.info("文件读取成功");
}
}
static class Person implements Serializable{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
标签:file,IO,Stream,age,File,new,public,name
From: https://www.cnblogs.com/dragon-925/p/18369832