FileDemo.java package 极客学院练习; import java.io.File; import java.io.IOException; public class FileDemo { public static void main(String[] args) { File file = new File("hello.txt"); File newname = new File("abc.txt"); file.renameTo(newname); // 文件重命名 System.out.println(file.exists()); // 测试文件是否存在 System.out.println(file.isFile()); // 判断是是否文件 System.out.println(newname.exists()); System.out.println(newname.isFile()); try { file.createNewFile(); // 创建文件,需要异常处理 if (file.exists()) System.out.println("成功创建" + file.getName()); } catch (IOException e) { e.printStackTrace(); } } } FolderDemo .java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package 极客学院练习; import java.io.File; import java.io.IOException; public class FolderDemo { public static void main(String[] args) { // File folder = new File("Demo/ad/ada/ad"); // if(folder.mkdirs()) // 创建文件夹,return boolean // System.out.println(folder + "文件夹创建完成."); // else // { // if(folder.exists()) // System.out.println("文件夹创建成功."); // else // System.out.println("文件夹创建失败."); // } // File folder1 = new File("Demo/ad/ada"); //文件夹的删除. // if(folder1.delete()){ // System.out.println("done"); // } // else // System.out.println("faild"); // // } // File file=new File("1/2/3/4/5"); File newfile=new File("1/2/3/4/5/6"); file.mkdirs(); newfile.mkdirs(); System.out.println(file.renameTo(newfile)); //文件夹的重命名. if(file.renameTo(newfile)) System.out.println("done"); else System.out.println("faild"); } } ReadFileDemo.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package 极客学院练习; import java.io.File; public class ReadFileDemo { public static void main(String[] args) { File file=new File("wang.txt"); System.out.println(file.exists()); System.out.println(file.getName()); System.out.println(file.getAbsolutePath()); System.out.println(file.getParent()); System.out.println(file.getPath()); System.out.println(file.length()); System.out.println(file.isHidden()); System.out.println(file.canRead()); System.out.println(file.canWrite()); } } 转载请注明来自Bcoder网站所有,本文地址:http://www.bcoder.cn/?p=700 除非注明,Bcoder文章均为原创,转载请注明出处和链接!
java本地新建文件_Java本地文件操作-新建、删除、重命名、属性
最新推荐文章于 2023-03-05 16:15:48 发布