当前位置: 移动技术网 > IT编程>开发语言>Java > 将内容写到txt文档里面并读取及删除的方法

将内容写到txt文档里面并读取及删除的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

鬼屋大院,北极星电力招聘,敌后之战

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('清空文件的内容失败,因为没有发送邮件日志文件!');";
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网