2012-09-09 68 views
2

我有一個關於在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()); 
          } 
        } 


    } 
} 
+0

爲什麼只有名稱傳遞給參數?爲什麼不通過整個路徑?文件的內容是什麼?它是應用程序資源嗎?信息是否需要用戶可編輯? –

回答

0

如果你把文本文件轉換成即「MyFolder文件」在你的Java項目中的路徑應該是這樣的:

String pathes = "/myfolder/bob.txt"; 

FileInputStream fstream = new FileInputStream(pathes); 
+0

我們可以不使用「/myfolder/bob.txt」嗎?我的意思是我只想使用bob.txt .. –

+0

呃...你可以。如果你只是在你的項目中創建一個bob.txt文件(沒有將文件放入文件夾) –

+0

我把bob.txt文件放在src文件夾中,所以jave文件和txt文件現在在同一個文件夾中。 –

0

負載從一個屬性文件的文件的路徑(使用java.util.Properties類),或將其作爲參數從命令行(在您的mainString[]參數中)傳遞。

無論哪種方式,處理文件的代碼都不能這樣做,它必須在外部完成。您的處理代碼從調用它的方法接收文件路徑,所以如果您決定使用其他方法(例如,GUI),則不需要更改。

1

改變這一行String pathes = "C:/Users/myname/Desktop/myfolder/";到:

String pathes = args[1]; 

這行FileInputStream fstream = new FileInputStream(pathes + "bob.txt");到:

FileInputStream fstream = new FileInputStream(pathes); 
+0

我們可以不使用「/myfolder/bob.txt」嗎?我的意思是我只想使用bob.txt。我把bob.txt文件放在同一個文件夾中 –

+0

@DcRedwing:呵?我告訴你要替換對'myfolder'或'bob.txt'的引用。 –

0

您可以使用 「」爲實際路徑並添加系統相關文件分隔符如:

FileInputStream fstream = new FileInputStream("."+System.getProperty("file.separator")+"bob.txt");