删除所有出现的" Meredith"从文本文件

时间:2017-04-13 06:16:14

标签: java command-line io

我的程序应该从命令行获取参数。例如,如果我调用java Test1 Meredith,则从myFile.txt中删除Meredith并将更新的信息发送到名为targetFile.txt的新文本文件中。广告我不确定它是否访问myFile.txt。它是如何访问的?我的意思是我怎么知道我需要从下面的代码中访问myFile.txt?那是args [0]吗?

import java.io.*;
import java.util.*;

public class Test1 {

    public static void main(String[] args) throws Exception {
        if (args.length != 4) {
            System.out.println(
                    "Usage: java Test1 myFile.txt targetFile.txt aaa ccc");
            System.exit(1);
        }

        // Check if source file exists
        File sourceFile = new File(args[0]);
        if (!sourceFile.exists()) {
            System.out.println("Source file " + args[0] + " does not exist");
            System.exit(2);
        }

        // Check if target file exists
        File targetFile = new File(args[1]);
        if (targetFile.exists()) {
            System.out.println("Target file " + args[1] + " already exists");
            System.exit(3);
        }
        StringBuilder sb = new StringBuilder();
        try (
                // Create input and output files
                Scanner input = new Scanner(sourceFile);
                PrintWriter output = new PrintWriter(targetFile);) {
            while (input.hasNext()) {
                String s1 = input.nextLine();
                String s2 = s1.replaceAll(args[2], args[3]);
                sb.append("\r\n" + s2);
            }
        }
        try (
                PrintWriter output = new PrintWriter(sourceFile);) {
            output.printf("%s\r\n", sb.toString());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

此...

try (
        // Create input and output files
        Scanner input = new Scanner(sourceFile);
        PrintWriter output = new PrintWriter(targetFile);) {
    while (input.hasNext()) {
        String s1 = input.nextLine();
        String s2 = s1.replaceAll(args[2], args[3]);
        sb.append("\r\n" + s2);
    }
}
try (
        PrintWriter output = new PrintWriter(sourceFile);) {
    output.printf("%s\r\n", sb.toString());
}

似乎从sourceFile读取,构建新的String(通过StringBuilder),然后将其写回sourceFile ...

我建议换成更像

的东西
    try (
            // Create input and output files
            Scanner input = new Scanner(sourceFile);
            PrintWriter output = new PrintWriter(targetFile);) {
        while (input.hasNext()) {
            String s1 = input.nextLine();
            String s2 = s1.replaceAll(args[2], args[3]);
            output.printf("%s%n", s2);
        }
    }

我还建议替换......

System.out.println(
        "Usage: java Test1 myFile.txt targetFile.txt aaa ccc");

更像是......

System.out.println("Usage: Test1 {source file} {target file} {source text} {replacement text}");
System.out.println("Where:");
System.out.println("\tsource file - is the source file to be read from");
System.out.println("\ttarget file - is the target file that the results are to be written to");
System.out.println("\tsource text - is the text to be replaced");
System.out.println("\treplacement text - is the new text to take its place");

对于那些不知道该计划的目的或工作方式的用户来说,这将更有意义

  

但是我的目标文件仍然是空的。但是我不知道。所以基本没有变化

修改后的代码对我来说很好,我建议您查看错误的输出文件。尝试添加...

System.out.println("Reading from " + sourceFile.getAbsolutePath());
System.out.println("  Writing to " + targetFile.getAbsolutePath());

在读/写循环之前(或之后),这将打印出文件的绝对路径,将其与您尝试使用的文件的位置进行比较