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

熱點內容
雜志版本號是什麼意思 瀏覽:223
地圖特效代碼 瀏覽:192
去除思科配置文件中的號 瀏覽:196
運行的16位程序太多 瀏覽:1
蘋果mac用什麼軟體好學編程 瀏覽:681
ai中線段怎麼添加寬度配置文件 瀏覽:956
lol文件怎麼找不到game 瀏覽:142
aecc視頻教程 瀏覽:983
linux怎麼查看資料庫用戶名 瀏覽:182
cefs文件系統 瀏覽:404
學平面設計個編程哪個好 瀏覽:701
如何把編程文件轉為hex 瀏覽:80
清除蘋果地圖來自地址 瀏覽:233
已經打開的文件如何清理 瀏覽:685
視頻網站有什麼用 瀏覽:70
多個表格文件怎樣壓縮文件 瀏覽:729
cad文件大很卡如何解決 瀏覽:633
將java程序打包成apk 瀏覽:277
2021唱吧文件找不到了 瀏覽:463
華為p9手機文件管理 瀏覽:284

友情鏈接