• 深入浅出java.io.File


    构造函数

    公有

    public File(File parent, String child)
    public File(String pathname)
    public File(String parent, String child)
    public File(URI uri)
    
    • 1
    • 2
    • 3
    • 4

    用法

    	File file = new File("D://a.txt");
    
    • 1

    私有

    private File(String child, File parent)
    private File(String pathname, int prefixLength)
    
    • 1
    • 2

    方法

    公有

    可读、可写、可执行。

    	public boolean canExecute()
    	public boolean canRead()
    	public boolean canWrite()
    
    • 1
    • 2
    • 3

    创建文件

    public boolean createNewFile() throws IOException
    
    • 1

    创建指定前缀后缀文件

    public static File createTempFile(String prefix, String suffix)
    public static File createTempFile(String prefix, String suffix, File directory)
    
    • 1
    • 2

    案例

            File tempFile = File.createTempFile("prefix", "suffix");
            System.out.println(tempFile.getAbsolutePath());
            File dir = new File("D://");
            File tempFile2 = File.createTempFile("prefix", "suffix", dir);
            System.out.println(tempFile2.getAbsolutePath());
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出

    C:\Users\admin\AppData\Local\Temp\prefix3726467976143431355suffix
    D:\prefix3154427126601248567suffix
    
    • 1
    • 2

    删除文件

    public boolean delete()
    public void deleteOnExit()
    
    • 1
    • 2

    是否存在

    public boolean exists()
    
    • 1

    上次修改

    public long lastModified()
    
    • 1

    文件长度

    public long length() 
    
    • 1

    list

    public String[] list()
    public String[] list(FilenameFilter filter)
    public File[] listFiles()
    public File[] listFiles(FileFilter filter) 
    public File[] listFiles(FilenameFilter filter)
    public static File[] listRoots()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建文件夹

    public boolean mkdir()
    public boolean mkdirs() 
    
    • 1
    • 2
    public boolean renameTo(File dest)
    
    • 1
    public boolean setExecutable(boolean executable)
    public boolean setExecutable(boolean executable, boolean ownerOnly)
    public boolean setLastModified(long time)
    public boolean setReadable(boolean readable)
    public boolean setReadable(boolean readable, boolean ownerOnly)
    public boolean setReadOnly()
    public boolean setWritable(boolean writable)
    public boolean setWritable(boolean writable, boolean ownerOnly)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    public Path toPath() 
    public URI toURI()
    public URL toURL() throws MalformedURLException
    
    • 1
    • 2
    • 3

    字段

    私有

        private static final long PATH_OFFSET;
        private static final long PREFIX_LENGTH_OFFSET;
        private static final sun.misc.Unsafe UNSAFE;
    	private transient PathStatus status = null;
    	private static final FileSystem fs = DefaultFileSystem.getFileSystem();
    	private volatile transient Path filePath;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    公有

    从文件系统中获得分隔符。

        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;
    
    • 1
    • 2
    • 3
    • 4

    静态代码块

        static {
            try {
                sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
                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);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    枚举

        private static enum PathStatus { INVALID, CHECKED };
    
    • 1

    内部类

        private static class TempDirectory {
            private TempDirectory() { }
    
            // temporary directory location
            private static final File tmpdir = new File(AccessController
                .doPrivileged(new GetPropertyAction("java.io.tmpdir")));
            static File location() {
                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();
                if (n == Long.MIN_VALUE) {
                    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;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    实战

  • 相关阅读:
    【每天一个cmake技巧】简单的cmake demo
    js 节流函数
    《计算机操作系统-第三章》之中断与系统调用
    645仪表以JSON格式上发方法
    资本方介入的第三方新能源充电桩平台到底“香”在哪里?
    spring-boot自定义网站头像(favicon.ico文件)
    如何使用.NET在2.2秒内处理10亿行数据(1brc挑战)
    银行卡四要素API接口的验证流程
    Haskell中的数据交换:通过http-conduit发送JSON请求
    指针笔试题
  • 原文地址:https://blog.csdn.net/qq_37151886/article/details/127768920