首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java io 文件路径有关问题

2014-01-23 
java io 文件路径问题首先我写出代码:public class test {public static void main(String[]args) throws

java io 文件路径问题
首先我写出代码:


public class test {
public static void main(String[]args) throws Exception{
File file = new File("d.txt");//错误的
//File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");//正确的
System.out.println(file.getAbsolutePath());
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}else{
System.out.println("oh,yse !");
}
}
}

这个代码,存在于H:\\eclipse\\workspace\\Socket文件夹中,
为什么File file = new File("d.txt");就报错,
File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");就正确?
不是说,直接写文件名就默认所在工程的路径么?
那么,为什么File file = new File("d.txt");却父类文件getParentFile()为null?
而写全路径H:\\eclipse\\workspace\\Socket\\d.txt就有了?
-------------
注:我用的是Eclipse
[解决办法]
File file = new File("d.txt");//错误的

的路径是:d.txt,(虽然绝对路径是:H:\\eclipse\\workspace\\Socket\\d.txt)在寻找父路径的时候就是以d.txt路径去寻找的,d.txt的父路径为null。你可以使用getPath()试一下

File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");//正确的

的路径是:H:\\eclipse\\workspace\\Socket\\d.txt,也就以这个路径去寻找父路径。


[解决办法]
楼主 你编写java类每次运行都是被虚拟机编译成class文件之后运行的,所以直接指定d.txt 肯定找不到。
相对路径中去找文件:
String name = this.getClass().getResource("d.txt").getPath();
[解决办法]
看一下File类的源码就很明了了

    public String getPath() {
        return path;
    }

    public File getParentFile() {
        String p = this.getParent();
        if (p == null) return null;
        return new File(p, this.prefixLength);
    }

    public String getParent() {
        int index = path.lastIndexOf(separatorChar);
        if (index < prefixLength) {
            if ((prefixLength > 0) && (path.length() > prefixLength))
                return path.substring(0, prefixLength);
            return null;
        }
        return path.substring(0, index);
    }

path是d.txt,当然getParentFile会得到null
[解决办法]
File file = new File("d.txt"); 寻找的是工程所在路径,请参考http://www.itzlk.com/io/index.jhtml
[解决办法]
3楼正解

    public String getPath() {
        return path;
    }

    public File getParentFile() {
        String p = this.getParent();
        if (p == null) return null;
        return new File(p, this.prefixLength);
    }

    public String getParent() {
        int index = path.lastIndexOf(separatorChar);        if (index < prefixLength) {
            if ((prefixLength > 0) && (path.length() > prefixLength))
                return path.substring(0, prefixLength);
            return null;
        }
        return path.substring(0, index);
    }
[解决办法]
引用:
File file = new File("d.txt"); 寻找的是工程所在路径,请参考http://www.itzlk.com/io/index.jhtml



这种路径是相对 System.getProperty("user.dir") 而言的。
[解决办法]
擦,那样写肯定不行呀
[解决办法]
默认的  是 类路径。。classpath
[解决办法]
引用:
Quote: 引用:

File file = new File("d.txt"); 寻找的是工程所在路径,请参考http://www.itzlk.com/io/index.jhtml


这种路径是相对 System.getProperty("user.dir") 而言的。


是的
[解决办法]
最根本的原因还是一个问题
相对路径还是绝对路径
[解决办法]
File类的getParent()方法和getParentFile方法会判断路径中是否有上一级目录,如果有,则会返回上级,如没有,返回null。楼主可以查看API

[解决办法]
new File("").getPath()看看当前目录绝对路径。把d.txt放到此目录下试试。
[解决办法]
引用:
首先我写出代码:

public class test {
public static void main(String[]args) throws Exception{
File file = new File("d.txt");//错误的
//File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");//正确的
System.out.println(file.getAbsolutePath());
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}else{
System.out.println("oh,yse !");
}
}
}


注:我用的是Eclipse


楼主兄弟,你好,首先我们对您出错的原因进行一次分析

问题一、 
File file = new File("d.txt");// 错误的


解答:
 1、对于File类构造方法,他需要的传入的是一个“路径名字符串”,而并不是一个单纯的文件名,对吧兄弟。

 2、对于jvm来说,在classloader加载时候,你所以存放的d.txt也会随classloader进行加载,因此他们属于同级目录。

 3、如果楼主真心想采用d.txt来读取的话。可以使用classloader加载原理来读取。

此方法需要注意,静态方法(通过当前的classloader加载的类来获取当前d.txt被加载的路径)
 String path = this.getClass().getResource("d.txt").getPath();

注意:this是不被静态方法所使用的。

通过具体的classloader加载的指定类来获取当前d.txt被加载的路径
 String path2 = Test.class.getResource("d.txt").getPath();


如下方式如何是正确的,我相信,楼主兄弟应该已经懂了吧。在下就不卖弄了。呵呵,祝楼主好运。
//File file = new File("H:\\eclipse\\workspace\\Socket\\d.txt");//正确的

热点排行