Java 日看一类(15)之IO包中的File类

Windows Server 搭建ftp服务 阅读详情

File是相当常用的一个类,也是IO包中的基础类之一


File类引用了如下包

import java.net.URI;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.ArrayList;
import java.security.AccessController;
import java.security.SecureRandom;
import java.nio.file.Path;
import java.nio.file.FileSystems;
import sun.security.action.GetPropertyAction;

继承了如下接口

implements Serializable, Comparable<File>




该类的类头注释如下(比较长,毕竟功能比较多)

/**
 * An abstract representation of file and directory pathnames.
 *
 * <p> User interfaces and operating systems use system-dependent <em>pathname
 * strings</em> to name files and directories.  This class presents an
 * abstract, system-independent view of hierarchical pathnames.  An
 * <em>abstract pathname</em> has two components:
 *
 * <ol>
 * <li> An optional system-dependent <em>prefix</em> string,
 *      such as a disk-drive specifier, <code>"/"</code>&nbsp;for the UNIX root
 *      directory, or <code>"\\\\"</code>&nbsp;for a Microsoft Windows UNC pathname, and
 * <li> A sequence of zero or more string <em>names</em>.
 * </ol>
 *
 * The first name in an abstract pathname may be a directory name or, in the
 * case of Microsoft Windows UNC pathnames, a hostname.  Each subsequent name
 * in an abstract pathname denotes a directory; the last name may denote
 * either a directory or a file.  The <em>empty</em> abstract pathname has no
 * prefix and an empty name sequence.
 *
 * <p> The conversion of a pathname string to or from an abstract pathname is
 * inherently system-dependent.  When an abstract pathname is converted into a
 * pathname string, each name is separated from the next by a single copy of
 * the default <em>separator character</em>.  The default name-separator
 * character is defined by the system property <code>file.separator</code>, and
 * is made available in the public static fields <code>{@link
 * #separator}</code> and <code>{@link #separatorChar}</code> of this class.
 * When a pathname string is converted into an abstract pathname, the names
 * within it may be separated by the default name-separator character or by any
 * other name-separator character that is supported by the underlying system.
 *
 * <p> A pathname, whether abstract or in string form, may be either
 * <em>absolute</em> or <em>relative</em>.  An absolute pathname is complete in
 * that no other information is required in order to locate the file that it
 * denotes.  A relative pathname, in contrast, must be interpreted in terms of
 * information taken from some other pathname.  By default the classes in the
 * <code>java.io</code> package always resolve relative pathnames against the
 * current user directory.  This directory is named by the system property
 * <code>user.dir</code>, and is typically the directory in which the Java
 * virtual machine was invoked.
 *
 * <p> The <em>parent</em> of an abstract pathname may be obtained by invoking
 * the {@link #getParent} method of this class and consists of the pathname's
 * prefix and each name in the pathname's name sequence except for the last.
 * Each directory's absolute pathname is an ancestor of any <tt>File</tt>
 * object with an absolute abstract pathname which begins with the directory's
 * absolute pathname.  For example, the directory denoted by the abstract
 * pathname <tt>"/usr"</tt> is an ancestor of the directory denoted by the
 * pathname <tt>"/usr/local/bin"</tt>.
 *
 * <p> The prefix concept is used to handle root directories on UNIX platforms,
 * and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms,
 * as follows:
 *
 * <ul>
 *
 * <li> For UNIX platforms, the prefix of an absolute pathname is always
 * <code>"/"</code>.  Relative pathnames have no prefix.  The abstract pathname
 * denoting the root directory has the prefix <code>"/"</code> and an empty
 * name sequence.
 *
 * <li> For Microsoft Windows platforms, the prefix of a pathname that contains a drive
 * specifier consists of the drive letter followed by <code>":"</code> and
 * possibly followed by <code>"\\"</code> if the pathname is absolute.  The
 * prefix of a UNC pathname is <code>"\\\\"</code>; the hostname and the share
 * name are the first two names in the name sequence.  A relative pathname that
 * does not specify a drive has no prefix.
 *
 * </ul>
 *
 * <p> Instances of this class may or may not denote an actual file-system
 * object such as a file or a directory.  If it does denote such an object
 * then that object resides in a <i>partition</i>.  A partition is an
 * operating system-specific portion of storage for a file system.  A single
 * storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may
 * contain multiple partitions.  The object, if any, will reside on the
 * partition <a name="partName">named</a> by some ancestor of the absolute
 * form of this pathname.
 *
 * <p> A file system may implement restrictions to certain operations on the
 * actual file-system object, such as reading, writing, and executing.  These
 * restrictions are collectively known as <i>access permissions</i>.  The file
 * system may have multiple sets of access permissions on a single object.
 * For example, one set may apply to the object's <i>owner</i>, and another
 * may apply to all other users.  The access permissions on an object may
 * cause some methods in this class to fail.
 *
 * <p> Instances of the <code>File</code> class are immutable; that is, once
 * created, the abstract pathname represented by a <code>File</code> object
 * will never change.
 *
 * <h3>Interoperability with {@code java.nio.file} package</h3>
 *
 * <p> The <a href="../../java/nio/file/package-summary.html">{@code java.nio.file}</a>
 * package defines interfaces and classes for the Java virtual machine to access
 * files, file attributes, and file systems. This API may be used to overcome
 * many of the limitations of the {@code java.io.File} class.
 * The {@link #toPath toPath} method may be used to obtain a {@link
 * Path} that uses the abstract path represented by a {@code File} object to
 * locate a file. The resulting {@code Path} may be used with the {@link
 * java.nio.file.Files} class to provide more efficient and extensive access to
 * additional file operations, file attributes, and I/O exceptions to help
 * diagnose errors when an operation on a file fails.
 *
 * @author  unascribed
 * @since   JDK1.0
 */

