导航:首页 > 文件目录 > 读取文件内容到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

友情链接