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. ICTCLAS中的HMM人名识别

    http://www.hankcs.com/nlp/segment/ictclas-the-hmm-name-recognition.html 本文主要从代码的角度分析标注过程中的细节,理论谁都能说, ...

  2. TextEdit 只能输入数字(0-9)的限制

    MaskType="RegEx" MaskUseAsDisplayFormat="True" Mask="[0-9]*" <dxe:T ...

  3. svn commit --cl app 时手动输入提交的注释,而不是在 -m 'comments here'这里输入

    原来只需要,提交的时候不指定 -m ,也不指定 -F就可以了,提交之前,svn会自动弹出编辑框来,可以修改信息. https://stackoverflow.com/questions/1746891 ...

  4. Struts2之server端验证

    声明:在我的教程中有些东西,没有提及到.不是我不知道,而是在我个人来看对你们不是太重要的知识点.你们在看课本时有了解到即可.我不会面面俱到的都给你们提及.我写博文的目的是把我这一年的开发经验通过学习s ...

  5. [JUnit] Introduce to Junit and it annotations

    Check the get started guid https://junit.org/junit5/docs/current/user-guide/#overview-getting-help p ...

  6. VC++导出具有命名空间的函数

    问题现象 原因分析 解决的方法 1 问题现象 导出具有命名空间的函数和类.源码例如以下: 头文件MiniMFC.h namespace MiniMFC { __declspec(dllexport) ...

  7. logstash启动脚本

    1  nohup ./redis-server 1>log.log 2>error.log &  2 nohup ./elasticsearch -f & 3 nohup ...

  8. Android中MVP模式与MVC模式比較(含演示样例)

    原文链接 http://sparkyuan.me/ 转载请注明出处 MVP 介绍 MVP模式(Model-View-Presenter)是MVC模式的一个衍生. 主要目的是为了解耦,使项目易于维护. ...

  9. message sent to deallocated instance

    在XCode的以前版本中,如果遇到了 [代码]c#/cpp/oc代码: 1 message sent to deallocated instance 0x6d564f0 我们可以使用info mall ...

  10. 分布式系统介绍-PNUTS

    PNUTS是Yahoo!的分布式数据库系统,支持地域上分布的大规模并发操作.它根据主键的范围区间或者其哈希值的范围区间将表拆分为表单元(Tablet),多个表单元存储在一个服务器上.一个表单元控制器根 ...