首页 > 其他分享 >IO流之创建文件

IO流之创建文件

时间:2023-01-07 19:57:45浏览次数:49  
标签:file IO 创建 流之 File new txt public String

IO流之创建文件

  • 方式1 new File(String pathname)
  • 方式2 new File(File parent,String child)//根据父目录文件+子路径构建
  • 方式3 new File(String parent,String child)//根据父目录+子路径构建
public class FileCreat {
    public static void main(String[] args) {

    }
    //方式1 new File(String pathname)
    @Test
    public void creat01() {
        String filePath = "d:\\news1.txt";
        File file = new File(filePath);
		
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式2 new File(File parent,String child)//根据父目录文件+子路径构建
    @Test
    public void creat02() {
        File parentFile = new File("d:\\");
        String fileName = "news2.txt";
        //这里的file对象,在java程序中,只是一个对象
        //只有执行了createNewFile方法,才会真正的在磁盘创建该文件
        File file = new File(parentFile,fileName);

        try {
            file.createNewFile();
            System.out.println("news2.txt创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式3 new File(String parent,String child)//根据父目录+子路径构建
    @Test
    public void creat03() {
        String parentPath = "d:\\";
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);

        try {
            file.createNewFile();
            System.out.println("news3.txt创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

标签:file,IO,创建,流之,File,new,txt,public,String
From: https://www.cnblogs.com/cyyyds/p/17033342.html

相关文章