大意如下

该类是一个文件和目录的路径名的抽象表示形式

用户界面和操作系统使用基于操作系统的命名方法,使用路径字符串对目录和目录命名

该类表现了一个不基于操作系统的、抽象的的层级路径视图


抽象路径分为两个组成部分:

1.可选的基于系统的前缀字符串,比如:磁盘驱动器的标识符。‘/’表示UNIX的根目录,‘\\\’表示windows的UNC目录

2.一个含有零个或更多字符的字符串序列

抽象路径的第一个名称可能是路径名,对于UNC来说就是主机名

抽象路径第一个之后的名称,每一个都表示一个目录名称,最后的名称可以表示目录或者文件

空路径没有前缀和名称序列


实际路径的字符串与抽象路径的转换方法取决于操作系统(操作系统固有)

当一个抽象路径转化为实际路径字符串时,每一个名字之间用默认的分隔符相互分离

默认分隔符通过操作系统的属性file.separator定义,可以调用此类的公共静态字段separator和separatorChar来使用

当实际路径字符串转化为抽象路径时,其中的名字可以被默认的名称分隔符分隔或者系统支持的其他任意名称分隔符分隔

无论是抽象路径还是实际路径字符串,都只能是相对路径名或绝对路径名

绝对路径是完整的,不需要其他任何信息去定位其表示的文件

相对路径相比之下必须要依靠其他路径信息来解释该路径指向

默认情况下,在IO包中的该类总是使用当前用户目录来解释相对路径

该目录由系统属性user.dir指定,并且该目录通常由java虚拟机调用


抽象路径的父路径通常通过调用该类的getParent()方法获得(包括前缀和每一级的名称,直到本级)

每一个目录的绝对路径是许多含有从该路径开始的绝对抽象路径的File对象的原型

例如,表示为‘/usr’的抽象路径是表示为‘/usr/local/bin’的抽象路径的原型


前缀概念用于:操作UNIX平台的根目录;在windows下使用根目录、特殊标识符和UNC的路径名。如下所示:

1. 对于 UNIX 平台,绝对路径名的前缀始终是 "/"。相对路径名没有前缀。表示根目录的绝对路径名的前缀为 "/" 且名称序列为空。

2.对于 Microsoft Windows 平台,包含盘符的路径名前缀由驱动器号和一个 ":" 组成。如果路径名是绝对路径名,还可能后跟 "\\"。UNC 路径名的前缀是 "\\\\";主机名和共享名是名称序列中的前两个名称。没有指定驱动器的相对路径名没有前缀。


此类的实例可能表示(也可能不表示)实际文件系统对象,如文件或目录。如果它表示这种对象,那么该对象驻留在一个分区 中。分区是文件系统特定于操作系统的存储分区。一个存储设备(例如,物理磁盘驱动器、闪存、CD-ROM)可以包含多个分区。对象(如果有)将驻留在此路径名(绝对形式)某个祖先指定的分区上。

文件系统可以实现对实际文件系统对象上的某些操作(比如,读、写、执行)进行限制。这些限制统称为访问权限。文件系统可以对一个对象设置多个访问权限。例如,一个设置可能适用于对象的所有者,另一个设置则可能适用于所有其他用户。对象上的访问权限可能导致此类的某些方法执行失败。

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




File类包含如下的成员变量:

文件系统

private static final FileSystem fs = DefaultFileSystem.getFileSystem();

文件路径

private final String path;

文件路径状态枚举(无效,已审核)

private static enum PathStatus { INVALID, CHECKED };

文件路径状态(transient修饰的变量不能被串行化)

private transient PathStatus status = null;

抽象路径前缀名长度

private final transient int prefixLength;

