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:

==================================
D:\testWorkspace//打印的是eclipse的workspace
D:\testWorkspace
==================================
D:\
D:\
==================================
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()

Windows下Java File对象创建文件夹时的一个"坑"的更多相关文章

  1. Java中对象JSON格式化处理时的一个坑

    在项目中遇到了一个JSON的坑.记录下. 直接上代码: import java.util.ArrayList; import com.alibaba.fastjson.JSON; public cla ...

  2. Idea创建文件夹自动合成一个

    在idea中创建文件夹时,它们总是自动合成一个,如下图: 文件夹自动折叠真的很影响效率,可能会引发一些不经意的失误 解决方法: 取消这个地方的勾选 这样就可以正常创建文件夹了

  3. windows下使用RoboCopy命令进行文件夹增量备份

    RoboCopy,它是一个命令行的目录复制命令,自从Windows NT 4.0 开始就成为windows 资源工具包的一部分,然后在Windows Vista.Windows 7和 Windows ...

  4. java 按天创建文件夹

    按天创建文件夹,也就是每天创建一个,适合上传文件服务使用,文件数量较多时可以按文件夹区分. public static final String FMT = "yyyy-MM-dd" ...

  5. windows下更换MySql数据库数据文件夹位置

    详细解决地址 ,感谢博主  :https://blog.csdn.net/u010953266/article/details/56499361 概述 由于更换硬盘,系统重新安装了一遍,原来的mysq ...

  6. Linux下修改默认字符集--->解决Linux下Java程序种中文文件夹file.isDirectory()判断失败的问题

    一.问题描述: 一个项目中为了生成树状目录,调用了file.listFiles()方法,然后利用file.isDirectory()方法判断是否为目录,该程序在windows下运行无问题,在Linux ...

  7. Windows下的MySQL删除data文件夹后……

    MySQL删除data文件夹后,怎么都无法启动了,出现错误: 150106 9:28:43 [Note] Plugin 'FEDERATED' is disabled. wampmysqld: Tab ...

  8. mac环境下java项目无创建文件的权限

    1.问题: 先抛问题,由于刚刚换用mac环境,之前windows上开发的代码调试完毕,还未上线.之后上线部署之前,tl直连测试本地环境(mac)环境,功能无法使用,显示java.io.IOExcept ...

  9. sls语法:创建file,创建文件夹

    http://blog.kukafei520.net/html/2014/942.html /tmp/aaa.txt: file.managed /tmp/salt_test: file.direct ...

随机推荐

  1. PowerPoint Office Mix 插件

    一个内嵌在PowerPoint里的一个教学工具,可以以PPT为核心录制视频. 点下面链接了解并安装 https://mix.office.com/ 首先这货是免费,当然是基于PowerPoint的基础 ...

  2. hive 操作(转)

    1.命令行操作 (1)打印查询头,需要显示设置: set hive.cli.print.header=true; (2)加"--",其后的都被认为是注释,但 CLI 不解析注释.带 ...

  3. STM32普通定时器实现延时函数

    /* SystemFrequency / 1000 1ms中断一次 * SystemFrequency / 100000 10us中断一次 * SystemFrequency / 1000000 1u ...

  4. 标签跳转break和continue

    标签是后面跟有冒号的标识符,例如  label1: 在java中,标签起作用的唯一的地方刚好是在迭代语句之前. “刚好之前”的意思表明,在标签和迭代之间置入热和语句都不好. 而在迭代之前设置标签的唯一 ...

  5. C++中用辗转相除法求两个数的最大公约数和最小公倍数

    两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0?  max=min,min=res return min; ...

  6. android中实现Parcelable序列化步骤

    import java.io.Serializable; import java.text.DecimalFormat; import android.os.Parcel; import androi ...

  7. 1996: [Hnoi2010]chorus 合唱队 - BZOJ

    Description Input Output Sample Input41701 1702 1703 1704Sample Output8HINT 水题,区间dp,f[l,r,k]表示区间[l,r ...

  8. 一个有趣的 SQL 查询(查询7天连续登陆)

    一个有趣的 SQL 查询 一个朋友有这样一个SQL查询需求: 有一个登录表(tmp_test),包含用户ID(uid)和登录时间(login_time).表结构如下: . row ********** ...

  9. linux常用命令详解

    Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想真正理解Linux系统, ...

  10. 【BZOJ】【1529】 【POI2005】ska Piggy banks

    本来以为是tarjan缩点……但是64MB的空间根本不足以存下原图和缩点后的新图.所以呢……并查集= = orz hzwer MLE的tarjan: /************************ ...