首页 > 其他分享 >getPath()与getAbsolutePath()的区别

getPath()与getAbsolutePath()的区别

时间:2023-01-09 17:44:56浏览次数:34  
标签:transferTo File 区别 getAbsolutePath getPath String file desc

public static void main(String[] args) {
        File file = new File("\\data\\draw_lottery\\test1.txt");
        //取得相对路径
        System.out.println("Path: " + file.getPath());
        //取得绝对路径 window对应盘符的根路径
        System.out.println("getAbsolutePath: " + file.getAbsolutePath());
}

结果:

Path: \data\draw_lottery\test1.txt
getAbsolutePath: D:\data\draw_lottery\test1.txt

图片上传到指定路径

String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
file.transferTo(Paths.get(absPath));
public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
{
File desc = new File(uploadDir + File.separator + fileName);
if (!desc.exists()){
if (!desc.getParentFile().exists()){
desc.getParentFile().mkdirs();
}
}
return desc;
}

关于transferTo,在本地没有指定盘符的情况下会报错

void transferTo(File dest) throws IOException, IllegalStateException;

default void transferTo(Path dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));
}

 

标签:transferTo,File,区别,getAbsolutePath,getPath,String,file,desc
From: https://www.cnblogs.com/person008/p/17037745.html

相关文章