路径分隔符(路径中的分隔符,如‘/’,‘//’)

public static final char separatorChar = fs.getSeparator();

字符串格式的路径分隔符

public static final String separator = "" + separatorChar;

路径序列的分隔符(分离多个路径,如‘:’,‘;’)

public static final char pathSeparatorChar = fs.getPathSeparator();

字符串格式的路径序列分隔符

public static final String pathSeparator = "" + pathSeparatorChar;

静态代码块绑定的常量

private static final long PATH_OFFSET;
private static final long PREFIX_LENGTH_OFFSET;
private static final sun.misc.Unsafe UNSAFE;

串行序列号

private static final long serialVersionUID = 301077366599181567L;

路径(要求每次使用都要读值,不会被编译器优化,同时不会被序列化)

private volatile transient Path filePath;



File 类包含如下的方法:

判定文件路径状态是否为无效(仅在该类初始化时使用,仅在初始化时状态为null)

final boolean isInvalid() {
    if (status == null) {
        status = (this.path.indexOf('\u0000') < 0) ? PathStatus.CHECKED//不存在为空的字符
                                                   : PathStatus.INVALID;//存在为空的字符
    }
    return status == PathStatus.INVALID;
}

获取前缀长度

int getPrefixLength() {
    return prefixLength;
}

构造函数(指定文件路径和前缀长度)

private File(String pathname, int prefixLength) {
    this.path = pathname;
    this.prefixLength = prefixLength;
}

构造函数(相对路径,参照文件)

private File(String child, File parent) {
    assert parent.path != null;//判定路径存在
    assert (!parent.path.equals(""));//判定路径不为空字符
    this.path = fs.resolve(parent.path, child);//转化为绝对路径
    this.prefixLength = parent.prefixLength;
}

构造函数(输入的是抽象路径,比如/usr/bin)

public File(String pathname) {
    if (pathname == null) {//文件名为空,不是文件找不到
        throw new NullPointerException();
    }
    this.path = fs.normalize(pathname);//输入的路径转化为实际路径字符串
    this.prefixLength = fs.prefixLength(this.path);
}

构造函数(相对路径,父类绝对路径)

public File(String parent, String child) {
    if (child == null) {
        throw new NullPointerException();
    }
    if (parent != null) {
        if (parent.equals("")) {//为空字符
            this.path = fs.resolve(fs.getDefaultParent(),//系统根目录
                                   fs.normalize(child));
        } else {
            this.path = fs.resolve(fs.normalize(parent),
                                   fs.normalize(child));
        }
    } else {//父路径为空视作绝对路径处理
        this.path = fs.normalize(child);
    }
    this.prefixLength = fs.prefixLength(this.path);
}

构造函数(参照文件和相对路径)

public File(File parent, String child) {
    if (child == null) {
        throw new NullPointerException();
    }
    if (parent != null) {
        if (parent.path.equals("")) {
            this.path = fs.resolve(fs.getDefaultParent(),
                                   fs.normalize(child));
        } else {
            this.path = fs.resolve(parent.path,
                                   fs.normalize(child));
        }
    } else {
        this.path = fs.normalize(child);
    }
    this.prefixLength = fs.prefixLength(this.path);
}

构造函数(uri,统一资源标识符)

public File(URI uri) {

    // Check our many preconditions
    if (!uri.isAbsolute())
        throw new IllegalArgumentException("URI is not absolute");//是否为绝对uri
    if (uri.isOpaque())
        throw new IllegalArgumentException("URI is not hierarchical");//是否不透明
    String scheme = uri.getScheme();//uri方案组成
    if ((scheme == null) || !scheme.equalsIgnoreCase("file"))//忽略大小写进行判等,并且判定方案是否为空
        throw new IllegalArgumentException("URI scheme is not \"file\"");
    if (uri.getAuthority() != null)//原始授权组成部分不为空
        throw new IllegalArgumentException("URI has an authority component");
    if (uri.getFragment() != null)//已解码的片段组成部分不为空
        throw new IllegalArgumentException("URI has a fragment component");
    if (uri.getQuery() != null)//已解码的查询组成部分不为空
        throw new IllegalArgumentException("URI has a query component");
    String p = uri.getPath();//已解码的路径组成部分
    if (p.equals(""))
        throw new IllegalArgumentException("URI path component is empty");

    // Okay, now initialize
    p = fs.fromURIPath(p);
    if (File.separatorChar != '/')
        p = p.replace('/', File.separatorChar);//替换分隔符
    this.path = fs.normalize(p);
    this.prefixLength = fs.prefixLength(this.path);
}

返回前缀符后面的字符串值(即文件路径即名称)

public String getName() {
    int index = path.lastIndexOf(separatorChar);//最后分隔符的位置
    if (index < prefixLength) return path.substring(prefixLength);//返回前缀之后的字符串
    return path.substring(index + 1);//不存在前缀情况
}

返回前缀(文件父路径)

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对象)

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

获得文件路径

public String getPath() {
    return path;
}

根据系统判定该文件对象路径是否为绝对路径

public boolean isAbsolute() {
    return fs.isAbsolute(this);
}

获得当前对象的绝对路径

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

获得当前对象的新对象(绝对路径构建)

public File getAbsoluteFile() {
    String absPath = getAbsolutePath();
    return new File(absPath, fs.prefixLength(absPath));
}

返回当前对象的规范路径名字符串

public String getCanonicalPath() throws IOException {
    if (isInvalid()) {//有效性检查
        throw new IOException("Invalid file path");
    }
    return fs.canonicalize(fs.resolve(this));
}

返回当前对象的规范路径名称形式(返回对象)

public File getCanonicalFile() throws IOException {
    String canonPath = getCanonicalPath();
    return new File(canonPath, fs.prefixLength(canonPath));
}

对路径进行规整化处理(私有,静态)

private static String slashify(String path, boolean isDirectory) {
    String p = path;
    if (File.separatorChar != '/')//保证分隔符为‘/’
        p = p.replace(File.separatorChar, '/');
    if (!p.startsWith("/"))//起始位置不存在分隔符
        p = "/" + p;
    if (!p.endsWith("/") && isDirectory)//如果该路径为目录,则最后也要加入‘/’
        p = p + "/";
    return p;
}

