`

java.io.File类介绍

 
阅读更多

 

File是用于表示或者说映射实际文件系统对象,如文件或目录。这样java代码就能操作计算机上的文件系统对象了。像setReadOnly()方法等一看就是设置该文件对象为只读。

File类的实例是不可变的;也就是说,一旦创建,File对象表示的抽象路径名将永不改变。

 

 

File类内部维护一个FileSystem接口的实现类,这个实现类非常重要,File许多方法的具体操作都是由这个FileSystem实现类完成的。不同操作系统文件系统不一样,那各自的JDK的FileSystem实现类也自然不一样。我用的是Windows系统,它的实现类就是WinNTFileSystem。

 

File类主要由两个属性path、prefixLength。代表文件对象路径,以及前缀长度(这个前缀长度表示了路径是绝对还是相对的)。新建一个File实例时就是调用FileSystem实现类来操作这两个属性、

 

public File(String pathname) {
        if (pathname == null) {
            throw new NullPointerException();
        }
//将路径标准化(比如规范路径字符串之间的分隔符等)
        this.path = fs.normalize(pathname);
//判断前缀长度
        this.prefixLength = fs.prefixLength(this.path);
    }

 

 

想看着个File对象到底对应文件系统哪个对象,可以调用getAbsolutePath()方法查看

 

public String getAbsolutePath() {
        return fs.resolve(this);
    }


//WinNTFileSystem中resolve方法
public String resolve(File f) {
        String path = f.getPath();
        int pl = f.getPrefixLength();
        if ((pl == 2) && (path.charAt(0) == slash))
            return path;                        /* UNC */
        if (pl == 3)
            return path;                        /* Absolute local */
        if (pl == 0)
            return getUserPath() + slashify(path); /* Completely relative */
        if (pl == 1) {                          /* Drive-relative */
            String up = getUserPath();
            String ud = getDrive(up);
            if (ud != null) return ud + path;
            return up + path;                   /* User dir is a UNC path */
        }
        if (pl == 2) {                          /* Directory-relative */
            String up = getUserPath();
            String ud = getDrive(up);
            if ((ud != null) && path.startsWith(ud))
                return up + slashify(path.substring(2));
            char drive = path.charAt(0);
            String dir = getDriveDirectory(drive);
            String np;
            if (dir != null) {
                /* When resolving a directory-relative path that refers to a
                   drive other than the current drive, insist that the caller
                   have read permission on the result */
                String p = drive + (':' + dir + slashify(path.substring(2)));
                SecurityManager security = System.getSecurityManager();
                try {
                    if (security != null) security.checkRead(p);
                } catch (SecurityException x) {
                    /* Don't disclose the drive's directory in the exception */
                    throw new SecurityException("Cannot resolve path " + path);
                }
                return p;
            }
            return drive + ":" + slashify(path.substring(2)); /* fake it */
        }
        throw new InternalError("Unresolvable path: " + path);
    }

 可以看到当前缀长度为3时,表示生成对象时使用绝对路径(暂称为传递路径),FIle对象对应文件系统对象的路径就是标准化后的传递路径

前缀长度为1时,传递路径为带斜杠的相对路径,那对应文件系统对象的路径就是工作目录(什么是工作目录后面解释)的根目录(即所在盘符位置)+标准化的传递路径

前缀长度为0时,传递路径为不带斜杠的相对路径,对应文件系统对象路径就是标准化后的工作目录+标准化的传递路径

 

 

工作目录即System.getProperty('user.dir')的值。这个值就是调用java命令的目录,比如cmd中执行一个class文件,值就是执行命令时所在目录。而在tomcat运行web项目,一般是tomcat目录/bin/startup.bat调用命令,则值就是bin目录。

如果是eclipse 新建的tomcat server ,默认为Working Directory ----- eclipse的目录,可以设置 eclipse下右击tomcat server 点击Open(有可能需要先stop)General Information 下的Open Lauch Configuration的Working directory 设置该路径

idea运行web应用时值默认是配置的tomcat server的bin目录,idea运行java应用时值默认是项目所在目录位置,当然可以通过Edit Configuration中的Working directory来配置

 

 

 

分享到:
评论

相关推荐

    java.io包中的File类.doc

    java.io包中的File类.doc

    java.io:clojure.java.io 的 JK7 java.nio.file.Path 兼容性

    java.io clojure.java.io 的 JK7 java.nio.file.Path 兼容性依赖信息该库托管在 Releases 上。 依赖: [me.moocar/java.io " 0.1.0 " ]用法是 JDK7 中引入的文件路径的抽象。 这个库提供了和 Paths 之间的兼容性。 ...

    java file,java读取txt文档

    java file,java读取txt文档,java操作txt文档,读取文档的内容

    android 串口驱动

    import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream...

    Java学生成绩管理系统源代码

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io....

    java io包课件

    了解java.io包 运用File类对文件或目录的属性进行操作 理解流,理解输入/输出流的概念 运用FileInputStream类和FileOutputStream类读/写字节文件 运用FileReader类和FileWriter类配合BufferedReader类和...

    Java简单的文件IO程序

    java做的简单文件IO操作,实现文件的读写追加功能。 压缩包里面有程序运行时的截图。

    Java对IO类File的操作

    对java.io.File类的操作

    xml2jsonjar包

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import ...

    Android实现下载zip压缩文件并解压的方法(附源码)

    前言 其实在网上有很多介绍下载文件或者解压zip文件的文章,但是...import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp

    学生管理系统

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io....

    优雅的操作文件:java.nio.file 库介绍.pdf

    所以本章,我们就来主要介绍 java.nio.file 中常用的类和模块,大致如下: Path 路径:Paths 模块和 Path 工具类介绍 Files 文件:File 和 FileSystems 工具类介绍 文件管理服务:WatchService 、PathMatcher 等等...

    java-io-file类笔记

    尽管java.io定义的大多数类是实行流式操作的,File类不是。它直接处理文件和文件系统。也就是说,File类没有指定信息怎样从文件读取或向文件存储;它描述了文件本身的属性。File对象用来获取或处理与磁盘文件相关的...

    编写一个java应用程序将一个包含多个子目录和文件的目录复制到另外一个指定的目录下

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.nio.channels.FileChannel; import java....

    wsdl文件解析

    import java.io.File; import java.io.FileOutputStream; //import java.io.FileWriter; //import java.io.OutputStreamWriter; import java.io.PrintStream; //import java.nio.charset.Charset; //import ...

    可以看成是java.io的扩展。它提供了文件阅读功能,以及一些使用的方法

    可以看成是java.io的扩展。它提供了文件阅读功能,以及一些使用的方法

    java 读写EXEcel文档范例

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import ...

    提款机系统源代码

    this.dataFile.createNewFile(); } this.fw = new FileWriter(this.dataFile,true); this.bw = new BufferedWriter(this.fw); } catch (IOException io) { System.err.println("Cannot open file...

    i18n Tools helper

    import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * . <p/> @author George Wei */ public class I18nTool { private static final String TEMP_...

    package com.test;package com.test;package com.test;package com.test;

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new ...

Global site tag (gtag.js) - Google Analytics