日期:2014-05-17 浏览次数:21001 次
package com.feijoy.wap.tools;
import java.io.*;
public class Txt {
public static BufferedReader bufread;
//指定文件路径和名称
private static String path = "F:/AllUserInfo.txt";
private static File filename = new File(path);
private static String readStr ="";
/**
* 创建文本文件.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
}
}
/**
* 删除文本文件.
* @throws IOException
*
*/
public static void deleteTxtFile() throws IOException{
if (filename.exists()) {
filename.delete();
}
}
/**
* 读取文本文件.
*
*/
public static String readTxtFile(){
String read;
FileReader fileread;
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "\r\n";
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return readStr;
}
public static String readTxtInfo(){
String read;
FileReader fileread;
File filename = new File("F:/info.txt");
try {
fileread = new FileReader(filename);
bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "\r\n";
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return readStr;
}
/**
* 写文件.
*
*/
public static void writeTxtFile(String newStr) throws IOException{
//先读取原有文件内容,然后进行写入操作
String filein = newStr + "\r\n";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
mm.writeBytes(filein);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
/**
* main方法测试
* @param s
* @throws IOException
*/
public static void main(String[] s) throws IOException {
Txt.creatTxtFile();
String rstr = Txt.readTxtFile();
String str = rstr + "aaa,456";
Txt.writeTxtFile(str);
}
}
package com.feijoy.wap.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.Http