JAVA使用IO对象流输入输出实现文件内容复制和读取输出-----JAVA

这段代码展示了如何使用Java进行文件读写,包括从一个文本文件复制内容到另一个文本文件,以及对象的序列化和反序列化。程序使用了FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream等流处理类,并实现了Serializable接口以进行对象持久化。
摘要由CSDN通过智能技术生成
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 +
'}';
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值