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;
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 +
'}';
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库