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文本的更多相关文章

  1. Java读写txt文件

    1.Java读取txt文件 1.1.使用FileInputStream: public static String readFile(File file, String charset){ //设置默 ...

  2. java操作txt文本(二):删除文本括号内的内容

    想法由来:之前写读书报告时,遇到一些烦人的文献,总喜欢把注释作为括号内容放到正文中,使文章繁琐冗长,所以写了下面这个代码,剔除了括号内的内容. 适用条件:原txt文本中的括号使用正确,即左右括号匹配正 ...

  3. java操作txt文本(一):遇到指定字符换行

    想法由来:有时查看网页源代码的css文件内容,竟是恼人的压缩后代码(不换行),如下图所示-- 它的可读性很差,所以写了下面这个简单的程序,实现自动换行. 适用条件:遇到指定字符换行(本例中遇到'}'换 ...

  4. java导出txt文本

    页面 项目结构 html代码 <html> </head> <body> <form action="down/downLoad" met ...

  5. JAVA读取TXT文本中的数据

    现在在Demo.txt中存在数据: ABC 需要将ABC从文本文件中读取出来 代码片: import java.io.*; class FileReaderDemo { public static v ...

  6. JAVA 解析TXT文本

    package file; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...

  7. java 从txt文本中随机获取名字

    代码: /* 获取随机文件文字 */ public static String random(String path) {//路径 String name = null; try { //把文本文件中 ...

  8. java指定编码的按行读写txt文件(几种读写方式的比较)

    转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976  版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  9. Java HashSet对txt文本内容去重(统计小说用过的字或字数)

    Java HashSet对txt文本内容去重(统计小说用过的字或字数) 基本思路: 1.字节流读需要去重的txt文本.(展示demo为当前workspace下名为utf-8.txt的文本) 2.对读取 ...

随机推荐

  1. mysql CAPI 接口 读取中文乱码的解决方案(转)

      最近的yymysqlsdk的开源项目里,对中文的支持不到位,因此用了1.5天的时间,对中文处理的各个情况进行了分析.     1.首先确认你的MySQL配置文件,my.ini (只针对window ...

  2. RedisTemplate 分页

    利用spring redis的RedisTemplate进行分页: 场景: 现有项目若干,根据项目的创建时间(createTime)进行降序读取: 存储结构: key:proList(list) 存放 ...

  3. matlab之simulink仿真入门

    Matlab Simulink仿真工具的应用 ****Simulink是一个用来对动态系统进行建模.仿真和分析的软件包.使用Simulink来建模.分析和仿真各种动态系统(包含连续系统.离散系统和混合 ...

  4. TCP/IP, UDP, ICMP, ARP协议族简介--纯图慎点

    ISO/OSI的网络模型架构 TCP/IP参考模型的层次结果 以太网头部结构 以太网属于数据链路层, 属于最基本的协议结构 IP协议 IP协议为TCP, UDP, ICMP提供最基本的数据传输通路 I ...

  5. Swift语言精要 - 浅谈结构体(Struct)

    CGRect, CGSize, CGPoint这些是 . String, Int, Array, Dictionary这些我们经常用的也是结构体(Struct). 那么结构体(Struct)到底是什么 ...

  6. ashx 一般处理程序中使用 Session

    项目中,调用 ashx 一般处理程序获取行政区划Json数据,在 ashx 里面有用到Session,但是总无法获取 Session . 查阅资料得知 ashx 一般处理程序要使用 Session,必 ...

  7. JS实现拖动(2)

    getBoundingClientRect() 来获取页面元素的位置 document.documentElement.getBoundingClientRect 该方法返回一个对象,从而获得页面中某 ...

  8. 创建一个入门的JAVA WEB站点(REST JERSEY)

    最近一直在看TOMCAT,想要自己创建一个小WEB站点,有不想要部署在其他的容器内这是一个不错的学习对象. 一.选择合适的模版 mvn archetype:generate -DarchetypeCa ...

  9. Android View的事件分发机制

    准备了一阵子,一直想写一篇事件分发的文章总结一下.这个知识点实在是太重要了. 一个应用的布局是丰富的,有TextView,ImageView,Button等.这些子View的外层还有ViewGroup ...

  10. string int 类型转换

    string  int  类型转换 (int) 此方法不适用于将string 转换为int 只能转换数值类型为int 而不能转换引用类型 不会四舍五入 直接去掉小数位 Conver.ToInt() 会 ...