返回路径的URL格式(该方法已经过时,无法自动转义非法字符,一般情况下使用toURI,然后使用URI.toURl方法转义)

public URL toURL() throws MalformedURLException {
    if (isInvalid()) {
        throw new MalformedURLException("Invalid file path");
    }
    return new URL("file", "", slashify(getAbsolutePath(), isDirectory()));
}

返回路径的URI格式

public URI toURI() {
    try {
        File f = getAbsoluteFile();
        String sp = slashify(f.getPath(), f.isDirectory());//路径标准化
        if (sp.startsWith("//"))
            sp = "//" + sp;//如果有两斜杠就再加两斜杠(格式要求
        return new URI("file", null, sp, null);//根据给定素材构造分层URI,其中host和fragement为空(file方案,sp路径)
    } catch (URISyntaxException x) {//语法异常
        throw new Error(x);         // Can't happen
    }
}

检测文件或目录可读性

public boolean canRead() {
    SecurityManager security = System.getSecurityManager();//获得安全管理器
    if (security != null) {
        security.checkRead(path);//判定线程是否能读取给定字符串所表示的文件,不行就抛出SecurityException
    }
    if (isInvalid()) {
        return false;
    }
    return fs.checkAccess(this, FileSystem.ACCESS_READ);//默认使用的是UNIX文件系统,该方法在UNIX文件系统中,作用是检查该抽象路径指定的文件是否能完成后一个参数的要求
}

检测文件或目录可写性

public boolean canWrite() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.checkAccess(this, FileSystem.ACCESS_WRITE);
}

检测文件或目录存在

public boolean exists() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(path);
    }
    if (isInvalid()) {
        return false;
    }
    return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);//判定文件是否存在,中间符号是与不是且,不过没看太懂为啥要和16进制的1与
}

检测抽象路径是否为目录

public boolean isDirectory() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(path);
    }
    if (isInvalid()) {
        return false;
    }
    return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)//这次是和16进制的4与,目测该方法返回值是根据不同状态返回不同值(没找到getBooleanAttributes0()的源码,不过由于返回值为int,应该是这样
            != 0);
}

检测抽象目录是否为文件

public boolean isFile() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(path);
    }
    if (isInvalid()) {
        return false;
    }
    return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0);//和2与
}

检测指定的目录和文件是否为隐藏目录或文件

public boolean isHidden() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(path);
    }
    if (isInvalid()) {
        return false;
    }
    return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);//和8与
}

返回文件的最后修改日期

public long lastModified() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(path);
    }
    if (isInvalid()) {
        return 0L;
    }
    return fs.getLastModifiedTime(this);
}

获得文件的大小

public long length() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(path);
    }
    if (isInvalid()) {
        return 0L;
    }
    return fs.getLength(this);
}

创建当前对象绑定的文件

public boolean createNewFile() throws IOException {
    SecurityManager security = System.getSecurityManager();
    if (security != null) security.checkWrite(path);
    if (isInvalid()) {
        throw new IOException("Invalid file path");
    }
    return fs.createFileExclusively(path);
}

删除当前对象绑定的文件

public boolean delete() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkDelete(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.delete(this);
}

在虚拟机退出时删除该文件

public void deleteOnExit() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkDelete(path);
    }
    if (isInvalid()) {
        return;
    }
    DeleteOnExitHook.add(path);//加入删除map
}

返回字符串数组,表示抽象路径中的目录和文件(分离开各层)

public String[] list() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkRead(path);
    }
    if (isInvalid()) {
        return null;
    }
    return fs.list(this);
}

 返回字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录。

public String[] list(FilenameFilter filter) {
    String names[] = list();
    if ((names == null) || (filter == null)) {
        return names;
    }
    List<String> v = new ArrayList<>();
    for (int i = 0 ; i < names.length ; i++) {
        if (filter.accept(this, names[i])) {//过滤器判定接受
            v.add(names[i]);
        }
    }
    return v.toArray(new String[v.size()]);//数组化
}
返回文件数组(每一个单元都是给定路径中的一层)
public File[] listFiles() {
    String[] ss = list();
    if (ss == null) return null;
    int n = ss.length;
    File[] fs = new File[n];
    for (int i = 0; i < n; i++) {
        fs[i] = new File(ss[i], this);
    }
    return fs;
}

返回满足过滤器的文件数组

public File[] listFiles(FilenameFilter filter) {//过滤文件、目录名
    String ss[] = list();
    if (ss == null) return null;
    ArrayList<File> files = new ArrayList<>();
    for (String s : ss)
        if ((filter == null) || filter.accept(this, s))//过滤器不为空且文件允许通过
            files.add(new File(s, this));
    return files.toArray(new File[files.size()]);
}

返回满足过滤器的文件数组(作用同上,写法不同,这个是先生成对象再对对象检查,上一个是检查通过再生成对象,主要是由于过滤器的类型不同)

public File[] listFiles(FileFilter filter) {//过滤抽象路径名
    String ss[] = list();
    if (ss == null) return null;
    ArrayList<File> files = new ArrayList<>();
    for (String s : ss) {
        File f = new File(s, this);
        if ((filter == null) || filter.accept(f))
            files.add(f);
    }
    return files.toArray(new File[files.size()]);
}

创建当前抽象路径所指定的目录

public boolean mkdir() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.createDirectory(this);
}

