![](/img/trans.png)
[英]How do I read in comma separated string from text file that is inputted from command-line as argument?
[英]How do I read in a text file from the command line
我創建了一個掃描器來從命令行中的第一個元素讀入文本文件,但它導致FileNotFoundException
。 如何將其格式化為接受此文件的位置?
這是我的代碼:
Scanner scanner = new Scanner(new File(args[0]));
例如,如果文件名為Hello.txt
,我將Hello.txt
作為命令行中的第一個元素。
為我工作。 “build.xml”是我碰巧知道存在於我的項目文件夾中的文件。 嘗試打印文件的“絕對路徑”,它會顯示 Java 正在查找的位置,這可能與您期望的不同。
public class ScratchPad {
public static void main( String[] args ) throws Exception {
System.out.println( new File("build.xml").exists() );
System.out.println( new File("build.xml").getAbsoluteFile() );
}
}
輸出:
run:
true
C:\Users\Brenden\Google Drive\proj\tempj8\build.xml
BUILD SUCCESSFUL (total time: 0 seconds)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.