/**
* 传入txt路径读取txt文件
*
* @param txtPath
* @return 返回读取到的内容
*/
public String readTxt(String txtPath) {
File file = new File(txtPath);
if (file.isFile() && file.exists()) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String text = null;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 使用FileOutputStream来写入txt文件
*
* @param txtPath txt文件路径
* @param content 需要写入的文本
*/
public void writeTxt(String txtPath, String content) {
FileOutputStream fileOutputStream = null;
File file = new File(txtPath);
try {
if (file.exists()) {
//判断文件是否存在,如果不存在就新建一个txt
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
java读写txt
原创
©著作权归作者所有:来自51CTO博客作者天天代码码天天的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:Oracle PL/SQL 中的 CHAR 和 VARCHAR2 比较
下一篇:MongoDB java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java 多数据源实现读写分离
多数据源 java
数据源 事务管理 User 读写分离 -
ProxySQL--读写分离(3)
ProxySQL--读写分离
MySQL proxysql -
java读写txt
新建项目 类得到结构如下:TestIo
txt读写 java System 输入流 -
Java学习——读写txt文件
Java学习
java 强制转换 数组 输入流 数据 -
perl读写txt
perl写文件,源码示例: open(FILE,">keyword添加循环): open(FILE,"keyword.txt
perl txt 读写 数组 字符串 -
java 整数 equals
/* 关于Java中的整数型 数据类型 占用空间 默认值 取值范围 -------------------------------------------------------------- byte 1 0 [-128~127] s
java 整数 equals java 字面值 强制类型转换 类型转换