java在指定路径下生成文件夹、文件

1.在指定路径下生成文件夹

String filePath1 = "H:\\Company\\2019.7.2报表工具\\chart-desginer\\src\\views\\_test2";
//生成文件夹
File file1=new File(filePath1);
if(!file1.exists()){	//如果_test2文件夹不存在
    file1.mkdir();		//创建文件夹
}

String filePath = "H:\\Company\\2019.7.2报表工具\\chart-desginer\\src\\views\\_test2\\module";
//生成文件夹
File file=new File(filePath);
if(!file.exists()){		//如果 module文件夹不存在
    file.mkdir();		//创建文件夹
}

2.在指定路径下生成文件、写入内容

try{
	//如果module文件夹下没有bar.txt就会创建该文件
	BufferedWriter bw = new BufferedWriter(new FileWriter("H:\\Company\\2019.7.2报表工具\\chart-desginer\\src\\views\\_test2\\module\\bar.txt"));
    bw.write("Hello bar!");	//在创建好的文件中写入"Hello bar"
    bw.close();				//关闭文件
}catch(IOException e) {
    e.printStackTrace();
}

3.把一个文件另存为

  • 引依赖
    import org.apache.commons.io.FileUtils;
//把一个文件另存为并重新命名(具体到新的文件名)
FileUtils.copyFile(new File("H:\\Company\\2019.7.2报表工具\\chart-desginer\\src\\views\\_test1\\module\\aa.vue"), new File("H:\\Company\\2019.7.2报表工具\\chart-desginer\\src\\views\\仪表板20\\module\\aa_1.vue"));

//把一个文件另存为(具体到文件夹)
FileUtils.copyFileToDirectory(new File("H:\\Company\\2019.7.2报表工具\\chart-desginer\\src\\views\\_test1\\module\\aa.vue"), new File("H:\\Company\\2019.7.2报表工具\\chart-desginer\\src\\views\\仪表板\\module"));
发布了23 篇原创文章 · 获赞 0 · 访问量 2662

猜你喜欢

转载自blog.csdn.net/xy405580364/article/details/102972521