簡體   English   中英

Java讀取文本文件

[英]Java reading text file

我對Java中的txt文件有疑問

當我必須閱讀文本文件時,我必須指出路徑。

但是,txt文件位於同一文件夾中。

需要做的是...

測試readingoption文件名。

測試:類名readoption:用於讀取文件名的選項filename:相同文件夾的文件名。

但是,我不想使用路徑來指出文件,這意味着我想在不使用代碼中使用“ 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());
                          }
                   }


    }
}

更改此行String pathes = "C:/Users/myname/Desktop/myfolder/"; 至:

 String pathes = args[1];

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

FileInputStream fstream = new FileInputStream(pathes);

如果將文本文件放入Java項目中的“ myfolder”,則路徑應如下所示:

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

FileInputStream fstream = new FileInputStream(pathes);

從.properties文件加載文件的路徑(使用java.util.Properties類),或從命令行將其作為參數傳遞(在您的main String[]參數中)。

無論哪種方式,處理文件的代碼都不能執行,而必須在外部執行。 您的處理代碼從調用它的方法接收文件路徑,因此,如果您決定使用其他方法(vg,GUI),則無需更改。

您可以使用 ”。” 為實際路徑添加系統依賴文件分隔符,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM