Java read text file

Posted in :

服用下列的範例, 可以讀出文字檔的內容為字串.

File file = new File(your_file_path);
BufferedReader br;
String content = "";
try
{
    br = new BufferedReader(new FileReader(file));

    String st;
    try
    {
        while ((st = br.readLine()) != null)
        {
            System.out.println(st);
            content += st;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
catch (FileNotFoundException e)
{
    e.printStackTrace();
}
return content;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *