Java中路径问题
2016-05-03 15:49
316 查看
package com.proxy; import java.io.File; public class TestPath { public static void main(String[] args) { File file1=new File(".\\test.propertise"); File file2=new File("F:\\Eclipseworkspace\\testspring\\src\\com\\proxy\\test.propertise"); System.out.println("file1:"+file1.getPath()); System.out.println("file1"+file1.getAbsolutePath()); System.out.println("file2:"+file2.getPath()); System.out.println("file2"+file2.getAbsolutePath()); } }
控制台显示:
file1:.\test.propertise
file1F:\Eclipseworkspace\testspring.\test.propertise
file2:F:\Eclipseworkspace\testspring\src\com\proxy\test.propertise
file2F:\Eclipseworkspace\testspring\src\com\proxy\test.propertise
但是刚刚从网上摘抄一段代码,也就稍微懂一点点,以后再深入研究:
package file; import java.io.File; import java.io.IOException; public class getFilePath { public static void main(String[] args) throws IOException { System.out.println("------默认相对路径,取得路径不同-----"); File f = new File("..\\src\\file"); System.out.println(f.getPath()); System.out.println(f.getAbsolutePath()); System.out.println(f.getCanonicalPath()); System.out.println("------默认相对路径,取得路径不同-----"); File f2 = new File(".\\src\\file"); System.out.println(f2.getPath()); System.out.println(f2.getAbsolutePath()); System.out.println(f2.getCanonicalPath()); System.out.println("------默认绝对路径,取得路径相同-----"); File f3 = new File("C:\\src\\file"); System.out.println(f3.getPath()); System.out.println(f3.getAbsolutePath()); System.out.println(f3.getCanonicalPath()); // 执行结果为: // ------默认相对路径,取得路径不同----- // ..\src\file // C:\workspace\Tip\..\src\file // C:\workspace\src\file // ------默认相对路径,取得路径不同----- // .\src\file // C:\workspace\Tip\.\src\file // C:\workspace\Tip\src\file // ------默认绝对路径,取得路径相同----- // C:\src\file // C:\src\file // C:\src\file // // 比较可以得到 // getPath()返回的是构造方法`这里写代码片`里的路径,不做任何处理 // getAbsolutePath()返回的是 user.dir+getPath(),也就是执行路径加上构造方法中的路径 // getCanonicalPath()返回的是将符号完全解析的路径,也就是全路径 } }
相关文章推荐
- java常量池
- Java多线程中wait, notify and notifyAll的使用
- Java中实现同步的集合
- JavaEE中遗漏的10个最重要的安全控制
- Java final关键字
- JAVA数据压缩简单测试
- 第六届蓝桥杯省赛(java)C组试题及参考答案
- spring核心容器
- JAVA 位操作学习
- 通过js把html标签转化为普通字符,再用java进行反转
- JAVA线程的创建与启动线程
- 对象序列化问题
- Struts+spring+Hibernate+Mysql--环境搭建
- 【JAVA】在线程里使用线程外的变量为什么一定要是final类型
- 利用java script获取两个日期区间内数组的方法
- eclipse mar整合lombok
- eclipse install gradle plugin used to create the gradle project
- java web学习路线
- Java多线程
- 77、Eclipse/MyEclipse怎么设置个性化代码注释模板