1019课堂内容
2015-10-20 08:41
148 查看
package text; import java.io.*; //文本文件操作类 public class file { private String fileName; public String getFileName() { return fileName; } //构造 private File file; public file(String fileName) { this.fileName = fileName; file = new File(fileName); } //创建文件 public void create() throws IOException { if(!file.exists())//如果文件不存在 { file.createNewFile(); System.out.println("文件创建成功!"); } else { System.out.println("文件已存在!"); } } //删除文件 public void delete() { File file = new File(fileName); if(file.exists())//如果文件存在 { file.delete(); System.out.println("删除成功!"); } else { System.out.println("文件不存在!"); } } //修改内容 public void edit(String strEdit) throws IOException//修改内容以参数形式传输进去 { if(file.exists()) { FileWriter fw = new FileWriter(file); BufferedWriter bfw = new BufferedWriter(fw); bfw.write(strEdit); bfw.close(); } else { System.out.println("文件不存在!"); } } //追加内容 public void addLine(String strLine) throws IOException//新加行,并传输内容 { if(file.exists()) { String str = readFile(); FileWriter fw = new FileWriter(file); BufferedWriter bfw = new BufferedWriter(fw); //bfw.newLine(); bfw.write(str+"\n"+strLine); bfw.close(); } else { System.out.println("文件不存在!"); } } public String readFile() throws IOException { String rtn = ""; if(file.exists()) { FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String s = ""; while((s = br.readLine()) != null) { rtn += s + "\n"; } br.close(); } else { System.out.println("文件不存在!"); } return rtn; } }
View Code
相关文章推荐
- 浅谈UML——九种图(一)
- 黑马程序员--Java多线程的两种实现方式:继承Thread类 & 实现Runable接口
- 10.19
- Microsoft 嵌套虚拟化技术(Nested Virtualization)
- 结构体
- 一个ZFC的新的模型并论及GCH的反面
- EL表达式
- php json中文乱码问题
- HTML:highcharts绘制折线图
- 【基础练习】【单调队列和其他】10.19.2015校内测试总结
- mysql 中 character set 与 collation 的点滴理解
- Android四大组件应用系列——实现电话拦截和电话录音
- 集算器实现记录合并
- linux下tar、zip等压缩、解压命令
- Http1.0和http1.1
- VirtualBox下安装CentOS,安装图解
- 文本注释系统 + MarkDown
- 浅谈HTTP 中get 和post方式
- leetcode-T7 Reverse digits of an integer
- sqlite3:基本操作