2014-09-02 38 views
0

我是新来的Java,我目前正在分析文件比较的Java工具,从这个环节比较两个文件:在此Java程序中何处插入文件路径?

http://www.java2s.com/Code/Java/File-Input-Output/Difftextfiledifferenceutility.htm

但在文件中任何地方,文件路径被提及。我应该在哪里插入文件路径?我搜索谷歌和检查Java Filestram和缓冲区输入输出流。但没有找到任何有用的信息。

我也搜索过stackoverflow,但似乎没有这样的问题存在。

通常,文件路径应该在主文件中更新,对吧?

但似乎在主文件中缺少。

public static void main(String argstrings[]) 
     { 
     if (argstrings.length != 2) { 
      System.err.println("Usage: diff oldfile newfile"); 
      System.exit(1); 
     } 
     Diff d = new Diff(); 
     d.doDiff(argstrings[0], argstrings[1]); 
     return; 
     } 
+0

的主要方法上面的代码使用信息。阅读并做到这一点。你可以从命令行运行它。 – Tom 2014-09-02 11:21:20

回答

1

可以肯定的方式juned告诉你,但如果你想程序更加用户友好的尝试操纵这样

public static void main(String[] args) throws ParseException { 
try{ 
    Scanner in = new Scanner(System.in); 
    System.out.println("Enter the path of old file"); 
    String oldFile = in.nextLine(); 
    System.out.println("Enter the path of new file"); 
    String newFile = in.nextLine(); 
    Diff d = new Diff(); 
    if(!oldFile.equals("") && !newFile.equals("")) { 
     d.doDiff(oldFile, newFile); 
    } 
} 
catch (Exception e){ 
    e.printStackTrace(); 
} 
} 
3

您的程序将文件名称作为参数。因此,在给出命令行输入时,您可以提供完整的文件路径。事情是这样的:

java yourClassName volume1:\dir1\filename1 volume2:\dir2\filename2 
+0

我是否需要使用命令行来运行此程序? – user3910265 2014-09-02 11:17:44

+0

@ user3910265不一定,如果您使用eclipse或其他IDE,则始终可以在运行配置中提供运行时参数 – 2014-09-02 11:20:52