Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件读取、更改但如何写入?_Java_Filewriter_Replaceall - Fatal编程技术网

Java 文件读取、更改但如何写入?

Java 文件读取、更改但如何写入?,java,filewriter,replaceall,Java,Filewriter,Replaceall,好的,请原谅我的初学者,请告诉我如何将我的文本从“before.txt”输出到一个名为“after”的新文件中。显然,我已经修改了文本,使其小写,并消除了非字母字符 import java.io.*; public class TextReader { public void openFile() throws IOException { try { // Read in the file BufferedReader

好的,请原谅我的初学者,请告诉我如何将我的文本从“before.txt”输出到一个名为“after”的新文件中。显然,我已经修改了文本,使其小写,并消除了非字母字符

import java.io.*;

public class TextReader {

    public void openFile() throws IOException {
        try {
            // Read in the file
            BufferedReader br = new BufferedReader(
                    new FileReader(
                    new File("before.txt")));

            String currentLine = br.readLine();

            currentLine = currentLine.toLowerCase();
            currentLine = currentLine.replaceAll("[A-Z]", "");
            br.close(); // Close br to prevent resource leak
        }
        // Exception if the file is not in the path specified
        catch (Exception e) {
            System.out.println("Error: File not found");
        }
    }

    public void writeFile() throws IOException {
        BufferedWriter output  = new BufferedWriter(new FileWriter("/WS3Ex3/after.txt"));    
        output.write("before.txt");
        output.close();
    }
}
这个怎么样

public void openFile() throws IOException {
    try {
        // Read in the file
        BufferedReader br = new BufferedReader(
                new FileReader(
                new File("before.txt")));

        String currentLine = br.readLine();

        currentLine = currentLine.toLowerCase();
        currentLine = currentLine.replaceAll("[A-Z]", "");
        br.close(); // Close br to prevent resource leak
        writeFile(currentLine);

    }
    // Exception if the file is not in the path specified
    catch (Exception e) {
        System.out.println("Error: File not found");
    }
}

public void writeFile(String text) throws IOException {
    BufferedWriter output  = new BufferedWriter(new FileWriter("/WS3Ex3/after.txt"));
    output.write(text);
    output.close();
}
}
让我猜猜,这是学校的作业吗?

试试这个:

public void ReadAndWrite() throws IOException {
try {
    // Read in the file
    BufferedWriter output  = new BufferedWriter(new FileWriter("/WS3Ex3/after.txt"));
    BufferedReader br = new BufferedReader(
            new FileReader(
            new File("before.txt")));

    String currentLine;
    while((currentLine = br.readLine()) != NULL){
        currentLine = currentLine.toLowerCase();
        currentLine = currentLine.replaceAll("[A-Z]", "");
        output.write(currentLine);
    }
    br.close(); // Close br to prevent resource leak
    output.close();
}
// Exception if the file is not in the path specified
catch (Exception e) {
    System.out.println("Error: File not found");
}
}

replaceAll(“[A-Z]”,“”)
不是您想要的,它将替换所有大写字母(因为您刚刚转换为小写,所以没有大写字母)。您需要
replaceAll(“[^a-z]”,“)
而不是谢谢,我会注意到,如果我足够聪明,能够得到一个输出文件,我会陷入困境。我添加了您建议的更改,但我只得到了“错误:找不到文件”。我甚至尝试手动创建一个“after”文件来写入,但仍然收到错误消息。束手无策。也许是我的日食什么的?我已经刷新了资源管理器,看看它是否写了一个“after”,但没有乐趣:(尝试显式地将整个文件路径(C:\file\file\before.txt)放在后面。还要打印出实际的异常(
println(e)
)以便知道确切的错误