import java.io.*; public class OutPrintSt { public static void main(String[] args) { File file = new File("D:\\新建文本文档.txt"); FileInputStream fileInputStream = null; BufferedInputStream bufferedInputStream = null; InputStreamReader inputStreamReader = null; FileOutputStream fileOutputStream = null; BufferedOutputStream bufferedOutputStream = null; OutputStreamWriter outputStreamWriter = null; ObjectOutputStream objectOutputStream = null; ObjectInputStream objectInputStream = null; try { fileInputStream = new FileInputStream(file); bufferedInputStream = new BufferedInputStream(fileInputStream); inputStreamReader = new InputStreamReader(bufferedInputStream,"UTF-8"); fileOutputStream = new FileOutputStream(new File("D:\\123.txt")); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); outputStreamWriter = new OutputStreamWriter(bufferedOutputStream,"UTF-8"); int len; char[] chars = new char[1024]; while((len = inputStreamReader.read(chars)) != -1) { outputStreamWriter.write(chars,0,len); } objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("D:\\qwe.txt"))); objectOutputStream.writeUTF("江山如此多娇"); objectOutputStream.flush(); objectOutputStream.writeObject(new Deer("鹿头人",new Account(123))); objectOutputStream.flush(); objectInputStream = new ObjectInputStream(new FileInputStream(new File("D:\\qwe.txt"))); String str = objectInputStream.readUTF(); System.out.println(str); Deer deer = (Deer)objectInputStream.readObject(); System.out.println(deer); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } finally { try { if(objectInputStream != null) { objectInputStream.close(); } if(objectOutputStream != null) { objectOutputStream.close(); } if(outputStreamWriter != null) { outputStreamWriter.close(); } if(inputStreamReader != null) { inputStreamReader.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } }
import java.io.*;
public class OutPrintSt
{
public static void main(String[] args)
{
File file = new File("D:\\新建文本文档.txt");
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
InputStreamReader inputStreamReader = null;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
ObjectOutputStream objectOutputStream = null;
ObjectInputStream objectInputStream = null;
try
{
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
inputStreamReader = new InputStreamReader(bufferedInputStream,"UTF-8");
fileOutputStream = new FileOutputStream(new File("D:\\123.txt"));
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
outputStreamWriter = new OutputStreamWriter(bufferedOutputStream,"UTF-8");
int len;
char[] chars = new char[1024];
while((len = inputStreamReader.read(chars)) != -1)
{
outputStreamWriter.write(chars,0,len);
}
objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("D:\\qwe.txt")));
objectOutputStream.writeUTF("江山如此多娇");
objectOutputStream.flush();
objectOutputStream.writeObject(new Deer("鹿头人",new Account(123)));
objectOutputStream.flush();
objectInputStream = new ObjectInputStream(new FileInputStream(new File("D:\\qwe.txt")));
String str = objectInputStream.readUTF();
System.out.println(str);
Deer deer = (Deer)objectInputStream.readObject();
System.out.println(deer);
}
catch (FileNotFoundException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
finally
{
try
{
if(objectInputStream != null)
{
objectInputStream.close();
}
if(objectOutputStream != null)
{
objectOutputStream.close();
}
if(outputStreamWriter != null)
{
outputStreamWriter.close();
}
if(inputStreamReader != null)
{
inputStreamReader.close();
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
}import java.io.Serializable; public class Deer implements Serializable//表示接口 { //用来唯一标识类 private static final long serialVersionUID = 42512124854L; //如果没有声明全局常量,代码运行时会自动生成,在运行时该数值有概率变化,有概率丢失信息 private String name; //类中的属性如果声明为transient或者static就无法序列化,不会保存到文件中 //transient表示瞬时的,故而不会序列化保存到文件中 //static和对象无关,和类有关 private transient int age = 17; static int year; private Account account; public Deer() { } public Deer(String name, Account account) { this.name = name; this.account = account; } public Deer(String name, int age, Account account) { this.name = name; this.age = age; this.account = account; } public Deer(String name) { this.name = name; } @Override public String toString() { return "Deer{" + "name='" + name + '\'' + ", age=" + age + ", account=" + account + '}'; } } class Account implements Serializable { private double balance; private static final long serialVersionUID = 42514122124854L; public Account(double balance) { this.balance = balance; } @Override public String toString() { return "Account{" + "balance=" + balance + '}'; } }
import java.io.Serializable;
public class Deer implements Serializable//表示接口
{
//用来唯一标识类
private static final long serialVersionUID = 42512124854L;
//如果没有声明全局常量,代码运行时会自动生成,在运行时该数值有概率变化,有概率丢失信息
private String name;
//类中的属性如果声明为transient或者static就无法序列化,不会保存到文件中
//transient表示瞬时的,故而不会序列化保存到文件中
//static和对象无关,和类有关
private transient int age = 17;
static int year;
private Account account;
public Deer() {
}
public Deer(String name, Account account) {
this.name = name;
this.account = account;
}
public Deer(String name, int age, Account account) {
this.name = name;
this.age = age;
this.account = account;
}
public Deer(String name) {
this.name = name;
}
@Override
public String toString() {
return "Deer{" +
"name='" + name + '\'' +
", age=" + age +
", account=" + account +
'}';
}
}
class Account implements Serializable
{
private double balance;
private static final long serialVersionUID = 42514122124854L;
public Account(double balance)
{
this.balance = balance;
}
@Override
public String toString()
{
return "Account{" +
"balance=" + balance +
'}';
}
}
JAVA使用IO对象流输入输出实现文件内容复制和读取输出-----JAVA
于 2023-07-11 16:18:04 首次发布
这段代码展示了如何使用Java进行文件读写,包括从一个文本文件复制内容到另一个文本文件,以及对象的序列化和反序列化。程序使用了FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream等流处理类,并实现了Serializable接口以进行对象持久化。
摘要由CSDN通过智能技术生成