为什么我的相对路径和绝对路径是一样的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.imooc.flie; import java.io.File; import java.io.IOException; public class FileDemo { public static void main(String[] args) { boolean flag = false ; File fileone = new File( "c:\\File\\DDD\\emem" ); File filetwo = new File(fileone, "Monday.docx" ); if (!fileone.exists()) { fileone.mkdir(); } if (!filetwo.exists()) { try { filetwo.createNewFile(); System.out.println( "创建文件成功" ); System.out.println( "文件名称:" + filetwo.getName()); System.out.println(filetwo.getParent()); System.out.println(filetwo.getAbsolutePath()); System.out.println(filetwo.getPath()); System.out.println( "文件上一级目录:" + filetwo.getParentFile().getName()); System.out.print( "文件/目录:" ); if (filetwo.isFile()) { System.out.println( "这是一个文件" ); } else if (filetwo.isDirectory()) { System.out.println( "这是一个文件夹" ); } System.out.print( "读写性:" ); if (filetwo.canRead() && filetwo.canWrite()) { System.out.println( "这个文件既可以读又可以写" ); } else if (filetwo.canRead() && !(filetwo.canWrite())) { System.out.println( "这个文件只能读" ); } else if (filetwo.canWrite() && !(filetwo.canRead())) { System.out.println( "这个文件只能读" ); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // TODO Auto-generated method stub // File one = new File("c:\\test\\imooc\\io\\score.txt"); // File one = new File("c:\\test\\imooc","io\\score.txt"); File one0 = new File( "c:\\test\\imooc" ); File one = new File(one0, "io\\score" ); // 判断文件还是目录 System.out.println( "是否是目录" + one.isDirectory()); System.out.println( "是否是文件" + one.isFile()); // 创建文件 File fileone0 = new File( "c:\\test\\set\\HashSet" ); if (!fileone0.exists()) { fileone0.mkdirs(); } } } |
18
收起
正在回答
2回答
同学你好,
1、对于第一个问题,相对路径是指与文件相对应的平级目录。而这里由于eclipse的目录结构比较特别,在eclipse中的相对路径都是只能相对于工程下的路径。所以在eclipse下无法使用相对目录创建在其他的文件夹,但是可以使用绝对路径进行创建。例如:
2、第二个问题,eclipse中使用绝对路径,是从盘符到文件的的整个路径,获取eclipse中相对路径也是与绝对路径一样的哦~,因为相对路径是指与文件相对应的平级目录。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
好帮手慕酷酷
2019-09-18 14:46:52
同学你好,这里同学在进行创建File对象时,使用的就是一个绝对路径创建的,所以输出的相对路径与绝对路径是一样的。具体如下:
只有创建的File对象是相对路径,输出的相对路径与绝对路径才会不同哦~具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import java.io.File; import java.io.IOException; public class FileDemo { public static void main(String[] args) { boolean flag = false ; File filetwo = new File( "Monday.docx" ); if (!filetwo.exists()) { try { filetwo.createNewFile(); System.out.println( "创建文件成功" ); System.out.println( "文件名称:" + filetwo.getName()); System.out.println(filetwo.getAbsolutePath()); System.out.println(filetwo.getPath()); System.out.print( "文件/目录:" ); if (filetwo.isFile()) { System.out.println( "这是一个文件" ); } else if (filetwo.isDirectory()) { System.out.println( "这是一个文件夹" ); } System.out.print( "读写性:" ); if (filetwo.canRead() && filetwo.canWrite()) { System.out.println( "这个文件既可以读又可以写" ); } else if (filetwo.canRead() && !(filetwo.canWrite())) { System.out.println( "这个文件只能读" ); } else if (filetwo.canWrite() && !(filetwo.canRead())) { System.out.println( "这个文件只能读" ); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