JavaI/O:简单的使用Reader和Writer来操作文件

Writer类与Reader类是以字符流传输数据,一个字符是两个字节

字符流除了是以字符方式(两个字节)传输数据外,另外一点与字节流不同的是字符流使用缓冲区,通过缓冲区再对文件进行操作。字节流字节对文件进行操作。使用字符流类时关闭字符流会强制将字符流缓冲区的类容输出,如果不想关闭也将字符流进行输出,使用Writer类的flush()方法。

Reader读取数据:

try {
            Reader reader = new FileReader(
                    "D:\\cmz\\java\\JavaOOP6.0\\pet.template");
            char ch[] = new char[45];
            StringBuffer buffer = new StringBuffer();
            try {
                int length = reader.read(ch);
                while (length != -1) {
                    buffer.append(ch);
                    length = reader.read();
                }
                String str = " 您好!我的名字是{name},我是一只{type}.我的主人是{master}";
                System.out.println("替换前:" + str);
                str = str.replace("{name}", "欧欧");
                str = str.replace("{type}", "狗狗");
                str = str.replace("{master}", "程沐喆");
                System.out.println("替换后:" + str);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

使用writer操作数据:

String str = "您好!我的名字是欧欧,我是一只狗狗.我的主人是李伟";
        try {
            FileWriter file = new FileWriter("D:\\cmz\\java\\JavaOOP6.0\\pet.template");
            file.write(str);
            if (null!=file) {
                file.close();
                System.out.println("写入成功");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

转载于:https://blog.51cto.com/13758648/2301444

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中使用 I/O 流复制文件可以使用如下的代码: ``` import java.io.*; public class CopyFile { public static void main(String[] args) { // 要复制的文件的路径 String srcFile = "/path/to/src/file.txt"; // 复制后的文件的路径 String destFile = "/path/to/dest/file.txt"; try (InputStream in = new FileInputStream(srcFile); OutputStream out = new FileOutputStream(destFile)) { // 缓冲区大小 byte[] buffer = new byte[1024]; int length; // 循环读取文件内容并写入到新文件中 while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } } } ``` 上面的代码使用了 try-with-resources 语句,这样在结束后会自动关闭流。如果不使用 try-with-resources 语句,需要在 finally 块中手动关闭流。 需要注意的是,上面的代码只能复制纯文本文件。如果要复制二进制文件,需要使用字节流(InputStream 和 OutputStream)而不是字符流(ReaderWriter)。 ### 回答2: 文件复制通过使用Java中的I/O流可以轻松地实现。下面是一个简单的示例,演示了如何使用I/O流来实现文件复制。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopy { public static void main(String[] args) { String sourceFilePath = "source.txt"; // 源文件路径 String targetFilePath = "target.txt"; // 目标文件路径 try { // 创建输入流和输出流 FileInputStream input = new FileInputStream(sourceFilePath); FileOutputStream output = new FileOutputStream(targetFilePath); // 创建一个缓冲区,用于存储读取/写入的数据 byte[] buffer = new byte[1024]; int bytesRead; // 读取源文件中的数据,并将其写入目标文件中 while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } // 关闭流 input.close(); output.close(); System.out.println("文件复制完成!"); } catch (IOException e) { System.out.println("发生错误:" + e.getMessage()); } } } ``` 上述代码中,我们首先指定源文件和目标文件的路径。然后,我们使用`FileInputStream`和`FileOutputStream`类创建输入流和输出流。我们还创建了一个缓冲区,用于存储读取和写入的数据。 在`while`循环中,我们使用`read()`方法从输入流读取数据,并使用`write()`方法将读取的数据写入输出流。我们还使用`bytesRead`变量来记录实际读取的字节数,以便正确写入到输出流中。 最后,我们在`IOException`中捕获任何可能发生的错误,并将错误消息输出到控制台。如果没有错误发生,则输出"文件复制完成!"的消息。 通过运行上述代码,我们可以将一个文件的内容复制到另一个文件中。请确保源文件存在,并且目标文件不存在,以避免发生意外的覆盖。 ### 回答3: Java使用I/O流完成文件复制非常简单。我们可以使用字节流或字符流来实现文件复制。 使用字节流实现文件复制时,首先需要创建一个文件输入流和一个文件输出流。然后,通过循环从文件输入流中读取字节,并将其写入文件输出流中,实现文件复制。最后记得关闭输入流和输出流。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileInputStream inputStream = new FileInputStream(inputFilePath); FileOutputStream outputStream = new FileOutputStream(outputFilePath); // 定义一个字节数组来存储读取的字节数据 byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // 关闭输入流和输出流 inputStream.close(); outputStream.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 使用字符流实现文件复制时,操作步骤与字节流类似。只是在创建流时需要使用ReaderWriter类,同时要保证源文件和目标文件的编码类型一致。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileReader reader = new FileReader(inputFilePath); FileWriter writer = new FileWriter(outputFilePath); // 定义一个字符数组来存储读取的字符数据 char[] buffer = new char[1024]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } // 关闭输入流和输出流 reader.close(); writer.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 无论使用字节流还是字符流,Java中的I/O流都提供了方便的方法来实现文件复制。通过以上的代码示例,我们可以轻松地完成文件复制操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值