Java文件操作(二)

2014-11-24 02:33:30 · 作者: · 浏览: 1
); while (sc.hasnextline()){ sbuffer.append(sc.nextline()); } //append if true, then bytes will be written to the end of the file rather than the beginning filewriter fw = new filewriter(file,true); bufferedwriter bw = new bufferedwriter(fw); bw.append(sbuffer.tostring()); bw.write("i'm xiva,我是xiva"); bw.newline(); bw.write("i'm njit.shot_go,我是shot_go");// string message = "i'm njit.shot_go,我是shot_go";// for(char mc:message.tochararray()){// bw.append(mc);// } bw.flush(); bw.close(); fw.close(); } /** * * @description fileoutputstream写文件 * @author xiva * @throws ioexception */ @test public void writefosfile() throws ioexception{ file file = new file("src/test.txt"); if(!file.exists()){ file.createnewfile(); } //append if true, then bytes will be written to the end of the file rather than the beginning fileoutputstream fos = new fileoutputstream(file,true); bufferedoutputstream bos = new bufferedoutputstream(fos); for (int i=0; i<100000; i++){ string message = "i'm njit.shot_go,我是shot_go"; bos.write(message.getbytes()); bos.flush(); bos.write("\r\n".getbytes()); } bos.close(); fos.close(); } /** * * @description 文件列表 * @author xiva * @param rootfile */ public void listdictionary(file rootfile){ file[] files = rootfile.listfiles(); for (file file:files){ if(file.isdirectory()){ system.out.println("add to tree node"); listdictionary(file); }else{ system.out.println(file.getpath()); } } } @test public void testlistdictionary(){ file rootfile = new file("e:"+ file.separatorchar +"liber"); listdictionary(rootfile); } }

而对文件的操作,总结为:

1、删除文件或文件夹

2、创建文件或文件夹

3、读取文本或者二进制文件

4、写文本或者二进制文件(对于写二进制文件,我认为在图像处理以及音频视频处理中会使用到)

5、重命名文件,遍历文件,移动文件等等