Java by API/java.io/File

Материал из Java эксперт
Перейти к: навигация, поиск

File: canRead()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: canWrite()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: createNewFile()

  
/*
 */
import java.io.File;
public class MainClass {
  public static void main(String[] args) throws Exception{
    new File("newFile.txt").createNewFile();
  }
}





File: createTempFile(String prefix, String suffix)

  
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class MainClass {
  public static void main(String[] args) {
    try {
      File tempFile = File.createTempFile("myfile", ".tmp");
      FileOutputStream fout = new FileOutputStream(tempFile);
      PrintStream out = new PrintStream(fout);
      out.println("some text");
    } catch (IOException ex) {
      System.out.println("There was a problem creating/writing to the temp file");
      ex.printStackTrace();
    }
  }
}





File: delete()

  
import java.io.File;

/*
 */
public class MainClass {
  public static void main(String[] args) {
    File bkup = new File("Delete.java~");
    bkup.delete();
  }
}





File: deleteOnExit()

  
import java.io.File;
public class Main {
  public static void main(String[] a) {
    File file = new File("c:\\test\\test.txt");
    file.deleteOnExit();
  }
}





File: exists()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: getAbsoluteFile()

  
import java.io.File;
public class Main {
  public static void main(String[] a) {
    File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.separator, "File.java");
    System.out.println(myFile.getAbsoluteFile());
  }
}





File: getAbsolutePath()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: getCanonicalPath()

  
/*
 * Output: 
getCanonicalPath() = C:\winnt
 */
