This document discusses the File class in Java and methods for working with files and directories. It describes the File class's constructors and methods for getting file attributes, checking permissions, renaming, deleting and creating files/directories. It also introduces the FileFilter and FilenameFilter interfaces for filtering files, and provides examples of their use. Finally, it discusses working with dates using the Date and GregorianCalendar classes.
2. File
● public static final char separatorChar
● public static final String separator
● public static final char pathSeparatorChar
● public static final String pathSeparator
● public File(String pathname)
● public File(String parent, String child)
● public File(File parent, String child)
● public File(URI uri)
● public String getName()
● public String getParent()
● public File getParentFile()
● public String getPath()
● public boolean isAbsolute()
● public String getAbsolutePath()
● public File getAbsoluteFile()
● public String getCanonicalPath() throws IOException
● public File getCanonicalFile() throws IOException
3. File (продолжение)
● public boolean canRead()
● public boolean canWrite()
● public boolean exists()
● public boolean isDirectory()
● public boolean isFile()
● public boolean isHidden()
● public long lastModified()
● public long length()
● public boolean createNewFile() throws IOException
● public boolean delete()
● public String[] list()
● public String[] list(FilenameFilter filter)
● public File[] listFiles()
● public File[] listFiles(FilenameFilter filter)
● public File[] listFiles(FileFilter filter)
● public boolean mkdir()
● public boolean mkdirs()
● public boolean renameTo(File dest)
4. File (продолжение)
● public boolean setReadOnly()
● public boolean setWritable(boolean writable, boolean
ownerOnly)
● public boolean setWritable(boolean writable)
● public boolean setReadable(boolean readable, boolean
ownerOnly)
● public boolean setReadable(boolean readable)
● public boolean setExecutable(boolean executable, boolean
ownerOnly)
● public boolean setExecutable(boolean executable)
● public boolean canExecute()
● public long getTotalSpace()
● public long getFreeSpace()
● public long getUsableSpace()
● public static File createTempFile(String prefix, String suffix,
File directory)
● public static File createTempFile(String prefix, String suffix)
throws IOException
5. Пример использования
● private File f;
private Date d;
private File f1;
public FileTestClass(String name, String name2)
{
f1=new File(name);
d=new Date(f1.lastModified());
f=new File(name2);
}
public String[] outAllFiles()
{
File[] fs=f.listFiles(new MyFilter(d));
String[] ss=new String[fs.length];
for(int i=0;i<fs.length;++i)
ss[i]=fs[i].getPath();
return ss;
}
7. Пример использования
● public class MyFilter implements FileFilter{
private Date date;
public MyFilter(Date date)
{
this.date=date;
}
public boolean accept(File pathname) {
Date date1 = new Date(pathname.lastModified());
return (date.getTime()>=date1.getTime());
}
}
9. Работа со временем
● class Date
● public Date(long date)
● class GregorianCalendar
● public GregorianCalendar(int year, int month, int
dayOfMonth, int hourOfDay, int minute, int second)
● public final Date getTime()
● public final void setTime(Date date)