您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 凉山分类信息网,免费分类信息发布

详细介绍java中的文件操作总结

2024/3/3 19:03:56发布13次查看
本篇文章主要介绍了java中的文件操作总结(干货),主要有文件读写,遍历文件夹,文件夹操作等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
file类简介
package com.file; import java.io.file; import java.io.ioexception; /** * created by elijahliu on 2017/2/10. */ public class filetest { public static void main(string[] args) { file file = new file("hello.txt"); //是否存在 if (file.exists()) { //文件 system.out.println(file.isfile()); //路径(文件夹) system.out.println(file.isdirectory()); file nameto = new file("new hello.txt"); file.renameto(nameto);//这里就是重命名文件的操作,直接新建一个file对象然后使用renameto方法可以重命名文件 } else { system.out.println("文件不存在"); try { file.createnewfile(); system.out.println("文件已被创建"); } catch (ioexception e) { system.out.println("文件无法创建"); } } if (file.exists()) { //删除文件 file.delete(); system.out.println("删除文件"); } else { } } }
文件夹操作
package com.file; import java.io.file; /** * created by elijahliu on 2017/2/11. */ public class hellofolder { public static void main(string[] args) { file folder = new file("my new folder"); if (folder.mkdir()) {//创建文件夹 判断是否成功 system.out.println("文件夹创建完成"); file newfolder = new file("myn new foleder - new"); folder.renameto(newfolder);//这里重命名了文件夹 文件夹的重命名是可以单独更改一级的文件夹名的 而这一级下面的文件夹不变 保存目录结构 if (folder.delete()) { system.out.print("done");//这里的删除只能删除空文件夹,如果文件夹中有东西,那么则不能删除,不问三七二十一直接删除一个非空文件夹是非常不负责任的 } else { system.out.println("fail"); } }else{ if (folder.exists()) { system.out.println("文件夹已经存在不用创建"); }else{ system.out.println("文件夹创建失败"); } } file folders = new file("my new folder/one/two/three/main"); folders.mkdirs();//在java中用mkdir只能创建一个,mkdirs可以创建多级目录 } }
文件属性设置
package com.file; import java.io.file; /** * created by elijahliu on 2017/2/11. */ public class setfileproperty { public static void main(string[] args){ file file = new file("test.file"); if (file.exists()){ file.setwritable(true);//可写 file.setreadable(true);//可读 file.setreadonly();//只读 } } }
遍历文件夹
public void printfiles(file dir,int tab) {//tab为不同目录结构的缩进量 if (dir.isdirectory()) { file next[] = dir.listfiles();//判断如果是目录 则返回目录所有的文件名数组用于遍历文件夹 for (int i = 0;i<next.length;i++) {//层次缩进输出 system.out.print("---"); } for(int i = 0;i<next.length;i++) {//这里用了递归获取目录结构 system.out.println(next[i].getname()); if (next[i].isfile()) { printfiles(next[i],++tab); } } } }
文件简单读写
package com.file; import java.io.*; /** * created by elijahliu on 2017/2/11. */ public class readfile { public static void main(string[] args) { file file = new file("new hello.txt"); if(file.exists()){ system.err.print("exsit"); try (fileinputstream fis = new fileinputstream(file)) {//文件输入流 这是字节流 inputstreamreader isr = new inputstreamreader(fis,"utf-8");//inputstreamreader是一个字节流,将字节流和字符流转化的时候,就需要制定一个编码方式,不然就会乱码 bufferedreader br = new bufferedreader(isr);//字符缓冲区 string line; while((line = br.readline())!=null){//这里将缓冲区里的内容如果非空就读出来打印 system.out.println(line); } br.close();//最后将各个线程关闭 isr.close(); fis.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } file newfile = new file("newtext.txt"); try { fileoutputstream fos = new fileoutputstream(newfile);//这里如果文件不存在会自动创建文件 outputstreamwriter osw = new outputstreamwriter(fos, "utf-8");//和读取一样这里是转化的是字节和字符流 bufferedwriter bw = new bufferedwriter(osw);//这里是写入缓冲区 bw.write("厉害了我的哥");//写入字符串 bw.close();//和上面一样 这里后打开的先关闭 先打开的后关闭 osw.close(); fos.close(); system.out.println("done"); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
以上就是详细介绍java中的文件操作总结的内容。
凉山分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录