import java.io.File;
public class MainClass {
  public static void main(String args[]) {
    try {
      File file = new File("c:\\winnt");
      System.out.println("getCanonicalPath() = " + file.getCanonicalPath());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





File: getFreeSpace()

  

import java.io.File;
public class Main {
  public static void main(String[] args) {
    File file = new File("C:");
    long totalSpace = file.getTotalSpace();
    System.out.println("Total space on " + file + " = " + totalSpace + "bytes");
    // Check the free space in C:
    long freeSpace = file.getFreeSpace();
    System.out.println("Free space on " + file + " = " + freeSpace + "bytes");
  }
}





File: getName()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: getParent()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: getParentFile()

  
import java.io.File;
public class Main {
  public static void main(String[] a) {
    File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.separator, "File.java");
    System.out.println(myFile.getParentFile());
    System.out.println(myFile);
  }
}





File: getPath()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: getTotalSpace()

  

import java.io.File;
public class Main {
  public static void main(String[] args) {
    File file = new File("C:");
    long totalSpace = file.getTotalSpace();
    System.out.println("Total space on " + file + " = " + totalSpace + "bytes");
    // Check the free space in C:
    long freeSpace = file.getFreeSpace();
    System.out.println("Free space on " + file + " = " + freeSpace + "bytes");
  }
}





File: getUsableSpace()

  

import java.io.File;
public class Main {
  public static void main(String[] args) {
    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
      System.out.println(roots[i]);
      System.out.println("Free space = " + roots[i].getFreeSpace());
      System.out.println("Usable space = " + roots[i].getUsableSpace());
      System.out.println("Total space = " + roots[i].getTotalSpace());
      System.out.println();
    }
  }
}





File: isAbsolute()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: isDirectory()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: isFile()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: isHidden()

  
/*
 * Output:
test.txt is not hidden
 */
import java.io.File;
public class MainClass {
  public static void main(String[] args) {
    // Create an object that is a directory
    File myFile = new File("test.txt");
    System.out.println(myFile + (myFile.isHidden() ? " is" : " is not")
                       + " hidden");
  }
}





File: lastModified()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: length()

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





File: list()

  
/*
 * Output:
 * 
Directory of c:\
 System.FormatException is a file
admin.lnk is a file
admiss.lnk is a file
AUTOEXEC.BAT is a file 
...
...  
 */
import java.io.File;
public class MainClass {
  public static void main(String args[]) {
    String dirname = "c:\\";
    File f1 = new File(dirname);
    if (f1.isDirectory()) {
      System.out.println("Directory of " + dirname);
      String s[] = f1.list();
      for (int i = 0; i < s.length; i++) {
        File f = new File(dirname + "/" + s[i]);
        if (f.isDirectory()) {
          System.out.println(s[i] + " is a directory");
        } else {
          System.out.println(s[i] + " is a file");
        }
      }
    } else {
      System.out.println(dirname + " is not a directory");
    }
  }
}





File: listFiles()

  
import java.io.File;
public class Main {
  public static void main(String[] a) {
    File myFile = new File("C:" + File.separator);
    for(File s: myFile.listFiles()){
      System.out.println(s);
    }
  }
}





File: listFiles(FilenameFilter filter)

  
/*
 * Output:
 The 0 matching items in the directory, , are:
 */
import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;
public class MainClass {
  public static void main(String[] args) {
    File myDir = new File("C:/");
    // Define a filter for java source files beginning with F
    FilenameFilter select = new FileListFilter("F", "java");
    File[] contents = myDir.listFiles(select);
    if (contents != null) {
      System.out.println("\nThe " + contents.length
          + " matching items in the directory, " + myDir.getName()
          + ", are:");
      for (File file : contents) {
        System.out.println(file + " is a "
                + (file.isDirectory() ? "directory" : "file")
                + " last modified on\n"
                + new Date(file.lastModified()));
      }
    } else {
      System.out.println(myDir.getName() + " is not a directory");
    }
    return;
  }
}
class FileListFilter implements FilenameFilter {
  private String name; 
  private String extension; 
  public FileListFilter(String name, String extension) {
    this.name = name;
    this.extension = extension;
  }
  public boolean accept(File directory, String filename) {
    boolean fileOK = true;
    if (name != null) {
      fileOK &= filename.startsWith(name);
    }
    if (extension != null) {
      fileOK &= filename.endsWith("." + extension);
    }
    return fileOK;
  }
}





File: listRoots()

  
import java.io.File;
import javax.swing.JFrame;
public class MainClass extends JFrame {
  public static void main(String[] a) {
    File[] drives = File.listRoots();
    for (int i = 0; i < drives.length; i++)
      System.out.println(drives[i]);
  }
}





File: mkdir()

  
import java.io.File;
/*
 */
public class MainClass {
  public static void main(String[] args) {
    System.out.println(new File("home").mkdir());
  }
}





File: mkdirs() (Creates a directory including any parent directories)

  
import java.io.File;
public class Main {
  public static void main(String[] a) {
    File file = new File("c:\\test\\test\\");
    file.mkdirs();
  }
}





File.pathSeparatorChar

  
/*
 * Output:
pathSeparatorChar = ;  
 */
import java.io.File;
public class MainClass {
  public static void main(String args[]) {
    try {
      System.out.println("pathSeparatorChar = " + File.pathSeparatorChar);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





File: renameTo(File newFile)

  
/*
 * Output:
 *  
 */
import java.io.File;
public class MainClass {
  public static void main(String args[]) {
    try {
      File oldFile = new File(args[0]);
      File newFile = new File(args[1]);
      boolean result = oldFile.renameTo(newFile);
      System.out.println(result);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





File.separator

  
/*
 * Output: 
 
  \
 */
import java.io.File;
public class MainClass {
  public static void main(String args[]) {
    System.out.println(File.separator);
  }
}





File: separatorChar

  
import java.io.File;
class FileDemo {
  public static void main(String args[]) throws Exception {
    // Display constants
    System.out.println("pathSeparatorChar = " + File.pathSeparatorChar);
    System.out.println("separatorChar = " + File.separatorChar);
  
  }
}





File: setLastModified(long time)

 
 
import java.io.File;
import java.util.Date;
public class Main {
  public static void main(String[] args) throws Exception {
    File fileToChange = new File("C:/myfile.txt");
    Date filetime = new Date(fileToChange.lastModified());
    System.out.println(filetime.toString());
    System.out.println(fileToChange.setLastModified(System.currentTimeMillis()));
    filetime = new Date(fileToChange.lastModified());
    System.out.println(filetime.toString());
  }
}





File: setReadOnly()

  
/*
 */
import java.io.File;
public class MainClass {
  public static void main(String[] args) throws Exception {
    File f = new File("f");
    if (!f.setReadOnly()) {
      System.out.println("Grrr! Can"t set file read-only.");
      return;
    }
  }
}





File: setWritable(boolean writable)

  
import java.io.File;
import java.io.IOException;
public class Main {
  public static void main(String[] args) throws IOException {
    // Create a new file, by default canWrite=true,
    boolean readonly = false;
    File file = new File("test.txt");
    if (file.exists()) {
      file.delete();
    }
    file.createNewFile();
    System.out.println("Before. canWrite?" + file.canWrite());
    // set to read-only, atau canWrite = false */
    file.setWritable(false);
    System.out.println("After. canWrite?" + file.canWrite());
  }
}





File: toURI()

  
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
  public static void main(String args[]) throws MalformedURLException {
    File file = new File("The End");
    URL url2 = file.toURI().toURL();
    System.out.printf("Good url %s%n", url2);
  }
}





File: toURL()

  
import java.io.File;
import javax.swing.JFrame;
public class MainClass extends JFrame {
  public static void main(String[] a) throws java.net.MalformedURLException {
    java.net.URL u = new File("GetResource.java").toURL();
    System.out.println(u);
  }
}





new File(String name)

  
/*
 * Output:
 * 
File Name:MainClass.java
Path:MainClass.java
Abs Path:C:\Java_Dev\workspace\cal\MainClass.java
Parent:null
does not exist
is not writeable
is not readable
is a directoryfalse
might be a named pipe
is not absolute
File last modified:0
File size:0 Bytes
 * 
 *  
 */
import java.io.File;
public class MainClass {
    public static void main(String args[]) {
  File f1 = new File("MainClass.java");
  System.out.println("File Name:" + f1.getName());
  System.out.println("Path:" + f1.getPath());
  System.out.println("Abs Path:" + f1.getAbsolutePath());
  System.out.println("Parent:" + f1.getParent());
  System.out.println(f1.exists() ? "exists" : "does not exist");
  System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
  System.out.println(f1.canRead() ? "is readable" : "is not readable");
  System.out.println("is a directory" + f1.isDirectory() );
  System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
  System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
  System.out.println("File last modified:" + f1.lastModified());
  System.out.println("File size:" + f1.length() + " Bytes");
    }
}





new File(String parent, String child)

  
import java.io.File;
public class Main {
  public static void main(String[] a) {
    File myFile = new File("C:" + File.separator + "jdk1.5.0" + File.separator, "File.java");
    System.out.println(myFile.isAbsolute());
  }
}





new File(URI uri)

  
                       
import java.io.File;
import java.net.URI;
public class Main {
  public static void main(String[] a) throws Exception {
    File remoteFile = new File(new URI("file:///index.htm"));
  }
}