Windows下Java File对象创建文件夹时的一个"坑"

简介: import java.io.File; import java.io.IOException; public class DirCreate { public static void main(String[] args) throws IOException { ...
import java.io.File;
import java.io.IOException;

public class DirCreate {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String dirStr="D:";
        File dir=new File(dirStr);
        System.out.println("=================1=================");
        System.out.println(dir.getAbsolutePath());
        System.out.println(dir.getCanonicalPath());
        
        dir=new File(dirStr+File.separator);
        System.out.println("=================2=================");
        System.out.println(dir.getAbsolutePath());
        System.out.println(dir.getCanonicalPath());
        
        dir=new File(dirStr,"placeholder").getParentFile();
        System.out.println("=================3=================");
        System.out.println(dir.getAbsolutePath());
        System.out.println(dir.getCanonicalPath());
    }
}

output:

=================1=================
D:\testWorkspace//打印的是eclipse的workspace
D:\testWorkspace
=================2=================
D:\
D:\
=================3=================
D:\
D:\

解析:

 String java.io.File.getAbsolutePath()


Returns the absolute pathname string of this abstract pathname. 

If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, 
which is named by the system property user.dir, is returned.
Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory. Returns: The absolute pathname string denoting the same file or directory as this abstract pathname Throws: SecurityException - If a required system property value cannot be accessed. See Also: java.io.File.isAbsolute()

 

相关文章
|
4天前
|
存储 Java 数据管理
Java零基础-Java对象详解
【10月更文挑战第7天】Java零基础教学篇,手把手实践教学!
20 6
|
4天前
|
监控 Java
Java定时扫码一个文件夹下的文件,如何保证文件写入完成后才进行处理?
【10月更文挑战第13天】Java定时扫码一个文件夹下的文件,如何保证文件写入完成后才进行处理?
17 1
|
8天前
|
Oracle Java 关系型数据库
重新定义 Java 对象相等性
本文探讨了Java中的对象相等性问题,包括自反性、对称性、传递性和一致性等原则,并通过LaptopCharger类的例子展示了引用相等与内容相等的区别。文章还介绍了如何通过重写`equals`方法和使用`Comparator`接口来实现更复杂的相等度量,以满足特定的业务需求。
13 3
|
8天前
|
存储 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第9天】在Java的世界里,对象序列化是连接数据持久化与网络通信的桥梁。本文将深入探讨Java对象序列化的机制、实践方法及反序列化过程,通过代码示例揭示其背后的原理。从基础概念到高级应用,我们将一步步揭开序列化技术的神秘面纱,让读者能够掌握这一强大工具,以应对数据存储和传输的挑战。
|
9天前
|
存储 Java 数据管理
Java零基础-Java对象详解
【10月更文挑战第3天】Java零基础教学篇,手把手实践教学!
10 1
|
14天前
|
XML Java Maven
在 Cucumber 测试中自动将 Cucumber 数据表映射到 Java 对象
在 Cucumber 测试中自动将 Cucumber 数据表映射到 Java 对象
27 7
|
13天前
|
Java 数据安全/隐私保护
java类和对象
java类和对象
19 5
|
13天前
|
Java 编译器 C语言
【一步一步了解Java系列】:类与对象的联系
【一步一步了解Java系列】:类与对象的联系
16 4
|
14天前
|
存储 安全 Java
Java编程中的对象序列化与反序列化
【10月更文挑战第3天】在Java编程的世界里,对象序列化与反序列化是实现数据持久化和网络传输的关键技术。本文将深入探讨Java序列化的原理、应用场景以及如何通过代码示例实现对象的序列化与反序列化过程。从基础概念到实践操作,我们将一步步揭示这一技术的魅力所在。
|
13天前
|
算法 Java API
【用Java学习数据结构系列】对象的比较(Priority Queue实现的前提)
【用Java学习数据结构系列】对象的比较(Priority Queue实现的前提)
23 1