输入输出流java实验

输入/输出流

 

  • 题目

 1. 演示通过字节流实现文件复制功能。

提示:使用 FileInputStream 类/FileOutputStream 类。

2. 读取一个文本文件,并显示该文本的内容,同时写到另外一个文本文件中。(注意文本存放位置!!)

提示:使用 File 类进行文件或目录的操作。

 

  • 程序编写

1.

package io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyFile {
    public static void main(String[] args) {
        try {
            //文件输入流
            FileInputStream fileInputStream = new FileInputStream("D:/a.txt");
            //文件输出流
            FileOutputStream fileOutputStream = new FileOutputStream("D:/remap.txt");
            //从此输入流读取下一字节,字节值被转换为0-255的一个无符号整数返回,若是已经读到文件末尾,无字节可读时,则会返回-1
            int read = fileInputStream.read();
            while(read!=-1){
                fileOutputStream.write(read);//读的内容返回值不等于-1,一边读一边写
                read = fileInputStream.read();
            }
            fileInputStream.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行:

de02935545cbf9d006fcb468f1a6d306b5c14d3f?

2.

package io;

import java.io.*;
public class ByteCharacterConversionStream {

    /**
     * 写文件
     * authorbw*/
    public void writeFile(String filePathAndName,String fileContent){
        try {

            FileOutputStream fos=new FileOutputStream(filePathAndName);

            OutputStreamWriter osw=new OutputStreamWriter(fos);//创建字符流--需要把字节流转成字符流

            BufferedWriter bw=new BufferedWriter(osw);//创建字符缓冲输入输出流

            if(fileContent!=null){//确定要写入文件的内容不为空
                bw.write(fileContent);
                bw.newLine();
            }

            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读文件*/
    public void readFile(String filePathAndName){
        try {
            //System.out.println(filePathAndName);
            FileInputStream fis=new FileInputStream(filePathAndName);


            Reader reader=new InputStreamReader(fis);//创建字符流--需要把字节流转成字符流


            BufferedReader br=new BufferedReader(reader);//创建字符缓冲输入输出流

            String str;
            while((str=br.readLine())!=null){
                System.out.println(str);
            }

            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制文件*/
    public void copyFile(String oldFilePath,String newFilePath){
        try {
            FileInputStream fis=new FileInputStream(oldFilePath);
            InputStreamReader isr=new InputStreamReader(fis);
            BufferedReader br=new BufferedReader(isr);

            FileOutputStream fos=new FileOutputStream(newFilePath);
            OutputStreamWriter osw=new OutputStreamWriter(fos);
            BufferedWriter bw=new BufferedWriter(osw);

            String str;
            while((str=br.readLine())!=null){
                bw.write(str);
                bw.newLine();
            }
            br.close();
            bw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

package io;

public class ByteCharacterConversionStreamTest {

    public static void main(String[] args) {
        ByteCharacterConversionStream file=new ByteCharacterConversionStream();

        //写文件
        file.writeFile("D:/实验报告/swap.txt","终会有尽头");//当文件不存在时会自动创建新文件//文件内容可以写中文

        //读文件
        System.out.println("文件的内容是:");
        file.readFile("D:/实验报告/swap.txt");

        //复制文件
        file.copyFile("D:/实验报告/swap.txt", "D:/a.txt");


        System.out.println("新文件的内容是:");
        file.readFile("D:/a.txt");
    }

}

运行:

5954317f519c3d65ce7e546ab568db355494567b?

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敗北97

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值