我有一個關於在JavaJava的閱讀文本文件
txt文件的問題。當我要讀的文本文件,我要指出的路徑。
但是,txt文件位於相同的文件夾中。
需要做的是...
testing readingoption filename。
測試:類名稱 readoption:讀取文件的選項 文件名:文件名稱相同的文件夾。
但是,我不想使用路徑指出文件,這意味着我想在不使用代碼中的「C:/ Users/myname/Desktop/myfolder /」的情況下閱讀文本文件。
有人知道該怎麼做嗎?
感謝。
public class testing{
private static boolean debug = true;
public static void main(String args [])
{
if(args.length == 0)
{// if you do nothing
if(debug == true)
{
System.out.println(args);
}
System.err.println("Error: Missing Keywords");
return;
}
else if(args.length == 1)
{// if you miss key word
if(debug == true)
{
System.out.println(args);
}
System.err.println("Error: Missing filename");
return;
}
else
{// if it is fine
String pathes = "C:/Users/myname/Desktop/myfolder/";// dont want to use this part
if(debug == true)
{
System.out.println("Everthing is fine");
System.out.println("Keyword :" + args[0]);
System.out.println("File name :" + args[1]);
}
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(pathes + "bob.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
}
爲什麼只有名稱傳遞給參數?爲什麼不通過整個路徑?文件的內容是什麼?它是應用程序資源嗎?信息是否需要用戶可編輯? –