import java.io.*;
import java.util.StringTokenizer;
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();
}
else //no files given on command line
{
infile = getInputFile();
outfile = getOutputFile();
}
//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;
do
{
System.out.println("Enter a valid filename:");
System.out.print(">> ");
fileName = stdin.readLine();
haveFile = fileExists(fileName);
}while(haveFile == false);
return fileName;
}
//get the output file and test things
public static String getOutputFile()
{
return null;
}
//check the given file to see if it exists in the current working directory
public static boolean fileExists(String n)
{
boolean exist = false;
if (n.length() != 0)
{
File tmp = new File(n);
exist = tmp.exists();
}
return exist;
}
}
我在第 53 行调用 fileExists 时遇到问题。据我所知,语法是正确的,但它只是无限期地停留在 do 循环中。在 Jcreator 中单步执行该程序并没有找到任何解决方案,尽管有趣的是,该程序直接跳过了第 53 行而没有跳转到 fileExists。有什么想法有什么问题吗?