创建当前抽象路径所指定的目录,同时生成必须的父目录

public boolean mkdirs() {
    if (exists()) {
        return false;
    }
    if (mkdir()) {
        return true;
    }
    File canonFile = null;
    try {
        canonFile = getCanonicalFile();//路径规范化
    } catch (IOException e) {
        return false;
    }

    File parent = canonFile.getParentFile();//生成父路径对象
    return (parent != null && (parent.mkdirs() || parent.exists()) &&
            canonFile.mkdir());//父路径不为空且父路径存在或上溯成功且规范化后创建成功
}

改名(同时修改路径名称)

public boolean renameTo(File dest) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);//检查可写性
        security.checkWrite(dest.path);//需改变的抽象路径可写性
    }
    if (dest == null) {
        throw new NullPointerException();
    }
    if (this.isInvalid() || dest.isInvalid()) {//均不为空
        return false;
    }
    return fs.rename(this, dest);//修改
}

设置最后修改时间

public boolean setLastModified(long time) {
    if (time < 0) throw new IllegalArgumentException("Negative time");
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.setLastModifiedTime(this, time);
}

设置只读属性

public boolean setReadOnly() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.setReadOnly(this);
}

设置可写属性

public boolean setWritable(boolean writable, boolean ownerOnly) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.setPermission(this, FileSystem.ACCESS_WRITE, writable, ownerOnly);//bool为真则仅允许所有者修改,为假则所有人均可修改
}

设置可写属性(默认)

public boolean setWritable(boolean writable) {
    return setWritable(writable, true);
}

设置可读属性

public boolean setReadable(boolean readable, boolean ownerOnly) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.setPermission(this, FileSystem.ACCESS_READ, readable, ownerOnly);//仅允许所有者读
}

设置可读属性(默认)

public boolean setReadable(boolean readable) {
    return setReadable(readable, true);
}

修改文件类型为可执行文件

public boolean setExecutable(boolean executable, boolean ownerOnly) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.setPermission(this, FileSystem.ACCESS_EXECUTE, executable, ownerOnly);//执行文件权限
}

修改文件类型为可执行文件

public boolean setExecutable(boolean executable) {
    return setExecutable(executable, true);
}

判断文件是否可执行

public boolean canExecute() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkExec(path);//检测路径的可执行性
    }
    if (isInvalid()) {
        return false;
    }
    return fs.checkAccess(this, FileSystem.ACCESS_EXECUTE);
}

返回文件系统根目录(可用的)

public static File[] listRoots() {
    return fs.listRoots();
}

返回该路径指定的分区空间大小(byte返回

public long getTotalSpace() {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
        sm.checkRead(path);
    }
    if (isInvalid()) {
        return 0L;
    }
    return fs.getSpace(this, FileSystem.SPACE_TOTAL);
}

返回该路径中未被使用的分区空间大小(byte返回

public long getFreeSpace() {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
        sm.checkRead(path);
    }
    if (isInvalid()) {
        return 0L;
    }
    return fs.getSpace(this, FileSystem.SPACE_FREE);
}

返回该路径中已被使用的分区大小(byte返回

public long getUsableSpace() {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
        sm.checkRead(path);
    }
    if (isInvalid()) {
        return 0L;
    }
    return fs.getSpace(this, FileSystem.SPACE_USABLE);
}

创建临时文件

public static File createTempFile(String prefix, String suffix,
                                  File directory)//前缀、后缀、目录
    throws IOException
{
    if (prefix.length() < 3)//前缀过短检测
        throw new IllegalArgumentException("Prefix string too short");
    if (suffix == null)//没有文件类型标识符就认为是.tmp
        suffix = ".tmp";

    File tmpdir = (directory != null) ? directory//给定目录不为空则创建给定目录,为空则调用私有静态类进行路径指定
                                      : TempDirectory.location();
    SecurityManager sm = System.getSecurityManager();
    File f;
    do {
        f = TempDirectory.generateFile(prefix, suffix, tmpdir);//获得一个临时文件

        if (sm != null) {
            try {
                sm.checkWrite(f.getPath());//检查目录或文件可写性
            } catch (SecurityException se) {
                // don't reveal temporary directory location
                if (directory == null)
                    throw new SecurityException("Unable to create temporary file");
                throw se;
            }
        }
    } while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0);//文件存在时循环

    if (!fs.createFileExclusively(f.getPath()))//创建新的空文件失败(文件路径已存在或者为空)
        throw new IOException("Unable to create temporary file");

    return f;
}

创建无父路径的临时文件

public static File createTempFile(String prefix, String suffix)
    throws IOException
{
    return createTempFile(prefix, suffix, null);
}

路径比较

public int compareTo(File pathname) {
    return fs.compare(this, pathname);
}

实例比较(实际还是调用路径比较)

public boolean equals(Object obj) {
    if ((obj != null) && (obj instanceof File)) {//对象不为空且对象为File实例
        return compareTo((File)obj) == 0;
    }
    return false;
}

返回路径的hash值

public int hashCode() {
    return fs.hashCode(this);
}

返回路径的字符串

public String toString() {
    return getPath();
}

