file创建文件和文件夹路径
File file = new File("路径名") 路径名的2种写法
/**
*mkdir:只能用来创建文件夹,且只能创建一级目录,如果上级不存在,就会创建失败。
mkdirs:只能用来创建文件夹,且能创建多级目录 ,如果上级不存在,就会自动创建。(创建文件夹多用此)
createNewFile:只能用来创建文件,且只能在已存在的目录下创建文件,否则会创建失败。
(FileOutputStream os=new FileOutputStream(file)也可创建文件,看情况使用)
*/
public class FileTest { public static void main(String[] args) { try { File file = new File("E:\\test\\test.txt"); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } if(!file.exists()){ file.createNewFile(); } } catch (IOException e) { // TODO e.printStackTrace(); } } }
项目的结构:相同颜色是同级的
bean的配置文件的读取和一般文件的读取有点差别的
public static void getValue(String key){ //传入"time" Properties prop = new Properties(); Properties prop2 = new Properties(); Properties prop3 = new Properties(); //要么是全路径 File file = new File("D:\\java\\content\\eclipse-win64\\S\\java\\fd.properties"); //要么是去在全路径基础上去掉项目名 File file2 = new File("java\\fd.properties"); File file3 = new File("fd2.properties"); try { //装载配置文件 prop.load(new FileInputStream(file)); prop2.load(new FileInputStream(file2)); prop3.load(new FileInputStream(file3)); } catch (IOException e) { e.printStackTrace(); } //返回获取的值 System.out.println(prop.getProperty(key) + prop2.getProperty(key)+prop3.getProperty(key)); } 8 8 9
fd.properties的内容
项目名是 S
点开bin文件夹
注意这个fd.properties文件
发现:
只有在src或者java文件夹下的java文件或资源文件才会编译,然后通过打包,会复制到commlib中
后面有2个ok
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 |
|