2

因此,以下程序应将输入和输出文件作为命令行参数。我在java FileCopy input.txt output.txt命令行上输入运行程序,它应该把文件名放在args. 对此进行测试,我在 args 中没有任何值。最重要的是,方法调用fileExists()不起作用,我无法弄清楚为什么这些调用没有被执行。请注意,该getOutputFile方法不完整,由于上述错误,当前没有执行任何代码。

class FileCopy
{
public static void main(String[] args) throws IOException
{
    String infile = null;
    String outfile = null;
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));    

    if (args.length >= 2) //both files given via command line
    {
        infile = args[0];
        if (fileExists(infile) == false)
        {
            infile = getInputFile();
        }
        outfile = args[1];
    }
    else if (args.length == 1) //input file given via command line
    {
        infile = args[0];
        outfile = getOutputFile(infile);
    }
    else //no files given on command line
    {
        infile = getInputFile();
        outfile = getOutputFile(infile);
    }

    //create file objects to use
    File in = new File(infile);
    File out = new File(outfile);

    /*
     *rest of code
     */
}

//get the input file from the user if given file does not exist
public static String getInputFile() //throws IOException
{
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    String fileName = null;
    boolean haveFile = false;

    while(haveFile == false)
    {
        System.out.println("Enter a valid filename for input:");
        System.out.print(">> ");
        try
        {
            fileName = stdin.readLine();
        }
        catch (IOException e)
        {
            System.out.println("Caught exception: " + e);
        }
        haveFile = fileExists(fileName);
    }

    return fileName;    
}

//get the output file and test things
public static String getOutputFile(String infile)
{
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    File input = new File(infile);
    String filename = null;
    boolean more = true;
    while(more)
    {
        System.out.println("Enter a valid filename for output:");
        System.out.print(">> ");
        try
        {
            filename = stdin.readLine();
        }
        catch (IOException e)
        {
            System.out.println("Caught exception: " + e);
        }
        File output = new File(filename);
        if (output.exists())
        {
            more = false;
        }
        if (filename == infile)
        {
            int selection;
            String inputString = null;

            System.out.println("The output file given matches the input file. Please choose an option:");
            System.out.println("1) Enter new filename");
            System.out.println("2) Overwrite existing file");
            System.out.println("3) Backup existing file");
            System.out.print(">> ");
            try
            {
                inputString = stdin.readLine();
            }
            catch (IOException e)
            {
                System.out.println("Caught exception: " + e);
            }
            selection = Integer.valueOf(inputString);
            switch (selection)
            {
                case 1: //new filename
                case 2: //overwrite
                case 3: //backup
                default: System.exit(0);
            }
        }
    }
    return null;
}

//check the given file to see if it exists in the current working directory
public static boolean fileExists(String n)
{
    return (new File(n)).exists();
}
}
4

3 回答 3

0

课堂不应该公开吗?

public class FileCopy

不确定这是否会解决问题。

于 2012-09-16T22:28:33.473 回答
0

刚刚在 eclipse 调试器中测试了这个并且命令行参数正确放置在 args 中,您可以检查您测试的文件是否在您的项目文件夹或实际目录“。” 因为如果不是,它将提示输入新文件

 if (fileExists(infile) == false)
    {
        infile = getInputFile();
    }
于 2012-09-16T22:32:36.993 回答
0

您将需要输入一个完全限定的路径,具体取决于您的项目结构。在我的测试环境中,它默认为 IDE 中项目的根级别。通常,new File(n)调用将默认到系统相关的默认目录。我确实从没有包的单个目录中编译并运行了它,并且能够成功使用抽象文件名(即input.txtoutput.txt)。强制用户提供完整的限定文件名或至少只检查args值是否有任何危害。这将允许您在无效参数上尽早失败,而不必担心提示用户输入其他文件名。

于 2012-09-16T23:46:17.697 回答