第03章_面向对象_第06章_常用类__File类

import java.io.*;

File类在IO包下


                String s1 ="mir2/mir3";  //路径
String s2 ="myfile.txt";    // 文件名

File f = new File (s1,s2);  // new一个新的File对象 参数为s1 路径 , s2 文件名


if (f.exists()){                                // 测试 f 对象中的路径及文件是否存在,如存在执行以下。

                                                    //如不存在 执行else

System.out.println("文件名"+f.getAbsolutePath());
System.out.println("文件大小"+f.length());
}else{

f.getParentFile().mkdirs();    //  f.getParentFile() 拿到f对象中的 路径名称 并返回一个file对象。

                                                             // 接着用返回的file对象 进行.mkdirs()  建立file对象中的一系列父子路径文件夹 



try{    
f.createNewFile();           // 使用f.createNewFile方法创建该方法所指定路径的文件。 f指定的路径必须存                                                                       在,如不存在将报错,而且该语句必须使用try catch
}catch (IOException e){
e.printStackTrace();

}


exisis 方法


getParent 方法


mkdirs 方法


creatNewFile 方法




猜你喜欢

转载自blog.csdn.net/e2603199/article/details/80245236