java开发之读写txt文件操作的实现
项目结构:
运行效果:
========================================================
下面是代码部分:
========================================================
/text/src/com/b510/txt/myfile.java
package com.b510.txt;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.filereader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
/**
* @author hongten
*
* @time 2011-12-12 2011
*/
public class myfile {
@suppresswarnings("static-access")
public static void main(string[] args) {
myfile myfile = new myfile();
try {
for (int i = 0; i < 5; i++) {
myfile.creattxtfile("test");
myfile.writetxtfile("显示的是追加的信息" + i);
string str = myfile.readdate();
system.out.println("*********\n" + str);
}
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
private static string path = "txt/";
private static string filenametemp;
/**
* 创建文件
*
* @throws ioexception
*/
public static boolean creattxtfile(string name) throws ioexception {
boolean flag = false;
filenametemp = path + name + ".txt";
file filename = new file(filenametemp);
if (!filename.exists()) {
filename.createnewfile();
flag = true;
}
return flag;
}
/**
* 写文件
*
* @param newstr
* 新内容
* @throws ioexception
*/
public static boolean writetxtfile(string newstr) throws ioexception {
// 先读取原有文件内容,然后进行写入操作
boolean flag = false;
string filein = newstr + "\r\n";
string temp = "";
fileinputstream fis = null;
inputstreamreader isr = null;
bufferedreader br = null;
fileoutputstream fos = null;
printwriter pw = null;
try {
// 文件路径
file file = new file(filenametemp);
// 将文件读入输入流
fis = new fileinputstream(file);
isr = new inputstreamreader(fis);
br = new bufferedreader(isr);
stringbuffer buf = new stringbuffer();
// 保存该文件原有的内容
for (int j = 1; (temp = br.readline()) != null; j++) {
buf = buf.append(temp);
// system.getproperty("line.separator")
// 行与行之间的分隔符 相当于“\n”
buf = buf.append(system.getproperty("line.separator"));
}
buf.append(filein);
fos = new fileoutputstream(file);
pw = new printwriter(fos);
pw.write(buf.tostring().tochararray());
pw.flush();
flag = true;
} catch (ioexception e1) {
// todo 自动生成 catch 块
throw e1;
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return flag;
}
/**
* 读取数据
*/
public void readdata1() {
try {
filereader read = new filereader(filenametemp);
bufferedreader br = new bufferedreader(read);
string row;
while ((row = br.readline()) != null) {
system.out.println(row);
}
} catch (filenotfoundexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}
public string readdate() {
// 定义一个待返回的空字符串
string strs = "";
try {
filereader read = new filereader(new file(filenametemp));
stringbuffer sb = new stringbuffer();
char ch[] = new char[1024];
int d = read.read(ch);
while (d != -1) {
string str = new string(ch, 0, d);
sb.append(str);
d = read.read(ch);
}
system.out.print(sb.tostring());
string a = sb.tostring().replaceall("@@@@@", ",");
strs = a.substring(0, a.length() - 1);
} catch (filenotfoundexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return strs;
}
}