2012-09-16 26 views
2

因此,以下程序應將輸入和輸出文件作爲命令行參數。我在命令行上輸入java FileCopy input.txt output.txt來運行該程序,該程序應該將文件名放在args中。測試這個,我沒有args中的任何值。最重要的是,方法調用fileExists()不工作,我不明白爲什麼這些調用沒有被執行。請注意,getOutputFile方法不完整,由於上述錯誤,目前沒有任何代碼執行。從命令行獲取文件名並查看它們是否存在於Java中

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(); 
} 
} 
+0

我認爲你應該減少問題/代碼到更集中的東西。 – Bill

回答

0

剛剛在eclipse調試器中測試了這個,並且命令行參數正確放置在參數中,您可以檢查您測試的文件是否在您的項目文件夾或實際目錄中。因爲如果不是,它會以其他方式提示新文件。

if (fileExists(infile) == false) 
    { 
     infile = getInputFile(); 
    } 
+0

所以通過不指定目錄,它假定目錄是「。」?我認爲該目錄與.class文件位於相同的位置。我將如何去改變目錄? – zakparks31191

0

班上課不應該公開嗎?

public class FileCopy 

不確定是否能解決問題。

+0

也許?無論如何,沒有任何更改 – zakparks31191

0

您需要輸入完全限定的路徑,具體取決於您的項目結構。在我的測試環境中,它默認爲IDE中項目的根級別。通常,new File(n)調用將默認爲系統相關的默認目錄。我從一個沒有包的目錄編譯並運行了這個目錄,並且能夠成功地使用抽象文件名(即input.txtoutput.txt)。強制用戶提供全限定文件名或者至少只檢查args值是否有任何傷害。這將允許您在無效參數上提前失敗,而不必擔心提示用戶輸入其他文件名。

相關問題