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

熱點內容
顯卡數據怎麼設置 瀏覽:557
無法創建cad圖紙集文件 瀏覽:788
紙質文件轉換電子版 瀏覽:807
矩陣鍵盤掃描程序原理 瀏覽:986
怎麼開發高級編程 瀏覽:530
政府的拆遷紅頭文件在哪裡找 瀏覽:600
xp串口工具 瀏覽:469
反射javaforname區別 瀏覽:249
java添加計時器和圖片 瀏覽:452
shell編輯文件內容 瀏覽:614
u盤驅動在哪個文件夾里 瀏覽:938
華為手機跟蘋果手機怎麼克隆app 瀏覽:172
cad文件為什麼顯示文件名是否正確 瀏覽:174
程序配置文件能自動轉換成txt 瀏覽:985
r4燒錄卡不同版本 瀏覽:962
怎麼升級qq群為2000 瀏覽:347
微信己冊除的文件怎樣找回來 瀏覽:715
蘋果美國賬號共享2017 瀏覽:252
ps文件修改後打開還是原圖 瀏覽:220
燒卡機蘋果4微信qq哪裡下載 瀏覽:780

友情鏈接