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.对读取 ...
随机推荐
- linux bash关闭标准输出1(exec 1<&-)后重新打开
linux bash shell的再次学习. 文件描述符: stdin,stdout 和 stderr 的文件描述符分别是 0,1 和 2(一个文件描述符说白了就是文件系统为了跟踪这个打开的文件而分配 ...
- Fixing the JavaScript typeof operator
https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/javascript 类 ...
- 转: Vim快捷键分类
Vim快捷键分类 http://www.cnblogs.com/jikey/archive/2011/12/28/2304341.html 一. 移动: h,j,k,l: 左,下,上,右. ...
- HTTP协议中源端口和目标端口的问题
[提问] How is source port for HTTP determined? Is there ever collision in NAT? I know that when a HT ...
- (转)Debug Assertion Failed! Expression: _pFirstBlock == pHead
最近在VS上开发C++程序时遇到了这个错误: Debug Assertion Failed! Expression:_pFirstBlock == pHead 如图: 点击Abort之后,查看调用 ...
- [Spring boot] Read values from external properties file
Let's say we have a extral app.proporites file which contains some extra configuration: // resources ...
- [Functional Programming] Using JS, FP approach with Arrow or State Monad
Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...
- Android 原生 Android ActionBar
本文内容 关于 ActionBar 必要条件 项目结构 环境 演示一:Action Bar 显示隐藏 演示二:Action Item 显示菜单选项 演示三:Action Home 启用"返回 ...
- 查看一个目录是否已经mount --bind
执行 mountpoint -q /test/mount echo $? 如果是0表示已经mount mountpoint -q /test/mount || mount -o bind /some/ ...
- vCenter Single Sign On 5.1 best practices
http://www.virtualizationteam.com/virtualization-vmware/vsphere-virtualization-vmware/vcenter-single ...