導航:首頁 > 文件目錄 > 讀取文件內容到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類相關的資料

熱點內容
讀取隱藏分區文件 瀏覽:64
上古卷軸5修改工具 瀏覽:745
javascript輸出 瀏覽:392
找不到文件描述設備管理 瀏覽:471
怎樣安裝路由器並設置密碼 瀏覽:74
解壓文件路徑不存在怎麼辦 瀏覽:430
wps手機文件不顯示圖片 瀏覽:447
蘋果手機老是自動打字 瀏覽:273
手機拍證件照的app叫什麼 瀏覽:920
linux暫停進程 瀏覽:888
如何看上交所數據 瀏覽:329
數控cnc銑床操作教程 瀏覽:987
質檢倉管數據員cnc哪個好 瀏覽:60
讀取文件內容到string類 瀏覽:703
win10如何取消回收站 瀏覽:540
得力牌驗鈔機怎麼升級 瀏覽:336
探月編程貓怎麼向左走 瀏覽:836
禁止win10自動安裝驅動 瀏覽:78
郵箱網盤一次能發多少文件 瀏覽:946
inword 瀏覽:756

友情鏈接