将内容写到txt文档里面并读取及删除的方法
程序员文章站
2024-02-21 13:40:22
1、将内容写到txt文档里面 复制代码 代码如下: public static void writefile() { string txtfilename = "email...
1、将内容写到txt文档里面
public static void writefile() {
string txtfilename = "emailrecord.txt";
string directorypath = "";
try {
directorypath = webpluscontext.getrealpath("/apps/schoolfellow/upload/smsrecord");
file directory = new file(directorypath);
if (!directory.exists()) {
directory.mkdirs();
}
file txtfile = new file(directorypath, txtfilename);
fileoutputstream out = new fileoutputstream(txtfile, true);
string line = system.getproperty("line.separator");
string smscontent = "将内容写到txt文件里面!" + line;
out.write(smscontent.tostring().getbytes("gbk"));
out.close();
} catch (exception ex) {
log.error("将结果写入文件失败!", ex);
}
}
2、读取文件里面的内容
public void readerfile() {
string filepath = webpluscontext.getservletcontext().getrealpath("/apps/schoolfellow/upload/emailrecord/emailrecord.txt");
fileinputstream fis = null;
try {
fis = new fileinputstream(filepath);
inputstreamreader reader = new inputstreamreader(fis, "gbk");
bufferedreader br = new bufferedreader(reader);
string info = "";
schoolfellows = new arraylist<schoolfellowdataviewwrap>();
while ((info = br.readline()) != null) {
system.out.println(info);
}
br.close();
fis.close();
} catch (exception ex) {
log.error("读取数据失败", ex);
} finally {
}
}
3、清除txt文件里面的内容
public void clearfilecontent() {
string filepath = webpluscontext.getservletcontext().getrealpath("/apps/schoolfellow/upload/emailrecord/emailrecord.txt");
try {
fileoutputstream out = new fileoutputstream(filepath,false);
out.write(new string("").getbytes());
out.close();
script = "alert('清空发送邮件日志成功!');";
} catch (exception ex) {
script = "alert('清空文件的内容失败,因为没有发送邮件日志文件!');";
}
}
复制代码 代码如下:
public static void writefile() {
string txtfilename = "emailrecord.txt";
string directorypath = "";
try {
directorypath = webpluscontext.getrealpath("/apps/schoolfellow/upload/smsrecord");
file directory = new file(directorypath);
if (!directory.exists()) {
directory.mkdirs();
}
file txtfile = new file(directorypath, txtfilename);
fileoutputstream out = new fileoutputstream(txtfile, true);
string line = system.getproperty("line.separator");
string smscontent = "将内容写到txt文件里面!" + line;
out.write(smscontent.tostring().getbytes("gbk"));
out.close();
} catch (exception ex) {
log.error("将结果写入文件失败!", ex);
}
}
2、读取文件里面的内容
复制代码 代码如下:
public void readerfile() {
string filepath = webpluscontext.getservletcontext().getrealpath("/apps/schoolfellow/upload/emailrecord/emailrecord.txt");
fileinputstream fis = null;
try {
fis = new fileinputstream(filepath);
inputstreamreader reader = new inputstreamreader(fis, "gbk");
bufferedreader br = new bufferedreader(reader);
string info = "";
schoolfellows = new arraylist<schoolfellowdataviewwrap>();
while ((info = br.readline()) != null) {
system.out.println(info);
}
br.close();
fis.close();
} catch (exception ex) {
log.error("读取数据失败", ex);
} finally {
}
}
3、清除txt文件里面的内容
复制代码 代码如下:
public void clearfilecontent() {
string filepath = webpluscontext.getservletcontext().getrealpath("/apps/schoolfellow/upload/emailrecord/emailrecord.txt");
try {
fileoutputstream out = new fileoutputstream(filepath,false);
out.write(new string("").getbytes());
out.close();
script = "alert('清空发送邮件日志成功!');";
} catch (exception ex) {
script = "alert('清空文件的内容失败,因为没有发送邮件日志文件!');";
}
}