保存文件名(保存对象,分隔符也同时保存

private synchronized void writeObject(java.io.ObjectOutputStream s)
    throws IOException
{
    s.defaultWriteObject();
    s.writeChar(separatorChar); // Add the separator character
}

读取保存文件(恢复文件

private synchronized void readObject(java.io.ObjectInputStream s)
     throws IOException, ClassNotFoundException
{
    ObjectInputStream.GetField fields = s.readFields();//获得读取域
    String pathField = (String)fields.get("path", null);//获得文件路径域
    char sep = s.readChar(); // read the previous separator char
    if (sep != separatorChar)
        pathField = pathField.replace(sep, separatorChar);//替换分隔符(一般是不同系统平台之间需要
    String path = fs.normalize(pathField);//标准化
    UNSAFE.putObject(this, PATH_OFFSET, path);
    UNSAFE.putIntVolatile(this, PREFIX_LENGTH_OFFSET, fs.prefixLength(path));
}

抽象路径实际路径化

public Path toPath() {
    Path result = filePath;
    if (result == null) {
        synchronized (this) {//线程锁
            result = filePath;
            if (result == null) {
                result = FileSystems.getDefault().getPath(path);//获取默认文件系统(“java.nio.file.spi.DefaultFileSystemProvider”),根据文件系统来获取实际路径
                filePath = result;
            }
        }
    }
    return result;
}




含有一个静态代码块

static {
    try {
        sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();//unsafe之后可以“突破限制”,有兴趣可以自己去查阅资料,(虽然资料很少)这里不再赘述
        PATH_OFFSET = unsafe.objectFieldOffset(//绑定各种域和常量
                File.class.getDeclaredField("path"));
        PREFIX_LENGTH_OFFSET = unsafe.objectFieldOffset(
                File.class.getDeclaredField("prefixLength"));
        UNSAFE = unsafe;
    } catch (ReflectiveOperationException e) {
        throw new Error(e);
    }
}



含有一个私有静态类:

临时目录

private static class TempDirectory {
    private TempDirectory() { }//私有构造函数

    // temporary directory location
    private static final File tmpdir = new File(AccessController
        .doPrivileged(new GetPropertyAction("java.io.tmpdir")));//实际是调用System.getProperty查询这个预定参数,获得的是默认的临时文件路径(权限不检查
    static File location() {//default权限仅允许包内访问,
        return tmpdir;
    }

    // file name generation
    private static final SecureRandom random = new SecureRandom();//生成强随机数
    static File generateFile(String prefix, String suffix, File dir)//前缀、后缀、上层目录名
        throws IOException
    {
        long n = random.nextLong();//获得一个随机long值
        if (n == Long.MIN_VALUE) {//其实都是绝对值,只不过long的最小值取绝对值会溢出,所以取0,此时各个值的所取概率也恰好相等(long的最小值为-2的63次方,最大值为2的63次方-1)
            n = 0;      // corner case
        } else {
            n = Math.abs(n);
        }

        // Use only the file name from the supplied prefix
        prefix = (new File(prefix)).getName();//获得目录名

        String name = prefix + Long.toString(n) + suffix;//生成路径
        File f = new File(dir, name);//依据父路径和新相对路径生成新对象
        if (!name.equals(f.getName()) || f.isInvalid()) {//生成路径与新对象的名字不相同或新对象无效
            if (System.getSecurityManager() != null)
                throw new IOException("Unable to create temporary file");
            else
                throw new IOException("Unable to create temporary file, " + f);
        }
        return f;
    }
}



File类实质就是一个抽象路径,在类中封装了大量对该抽象路径的操作,也同时封装了对当前文件系统的大量操作(其实file感觉啥都没做,全是filesystem在做),功能十分的全面,推荐大家去自主学习并观察底层的使用与链接方式

该类也是目前做到的类中最难的,如有错误请不吝赐教。

VMware虚拟机的安装教程 VMware 虚拟机的备份与恢复是保障数据安全和业务连续性的重要措施。综上所述,VMware 虚拟机的备份与恢复策略有多种选择,企业和用户可以根据实际需求和资源情况选择适合自己的方案,以确保虚拟机数据的安全和业务的连续性。 阅读详情

相关推荐

WPS专业版自带字体

WPS专业版其中一个好处就是自带了部分有版权的字体,就算你安装了没买授权,他也会给你在系统里面安装上这些字体,至于没授权继续用合不合规我不知道。但是带来的实实在在的好处就是有些子他支持的全啊。 /usr/share/fonts/wps-office/FZWBK.TTF: 方正魏碑_GBK,FZWeiBei\-S03:style=Regular /usr/share/fonts/wps-office/FZFSK.TTF: 方正仿宋_GBK,FZFangSong\-Z02:style=Regular /us

chenqioulin的博客 5710

Java IO流学习总结二:File

转载请标明出处:http://blog.youkuaiyun.com/zhaoyanjun6/article/details/54581478 本文出自【赵彦军的博客】 Java File类的功能非常强大,利用java基本上可以对文件进行所有操作。 首先来看File类的构造函数的源码 /** * Internal constructor for already-normalized pat...

weixin_34378045的博客 108

【C++】lambda表达式

本篇文章我们要学习的是lambda表达式,它是C++11新增的一种特别方便的语法,相较于之前的函数指针以及仿函数都方便了不少,下面就让我们一起来学习吧。

malloc不出对象的博客 2633

临时文件的使用

临时文件的使用 1、创建临时文件 File tempFile = File.createTempFile(prefix, suffix); prefix :文件名的前缀 suffix :文件名的后缀 注意: 当你运行上面一行代码时,会创建一个空的文件 使用场景: 例如: 你后端接收前端传来的文件MultipartFile file 需要把这个文件生成预览上传到OSS上,把这个文件保存到临时文件使用后删除。 file.transferTo(tempFile.getAbsoluteFile()); 2、

weixin_44689277的博客 1619

手把手教你用FreeModbus RTU协议栈实现STM32与PLC通信(附Keil工程)

本文详细介绍了如何使用FreeModbus RTU协议栈在STM32上实现与PLC的工业通信。从环境搭建、工程配置到关键移植接口实现,提供了Keil工程模板和调试技巧,帮助开发者快速掌握Modbus RTU通信技术,适用于工业自动化领域的设备间通信需求。

weixin_42549037的博客 419

保姆级教程:用YOLOv8n模型在RKNN和Horizon平台完成目标检测(附C++部署代码与性能实测)

本文提供了一份详尽的YOLOv8n模型在RKNN和Horizon平台上的部署指南,涵盖环境配置、模型优化、跨平台适配及性能调优。通过实战案例和C++部署代码,帮助开发者在嵌入式设备上高效实现目标检测,显著提升推理性能。

weixin_42659194的博客 469

file java详解_java File详解

一、简介File类是“文件”和“目录名”的抽象表示形式。因此在java语言中,File类既可以表示文件也可以表示目录。尽管java.io定义的大多数类是实行流式操作的,而File类则不是,它没有指定信息怎样从文件读取或向文件存储。File描述了文件本身的属性,直接处理文件和文件系统,是唯一一个与文件本身有关的操作。1.1 File的继承与实现接口File 直接继承于Object,实现了Serial...

weixin_39867208的博客 701

YOLOV8主干改进方法:C2f-Dual——DualConv轻量化双卷积设计(附改进代码)

DualConv是一种创新的卷积网络结构,旨在构建轻量级的深度神经网络。它通过结合3×3和1×1的卷积核处理相同的输入特征映射通道,优化了信息处理和特征提取。DualConv利用组卷积技术高效排列卷积滤波器,大大降低了计算成本和参数数量。

m0_74890428的博客 4702

JDK1.8源码学习--io包(File)

前言 月是一轮明镜,晶莹剔透,代表着一张白纸(啥也不懂) 央是一片海洋,海乃百川,代表着一块海绵(吸纳万物) 泽是一柄利剑,千锤百炼,代表着千百锤炼(输入输出) 月央泽,学习的一种过程,从白纸->吸收各种知识->不断输入输出变成自己的内容 希望大家一起坚持这个过程,也同样希望大家最终都能从零到零,把知识从薄变厚,再由厚变薄! 一.File的作用: 直接看源码注释(我的翻译可能不太准,如果道友们有更棒的理解,可以留言或者私信) /** * An abstr...

w5_Silence的博客 840

【ARFoundation自学04】AR Tracked Image 图像追踪识别与对应类的调用

1.添加组件 2.创建图像识别库 3.创建识别后追踪的物体(UI、模型等)

leoysq的博客 2560

WPF实战案例 | C# WPF实现大学选课系统

WPF实战案例_C# WPF实现大学选课系统, C# WPF实现大学选课系统,大作业,毕业设计,该系统实现多身份登录、修改密码、课程的管理,个人信息的管理等功能。这个源码让你快速搭建自己的应用程序,注释完整,代码规范,各种风格都有,代码上手简单,代码独立,可以直接用程序打开,运行使用。也可直接点击EXE运行程序。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 4625

JAVA mac系统io文件流,JAVA常用包之java.io文件流

学习目的了解java.io包的含义和作用学会查看java.io包下类和接口的底层源码实现掌握java.io包的常用接口和类,以及常用的方法四、java.io概念通过数据流、序列化和文件系统提供系统输入和输出。流是java文件和计算机底层进行交流的一种方式、形式,计算机只认识二进制的补码。流的形式写入:硬盘文件-->译成字节码-->二进制流-->字节解码-->内存文件写出:内...

weixin_39925031的博客 401

基于深度学习的生活垃圾检测与分类系统:YOLOv5、YOLOv6、YOLOv7与YOLOv8实现

本项目的目标是开发一个基于YOLO系列模型的生活垃圾检测与分类系统。数据集准备与标注:收集并标注生活垃圾图像数据集。模型训练:使用YOLOv5、YOLOv6、YOLOv7、YOLOv8等深度学习模型训练垃圾分类系统。UI界面设计:设计一个简洁的用户界面,支持图像或视频的上传与垃圾分类结果展示。实时垃圾检测与分类:通过摄像头进行实时垃圾检测和分类。结果展示与保存:展示检测结果,支持用户保存分类后的图像或视频。

YOLO 705

JAVA学习63_ Java 日看一类(15)之IO包中的File类

File是相当常用的一个类,也是IO包中的基础类之一 File类引用了如下包 import java.net.URI; import java.net.URL; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.util.List; import java.util.Ar...

wang_zhenwei的博客 454

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

第15章 File类与IO流

File类及本章下的各种流,都定义在java.io包下。一个File对象代表硬盘或网络中可能存在的一个文件或者文件目录(俗称文件夹),与平台无关。(体会万事万物皆对象)File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。File对象可以作为参数传递给流的构造器。想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。

qq_43117059的博客 1089

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

JAVA基础学习笔记-day15-File类与IO流

File类及本章下的各种流,都定义在java.io包下。一个File对象代表硬盘或网络中可能存在的一个文件或者文件目录(俗称文件夹),与平台无关。(体会万事万物皆对象)File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。File对象可以作为参数传递给流的构造器。想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。

天才在左,疯子在右,而我只想做个不折不扣的乐天派 1169

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

java中的io流

流是一种抽象概念,它代表了数据的无结构化传递。按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列。从流中取得数据的操作称为提取操作,而向流中添加数据的操作称为插入操作。用来进行输入输出操作的流就称为IO流。换句话说,IO流就是以流的方式进行输入输出 。

m0_66689823的博客 1万+

java.io包的file_java io包中File类

java.io包中File类发表于2011-8-24 15:49:00 点击率:204人次File类是IO包中唯一代表磁盘文件本身的对象,File类定义了一些与平台无关的方法来操纵文件,通过调用File类提供的各种方法,能够完成创建、删除文件,重命名文件,判断文件的读写权限及是否存在,设置和查询文件的最近修改时间等操作。Java能正确处理UNIX和Windows/DOS约定路径分隔符。如果在Win...

weixin_28753647的博客 1137

​Java基础复习笔记 ​第15章:File类与IO流#​

流的分类流的流向:输入流、输出流操作的数据单位:字节流、字符流角色的不同:节点流、处理流4个抽象基类整个流这一章涉及到的具体的流的使用,操作的步骤都是标准规范的。步骤1:创建File的对象步骤2:创建流的对象,构造器中需要传入File的对象步骤3:读取、写出操作的细节步骤4:关闭资源。

2302_80888278的博客 2098

Java_第15章_File类与IO流

File类及本章下的各种流,都定义在java.io包下。一个File对象代表硬盘或网络中可能存在的一个文件或者文件目录(俗称文件夹),与平台无关。(体会万事万物皆对象)File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。File对象可以作为参数传递给流的构造器。想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。

qq_52922453的博客 979

第15章:File类与IO流

IO流的分类流向:输入流、输出流处理数据单位:字节流、字符流流的角色:节点流、处理流IO的4个抽象基类:InputStream \ OutputStream \ Reader \ Writer略内部提供了一个数组,将读取或要写出的数据,现在此数组中缓存。达到一定程度时,集中性的写出。作用:减少与磁盘的交互,进而提升读写效率。对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。

weixin_65644655的博客 675

Java的File类与IO流

File类及本章下的各种流,都定义在java.io包下。一个File对象代表硬盘或网络中可能存在的一个文件或者文件目录(俗称文件夹),与平台无关。(体会万事万物皆对象)File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。File对象可以作为参数传递给流的构造器。想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录。

qq_63088307的博客 1022

java之IO篇——File、字节流、字符流

IO流是用于读写文件中的数据,要读写文件之前可以创建文件获取文件对象再创建IO流,正文会先介绍File类,通过File类的构造方法获取文件的对象,创建文件或目录以及File类的一些方法获取文件对象的属性。后面还介绍了相关的IO流体系,字节流和字符流的区别用法。目录前言一、File1.常用的成员方法(1) 判断、获取(2)创建、删除(3)获取并遍历2. 实操(1)遍历C盘下的以.avi结尾的文件(2)删除一个多级文件夹(里面的有些文件有内容)(3)统计各种文件的数量二、IO流分类三、IO体系1.字节流。

2202_75483664的博客 1710

Day15——File类与IO流

自定义类需要实现接口:Serializable。要求自定义类声明一个全局常量:static final long serialVersionUID=42234234L;用来唯一标识当前类。如果不声明全局常量serialVersionUID,系统会自动声明一个针对于当前类的serialVersionUID。但是修改此类时,会导致serialVersionUID变化,进而导致反序列化时,出现InvaliedClassException异常。

qq_43069447的博客 474

第15章_File类与IO流(File类的使用,FileReader,FileWriter,FileInputStream,FileOutputStream,缓冲流,转换流,数据流,对象流,标准输入)

File类与IO流(File类的使用,FileReader,FileWriter,FileInputStream,FileOutputStream,缓冲流,转换流,数据流,对象流,标准输入、输出流)

小丁的博客 1174

Day15:File类、IO字节流

一、File类 File类 -- 表示文件或文件夹,不能对文件里的数据进行操作。 File类是IO包中唯一代表磁盘文件本身的对象。File类定义了一些与平台无关的方法来操纵文件,通过调用File类提供的各种方法,能够完成创建、删除文件,重命名文件,判断文件的读写权限及文件是否存在,设置和查询文件的最近修改时间等操作。 //创建文件对象 File file = new File("E:\\J...

亲爱的瑶的博客 452
上一篇: Java 日看一类(14)之IO包中的EOFException异常类和ExpiringCache内部类
下一篇: Java 日看一类(16) 之IO包中的FileDescriptor类
wyf95278
博客等级 码龄10年 38粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值