简体   繁体   English

使用Java将文本从一个文件复制到另一个文件

[英]Copy text from one file to another using Java

The content of Testing.txt is not getting copied in Testing2.txt . 的内容Testing.txt是没有得到复制在Testing2.txt If I store some random data in Testing2.txt it gets erased when i run the java project instead of copying the content of Testing.txt . 如果我在Testing2.txt存储一些随机数据,则在我运行Java项目而不是复制Testing.txt的内容时,它将被擦除。

Here is the link to the tutorial I am practicing . 这是我正在练习教程的链接。 The steps are strictly followed and I have named the project, package and classes as it is given. 严格遵循这些步骤,并按给出的名称命名了项目,程序包和类。

Why is the content not getting copied? 为什么没有复制内容?

Both in reading and writing there is missing: 在阅读和写作方面都缺少:

} finally {
    writer.close();
}

As: 如:

public String readTextFile(String fileName) {
    String returnValue = "";
    FileReader file;
    String line = "";
    try {
        file = new FileReader(fileName);
        BufferedReader reader = new BufferedReader(file);
                    try {
            while ((line = reader.readLine()) != null) {
            returnValue += line + "\n";
            }
                    } finally {
                        reader.close();
                    }
    } catch (FileNotFoundException e) {
        throw new RuntimeException("File not found");
    } catch (IOException e) {
        throw new RuntimeException("IO Error occured");
    }
    return returnValue;

}

public void writeTextFile(String fileName, String s) {
    FileWriter output;
    try {
        output = new FileWriter(fileName);
        BufferedWriter writer = new BufferedWriter(output);
        writer.write(s);
                    writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

操作后关闭作家和读者,它可以正常工作。

You FileWriter object overrides the data written to your file. FileWriter对象将覆盖写入文件的数据。 Try using this constructor, which appends the data: 尝试使用此构造函数,该构造函数会附加数据:

FileWriter

public FileWriter(String fileName,
                  boolean append)
           throws IOException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Java将文件从一个文件夹复制到另一个文件夹 - Copy file from one folder to another in Java Java文件从一台服务器复制到另一台服务器 - Java file copy from one server to another Java代码复制所有文本,同时将大写字母从一个文本文件转换为另一文本文件中的小写字母 - Java code to copy all the text while converting upper case letters from one text file to lower case letters in another text file JAVA-将重复从一个文本文件复制到另一个-需要帮助删除 output 中的多个副本 - JAVA- copy duplicates from one text file to another- need help removing multiple copies in output 如何将输入数据从一个文件复制到另一个文件? 在 java - how to copy input data from one file to another one? in java 使用Java将数据从一个csv文件复制到另一个csv文件 - Copy data from one csv file to another csv file using java Java中如何将文件属性从一个文件复制到另一个文件? - How to copy file attributes from one file to another in Java? 使用Java将AWS S3文件从一个根复制到另一根 - AWS S3 copy file from one root to another using java 使用Java将.mp3文件从一个文件夹复制到另一个文件夹 - Copy .mp3 file from one folder to another folder using java 如何使用Java API将文件从一个HDFS文件夹复制到另一个HDFS文件夹? - How to copy a file from one HDFS folder to another HDFS folder using Java API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM