操作Text
来源:互联网 发布:js 退出递归 编辑:程序博客网 时间:2024/06/06 02:30
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
//写入文件
public void writeToFile(File file, String context, boolean isAppend)
throws IOException {
BufferedWriter bw = null;
if (file.exists()) {
file.createNewFile();
file = new File(file.getPath());
}
FileWriter fw = new FileWriter(file, isAppend);
bw = new BufferedWriter(fw);
fw.write(context);
fw.flush();
fw.close();
}
//读取文件内容
public void readToBuffer(StringBuffer buffer, InputStream is)
throws IOException {
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("/n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
}
public String getAll() {
File file = new File(ServletActionContext.getServletContext()
.getRealPath("test.txt"));
// ---写入
try {
this.writeToFile(file, "a:a<br /> b:b<br /> c:c<br /> ", false);
this.writeToFile(file, "a:AAAAA<br /> b:BBB", true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// ---读取
InputStream is = null;
try {
is = new FileInputStream(file.getPath());
StringBuffer buffer = new StringBuffer();
this.readToBuffer(buffer, is);
response.getWriter().println(buffer.toString());
request.setAttribute("text", buffer.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return SUCCESS;
}
说明:部分内容来自网友提供
- 操作Text
- ASP.NET 操作 Text
- Flex Text 一些操作
- java 操作 text
- sublime text 快捷操作
- java text文件读写操作
- Text文件的读写操作
- Sublime Text 2快捷操作
- Sublime Text 3 操作指南
- dom操作val(),text(),html()
- Sublime Text 3操作技巧
- html5 svg文字text操作
- sublime text一些基本操作
- linux text 进入图形操作界面
- jquery text,radio,checkbox,select操作
- delphi 方便的文本文件操作(TEXT)
- jquery操作text、radio、select、checkbox
- Jquery操作服务器控件 text和val
- Qt函数之QPainter::drawImage
- Preparement方法问题
- fgets 之用法
- 晒晒专利证书,兼谈国产软件产品化之道
- malloc()与 alloc()区别
- 操作Text
- 找CALL入门写给对找call一片茫然的兄弟
- Inside Qt Series (一):Let’s go, Starting From the QObject
- Java程序员应该掌握的10项技能
- oracle 数据库备份
- 采用JAVASCRIPT实现全选的三种情况
- 急需帮助
- 内核调用
- GPG密钥修复失败 Fedora 解决办法初探