构造函数
公有
public File(File parent, String child)
public File(String pathname)
public File(String parent, String child)
public File(URI uri)
用法
File file = new File("D://a.txt");
私有
private File(String child, File parent)
private File(String pathname, int prefixLength)
方法
公有
可读、可写、可执行。
public boolean canExecute()
public boolean canRead()
public boolean canWrite()
创建文件
public boolean createNewFile() throws IOException
创建指定前缀后缀文件
public static File createTempFile(String prefix, String suffix)
public static File createTempFile(String prefix, String suffix, File directory)
案例
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());
输出
C:\Users\admin\AppData\Local\Temp\prefix3726467976143431355suffix
D:\prefix3154427126601248567suffix
删除文件
public boolean delete()
public void deleteOnExit()
是否存在
public boolean exists()
上次修改
public long lastModified()
文件长度
public long length()
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()
创建文件夹
public boolean mkdir()
public boolean mkdirs()
public boolean renameTo(File dest)
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)
public Path toPath()
public URI toURI()
public URL toURL() throws MalformedURLException
字段
私有
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;
公有
从文件系统中获得分隔符。
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;
静态代码块
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);
}
}
枚举
private static enum PathStatus { INVALID, CHECKED };
内部类
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;
}
}