IO流(拷贝文本文件)

//将C盘一个文本文件复制到D盘


/*

复制的原理:

其实就是将C盘下的文件数据存储到D盘的一个文件中


步骤:

1,在D盘创建一个文件。用于存储C盘文件中的数据

2,定义读取流和C盘文件关联

3,通过不断的读写完成数据存储

4,关闭资源。

*/

import java.io.*;
class CopyTest
{
    public static void main(String[] args) throws IOException
    {
        copy_1();
    }
     
    public static void copy_2()
    {
        FileWriter fw = null;
        FileReader fr = null;
         
        try
        {
            fw = new FileWriter("SystemDemo_copy.txt");
            fr = new FileReader("SystemDemo.txt");
             
            char[] buf = new char[1024];
             
            int len = 0;
            while((len=fr.read(buf))!=-1)
            {
                fw.write(buf,0,len);
            }
        }
        catch(IOException e)
        {
            throw new RuntimeException("读写失败");
        }
        finally
        {
            if(fr!=null)
                try
                {
                    fr.close();
                }
                catch(IOException e)
                {
                 
                }
            if(fw!=null)
                try
                {
                    fw.close();
                }
                catch(IOException e)
                {
                 
                }
                finally
                {
                    if(fw!=null)
                }
        }
    }
     
    //从C盘读一个字符,就往D盘写一个字符。
    public static void copy_1() throws IOException
    {
        //创建目的地。
        FileWriter fw = new FileWriter("RuntimeDemo_copy.txt");
         
        //与已有文件关联
        FileReader fr = new FileReader("RuntimeDemo.java");
         
        int ch = 0;
         
        while((ch=fr.read())!=-1)
        {
            fw.write(ch);
        }
         
        fw.close();
        fr.close();
    }
}

使用字节流拷贝文本文件: 1. 创建一个 FileInputStream 对象,用于读取源文件。 2. 创建一个 FileOutputStream 对象,用于写入目标文件。 3. 定义一个 byte 数组,用于存储读取的数据。 4. 循环读取源文件中的数据,将读取到的数据存储到 byte 数组中。 5. 将 byte 数组中的数据写入目标文件中。 6. 关闭 FileInputStream 和 FileOutputStream 对象。 示例代码: ``` import java.io.*; public class ByteStreamCopy { public static void main(String[] args) { String srcFilePath = "source.txt"; String destFilePath = "target.txt"; try (FileInputStream fis = new FileInputStream(srcFilePath); FileOutputStream fos = new FileOutputStream(destFilePath)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } } } ``` 使用字符流拷贝文本文件: 1. 创建一个 FileReader 对象,用于读取源文件。 2. 创建一个 FileWriter 对象,用于写入目标文件。 3. 定义一个 char 数组,用于存储读取的数据。 4. 循环读取源文件中的数据,将读取到的数据存储到 char 数组中。 5. 将 char 数组中的数据写入目标文件中。 6. 关闭 FileReader 和 FileWriter 对象。 示例代码: ``` import java.io.*; public class CharacterStreamCopy { public static void main(String[] args) { String srcFilePath = "source.txt"; String destFilePath = "target.txt"; try (FileReader fr = new FileReader(srcFilePath); FileWriter fw = new FileWriter(destFilePath)) { char[] buffer = new char[1024]; int length; while ((length = fr.read(buffer)) > 0) { fw.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值