首页 > 技术文章 > java项目路径总结,java.io.File支持的路放方式

whalesea 2019-10-10 16:48 原文

1、直接输入路径

  已maven项目为例,直接输入路径的4种方式,即是File类支持的方式:

/**
     * FileOutpurStream以字节数组方式写入文件
     * @throws IOException
     */
    @Test
    public void testFileOutputStramBytes() throws IOException{
//        String path = "src/main/resources/write.data";
        String path = "write1.data";//在项目根目录创建
//        String localPath = "E:\\demo\\pdfCreat\\test.data";
        String localPath = "E:/demo/pdfCreat/test1.data";
        File file = new File(path);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write("你好!".getBytes("utf-8"));
        fos.close();
    }
src/main/resources/write.data  项目内路径
write1.data  项目的根目录
E:\\demo\\pdfCreat\\test.data  windows系统内路径
E:/demo/pdfCreat/test1.data  同样是windows系统内路径的另一种写法
注意:如果在路径的开头加上/,就意味着是绝地路径,是要从系统的根目录开始的,windows就是C、D、E之类,类似E:/demo/pdfCreat/test1.data;linux就是类似:/root/。


推荐阅读