Java读写TXT文本
public String readTxtFile(String filePath) {
StringBuffer appInfolistInput = new StringBuffer();
try {
String encoding = "UTF8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
appInfolistInput.append(lineTxt);
}
read.close();
bufferedReader.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return appInfolistInput.toString();
}
public void readByte(String fileName) {
InputStream is = null;
try {
is = new FileInputStream(fileName);
byte[] byteBuffer = new byte[is.available()];
int read = 0;
while((read = is.read(byteBuffer)) != -1){
System.out.write(byteBuffer, 0, read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void writeBuffer(String fileName){
try {
File file = new File(fileName);
BufferedWriter output = new BufferedWriter(new FileWriter(file));
output.write("hello wrold");
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeByte(String fileName){
try {
File file = new File(fileName);
OutputStream os = new FileOutputStream(file);
String s = "hello world";
byte[] byteBuffer = s.getBytes();
os.write(byteBuffer, 0, byteBuffer.length);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Java读写TXT文本的更多相关文章
- Java读写txt文件
1.Java读取txt文件 1.1.使用FileInputStream: public static String readFile(File file, String charset){ //设置默 ...
- java操作txt文本(二):删除文本括号内的内容
想法由来:之前写读书报告时,遇到一些烦人的文献,总喜欢把注释作为括号内容放到正文中,使文章繁琐冗长,所以写了下面这个代码,剔除了括号内的内容. 适用条件:原txt文本中的括号使用正确,即左右括号匹配正 ...
- java操作txt文本(一):遇到指定字符换行
想法由来:有时查看网页源代码的css文件内容,竟是恼人的压缩后代码(不换行),如下图所示-- 它的可读性很差,所以写了下面这个简单的程序,实现自动换行. 适用条件:遇到指定字符换行(本例中遇到'}'换 ...
- java导出txt文本
页面 项目结构 html代码 <html> </head> <body> <form action="down/downLoad" met ...
- JAVA读取TXT文本中的数据
现在在Demo.txt中存在数据: ABC 需要将ABC从文本文件中读取出来 代码片: import java.io.*; class FileReaderDemo { public static v ...
- JAVA 解析TXT文本
package file; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...
- java 从txt文本中随机获取名字
代码: /* 获取随机文件文字 */ public static String random(String path) {//路径 String name = null; try { //把文本文件中 ...
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- Java HashSet对txt文本内容去重(统计小说用过的字或字数)
Java HashSet对txt文本内容去重(统计小说用过的字或字数) 基本思路: 1.字节流读需要去重的txt文本.(展示demo为当前workspace下名为utf-8.txt的文本) 2.对读取 ...
随机推荐
- Oracle中对数字加汉字的排序(完好)
之前写过一篇 Oracle中对数字加汉字的排序以及REGEXP_SUBSTR介绍 后来在开发的过程中又遇到相似问题.数据不一样了,按之前的使用方法是不适用的. 之前的是数字在前汉字在后,最基本的差别是 ...
- Flask莫名其妙特别慢
其他上网都正常,代理服务器.防火墙反复检查都没问题,最后,严重怀疑腾讯的mysql服务器,监控是这样的: 突然下降那段就很不正常. 后面忽然就崩溃了,死活都连不上.把可疑连接都关闭了,把防火墙都关闭了 ...
- how to restrict copy paste in a Textbox, in MFC?
[问题] I am developing a small application in MFC... there is a little problem..hope you guys would he ...
- MODBUS协议整理——功能码简述
1.Modbus简介——来自维基百科 Modbus是一种串行通信协议,是Modicon于1979年,为使用可编程逻辑控制器(PLC)而发表的.Modbus是工业领域通信协议的业界标准,并且现在是工业电 ...
- WinCE 下编程须要IP地址控件咋办?
默认控件栏里面没有IP Address Control. 可是 WinCE 系统内的网络属性中是有这种控件的,想知道怎么调用IP Address Control的吗? 先通过远程工具 Remote S ...
- CentOS7安装Docker与使用篇
一.在CentOS7上安装Docker篇 1. 查看系统版本: $ cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) 2. 安装 ...
- Angularjs乱记
http://www.cnblogs.com/floor/p/6652313.html 在指令中使用filter DI规则 AngularJS注入规则 数据绑定 http://www.cnblogs. ...
- http put post请求区别
1.RESTful API REST: Representational State Transfer url 对应服务器上的一种资源,e.g. 数据,图片等,所以url 中只含有名词,通过HTTP动 ...
- snabbdom vdom 框架
1.snabbdom github地址:https://github.com/snabbdom/snabbdom 2.核心方法 var vnode = h('div#container.two.cla ...
- yii框架的部署方法
yii框架(yii framework)的部署方法 刚開始学习的人来说,部署yii框架还是有一定难度的,Yii是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web ...