导航:首页 > 文件目录 > 读取文件内容到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类相关的资料

热点内容
大数据专业哪个最好 浏览:467
一个文件内容替换另一个文件 浏览:288
ios8最好的版本 浏览:400
录屏决定文件大小的是什么 浏览:322
数据用不了是哪里坏掉了 浏览:310
百度网盘文件传输格式 浏览:455
苹果系统txt文件 浏览:629
家装网站怎么设计 浏览:202
adc指令微程序 浏览:487
名片模板word 浏览:635
怎么把文件夹上锁 浏览:331
我不会英语怎么编程 浏览:895
c盘和文件大小 浏览:63
maya工具架怎么调出来 浏览:191
文件系统坏了怎么办 浏览:261
小公司网络布线 浏览:515
怎么用二维码编程 浏览:341
f9b文件能用手机看吗 浏览:364
手机暴风视频文件夹 浏览:394
vsftpdlinux下载文件 浏览:948

友情链接