topics: Java file getPath getAbsolutePath getCanonicalPath
2015-12-31 17:08
585 查看
public static void testFileIO(){
/* output:
-----case 1------
getPath()=.\test1.txt
getAbsolutePath()=C:\Users\hongbin\workspace\IOtrial\.\test1.txt
getCanonicalPath()=C:\Users\hongbin\workspace\IOtrial\test1.txt
*/
File file1 = new File(".\\test1.txt");
out.println("-----case 1------");
out.println("getPath()="+file1.getPath());
out.println("getAbsolutePath()="+file1.getAbsolutePath());
try{
out.println("getCanonicalPath()="+file1.getCanonicalPath());
} catch(IOException e) {
e.printStackTrace();
}
/*output
----case 2------
getPath()=\workspace\test\test1.txt
getAbsolutePath()=C:\workspace\test\test1.txt
getCanonicalPath()=C:\workspace\test\test1.txt
*/
File file2 = new File("\\workspace\\test\\test1.txt");
out.println("----case 2------");
out.println("getPath()="+file2.getPath());
out.println("getAbsolutePath()="+file2.getAbsolutePath());
try{
out.println("getCanonicalPath()="+file2.getCanonicalPath());
} catch(IOException e) {
e.printStackTrace();
}
/* output
----case 3------
getPath()=D:\workspace\test\test1.txt
getAbsolutePath()=D:\workspace\test\test1.txt
getCanonicalPath()=D:\workspace\test\test1.txt
*/
File file3 = new File("D:\\workspace\\test\\test1.txt");
out.println("----case 3------");
out.println("getPath()="+file3.getPath());
out.println("getAbsolutePath()="+file3.getAbsolutePath());
try{
out.println("getCanonicalPath()="+file3.getCanonicalPath());
} catch(IOException e) {
e.printStackTrace();
}
return;
}
/* output:
-----case 1------
getPath()=.\test1.txt
getAbsolutePath()=C:\Users\hongbin\workspace\IOtrial\.\test1.txt
getCanonicalPath()=C:\Users\hongbin\workspace\IOtrial\test1.txt
*/
File file1 = new File(".\\test1.txt");
out.println("-----case 1------");
out.println("getPath()="+file1.getPath());
out.println("getAbsolutePath()="+file1.getAbsolutePath());
try{
out.println("getCanonicalPath()="+file1.getCanonicalPath());
} catch(IOException e) {
e.printStackTrace();
}
/*output
----case 2------
getPath()=\workspace\test\test1.txt
getAbsolutePath()=C:\workspace\test\test1.txt
getCanonicalPath()=C:\workspace\test\test1.txt
*/
File file2 = new File("\\workspace\\test\\test1.txt");
out.println("----case 2------");
out.println("getPath()="+file2.getPath());
out.println("getAbsolutePath()="+file2.getAbsolutePath());
try{
out.println("getCanonicalPath()="+file2.getCanonicalPath());
} catch(IOException e) {
e.printStackTrace();
}
/* output
----case 3------
getPath()=D:\workspace\test\test1.txt
getAbsolutePath()=D:\workspace\test\test1.txt
getCanonicalPath()=D:\workspace\test\test1.txt
*/
File file3 = new File("D:\\workspace\\test\\test1.txt");
out.println("----case 3------");
out.println("getPath()="+file3.getPath());
out.println("getAbsolutePath()="+file3.getAbsolutePath());
try{
out.println("getCanonicalPath()="+file3.getCanonicalPath());
} catch(IOException e) {
e.printStackTrace();
}
return;
}
相关文章推荐
- Spring 集成 MyBatis 笔记(Mybatis-Spring 的用法)- 实战部分
- java10.面向对象之泛型
- Spring三种实例化Bean的方法
- struts2和spring的两种整合方式
- Java基础之泛型
- Java基础之泛型
- Java基础之泛型
- Java基础之泛型
- Java类的生命周期详解
- Java回调以及线程综合应用实例
- jsp+java下拉框读取数据库数据
- Spring boot将配置属性注入到bean类中
- jdk并发包 CopyOnWriteArrayList源代码分析
- JAVA中取子字符串的几种方式
- Java设计模式透析之 —— 适配器(Adapter)
- java 时间解析
- 导出Excel工具类
- java 死锁产生原因及解锁
- Java高级特性之泛型
- Eclipse的单步调试