導航:首頁 > 文件目錄 > 讀取文件內容到string類

讀取文件內容到string類

發布時間:2024-10-10 20:46:51

java讀取text文件為String,如何實現

/**
* 主要是輸入流的使用,最常用的寫法
* @param filePath
* @return
*/
public static String read(String filePath)
{
// 讀取txt內容為字元串
StringBuffer txtContent = new StringBuffer();
// 每次讀取的byte數
byte[] b = new byte[8 * 1024];
InputStream in = null;
try
{
// 文件輸入流
in = new FileInputStream(filePath);
while (in.read(b) != -1)
{
// 字元串拼接
txtContent.append(new String(b));
}
// 關閉流
in.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return txtContent.toString();
}

閱讀全文

與讀取文件內容到string類相關的資料

熱點內容
百度網盤怎麼同步文件夾嗎 瀏覽:937
移動app為什麼沒有吉利卡 瀏覽:629
網站文字模板 瀏覽:356
單片機測電壓怎麼編程 瀏覽:880
APP怎麼認證磁條 瀏覽:48
repeaterjs選中行 瀏覽:808
法律研究工作室微信公眾號查詢 瀏覽:309
開物流網站多少錢 瀏覽:440
6s用哪個版本好 瀏覽:324
數控編程怎麼來的 瀏覽:103
廣西廣電網路的默認ip 瀏覽:191
政府發紅頭文件內容會實現嗎 瀏覽:552
騰訊貴安七星數據中心英文怎麼寫 瀏覽:489
玩火的女孩有幾個版本 瀏覽:113
tvshowtracker安卓版 瀏覽:983
App數據分析應該從哪些緯度 瀏覽:17
到底要不要升級ios92 瀏覽:875
cad2014安裝包安裝文件在哪裡 瀏覽:63
電信密碼無法登錄 瀏覽:820
cad的列印到文件怎麼設置 瀏覽:902

友情鏈接