读写文件及创建目录
2011-04-11 16:46
176 查看
public static void main(String[] args) { String filePath="D:/java/move2/move2.txt"; File file = new File(filePath); if(!file.getParentFile().exists()){ // 如果目录不存在 file.getParentFile().mkdirs();//创建目录 try { file.createNewFile();//再创建文件 } catch (IOException e) { e.printStackTrace(); } } writeFile(filePath); readFile(filePath); } /** * 写文件 */ public static void writeFile(String path){ try { BufferedWriter bw =new BufferedWriter(new FileWriter(path)); bw.write("hello fileWriter :"); bw.newLine(); bw.write("/t"+new Date().toString()); bw.newLine(); bw.write("add string! "); bw.write("/n"); bw.write("追加/"//n/""); bw.newLine(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 读文件 */ public static void readFile(String path){ String content=""; try { BufferedReader br = new BufferedReader(new FileReader(path)); try { content = br.readLine(); while(content!=null&&!content.equals("")){ System.out.println(content); content=br.readLine(); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { System.err.println("地址文件不存在……"+path); // e.printStackTrace(); } }
相关文章推荐
- PHP目录与文件操作技巧总结(创建,删除,遍历,读写,修改等)
- 在/home 下创建actor目录,要求该目录的拥有组为actor,该组中成员在该目录下有读写权限,组中成员在该目录下创建的文件也属于actor组
- Android与LinuxC目录创建以及文件读写操作
- PHP目录与文件操作技巧总结(创建,删除,遍历,读写,修改等)
- android用NDK在内存卡创建目录、文件并进行读写操作
- python 创建读写目录,文件
- android 创建目录/文件/读写文件
- C#文件读写追加创建目录,判断目录是否存在等操作
- 我的内核学习笔记5:proc目录文件创建及读写
- FTP文件操作之创建目录
- fstream读写创建可能不存在的文件
- QT跨目录编译及多工程集成(动态库的创建、引用及工程间相互依赖关系的pro文件编写方法)
- nodeJs读写流(读取json目录文件后,自定义写入新文件中)
- linux创建读写文件示例
- 如何解决C盘根目录无法创建或写入文件
- MFC中文件的查找、创建、打开、读写等
- VC 判断目录/文件是否存在 创建目录 求目录/文件大小
- c#ftp操作全解:创建删除目录,上传下载文件,删除移动文件,文件改名,文件目录查询
- Linux下打开目录、创建文件的权限
- rm 利用 ls -l, grep, awk 命令删除指定时间创建的文件或目录