A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 左手神刀 中级黑马   /  2013-7-9 18:58  /  1090 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨兴庭 于 2013-7-10 22:26 编辑

File f=new File("abc.txt");
                f.mkdir();//创建文件夹
                f.createNewFile();//创建文件
               
                System.out.println("是文件吗?"+f.isFile());
                System.out.println("是文件夹吗?"+f.isDirectory());
为什么创建文件成功了,而创建文件就失败了呢?想不通?求牛人给解释下。

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

5 个回复

倒序浏览
写错了修改一下,为什么创建文件夹成功了,而创建文件就失败了呢?想不通求牛人给解释下!!
回复 使用道具 举报
文件夹不能与文件重名。有个文件叫abc.txt就不能有一个叫abc.txt的文件夹了
回复 使用道具 举报
java在一个目录下无法同时创建同名的文件和文件夹,但是windows操作系统本身是支持这样的操作的。。。
回复 使用道具 举报 1 0
本帖最后由 toShareBeauty 于 2013-7-9 21:30 编辑

这个要从 java 的 File 说起,
  1.     /**
  2.      * Atomically creates a new, empty file named by this abstract pathname if
  3.      * and only if a file with this name does not yet exist.  The check for the
  4.      * existence of the file and the creation of the file if it does not exist
  5.      * are a single operation that is atomic with respect to all other
  6.      * filesystem activities that might affect the file.
  7.      * <P>
  8.      * Note: this method should <i>not</i> be used for file-locking, as
  9.      * the resulting protocol cannot be made to work reliably. The
  10.      * {@link java.nio.channels.FileLock FileLock}
  11.      * facility should be used instead.
  12.      *
  13.      * @return  <code>true</code> if the named file does not exist and was
  14.      *          successfully created; <code>false</code> if the named file
  15.      *          already exists
  16.      *
  17.      * @throws  IOException
  18.      *          If an I/O error occurred
  19.      *
  20.      * @throws  SecurityException
  21.      *          If a security manager exists and its <code>{@link
  22.      *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  23.      *          method denies write access to the file
  24.      *
  25.      * @since 1.2
  26.      */
  27.     public boolean createNewFile() throws IOException {
  28.         SecurityManager security = System.getSecurityManager();
  29.         if (security != null) security.checkWrite(path);
  30.         return fs.createFileExclusively(path);
  31.     }
复制代码
上面是 File 源文件的 createNewFile() 函数,return fs.createFileExclusively(path); 中的 fs 是类 FileSystem 的对象,
  1.     /**
  2.      * Create a new empty file with the given pathname.  Return
  3.      * <code>true</code> if the file was created and <code>false</code> if a
  4.      * file or directory with the given pathname already exists.  Throw an
  5.      * IOException if an I/O error occurs.
  6.      */
  7.     public abstract boolean createFileExclusively(String pathname)
  8.         throws IOException;
复制代码
这是 FileSystem.java 中的  createFileExclusively(String pathname) 函数,这个 FileSystem 是个 JNI ,也就是 java 本地扩展,它的实现使用 c/c++ 调用系统api或者函数库实现的。看到上面的注释了么,
Return <code>true</code> if the file was created and <code>false</code> if a file or directory with the given pathname already exists.
java 的 createNewFile 的时候会检查 File 表示的文件夹或者文件是否存在,如果其中一个存在就不会创建啦。同理,mkdir 的源码也可以这样分析。




评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

回复 使用道具 举报
楼上牛人,就是说文件夹和文件在同名情况下只能创建一个,在同一目录下时,如果你第二行,第三行代码换下位置,那么就是只能创建文件了,就是先来后到,谁先来了,就给谁
回复 使用道具 举报 0 1
您需要登录后才可以回帖 登录 | 加入黑马