import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class TestFileIO { public static void main(String[] args) throws IOException { // TODO 自动生成的方法存根 String s = "这是java的file类测试"; File f = new File("D:/test/test1/234.txt"); /*如果路径不存在则创建*/ if(!f.exists()) { /*getParentFile返回此抽象路径名的父路径名的抽象路径名,如果直接使用f.mkdirs则会将234.txt也创建成目录*/ f.getParentFile().mkdirs(); f.createNewFile(); } FileOutputStream fos = new FileOutputStream(f); byte [] b = s.getBytes(); fos.write(b); fos.close(); System.out.println(f.getParentFile()); } }
java创建多层路径和文件并写入内容
最新推荐文章于 2023-12-12 17:35:19 发布