Java/JDK 6/File

Материал из Java эксперт
Версия от 06:12, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create a file and change its attribute to readonly

 
import java.io.File;
import java.io.IOException;
public class FileAttributesDemo {
  public static void main(String[] args) throws IOException {
    // Create a new file, by default canWrite=true, 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());
  }
}





Creates a file and sets it to read-only.

 
import java.io.File;
import java.io.IOException;
public class FileAttributesDemo {
    public static void main (String[] args) throws IOException {
        // Create a new file, by default canWrite=true,
       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 Class Enhancements

 
import java.io.File;
public class DiskSpaceDemo {
  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");
  }
}
//Total space on C: = 40015953920bytes
//Free space on C: = 6470483968bytes





Get the free space

 
import java.io.File;
public class SpaceChecker {
  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();
    }
  }
}





Get the total space

 

import java.io.File;
public class SpaceChecker {
  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("Total space = " + roots[i].getTotalSpace());
      System.out.println();
    }
  }
}





Get the usable space

 
import java.io.File;
public class SpaceChecker {
  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("Usable space = " + roots[i].getUsableSpace());
      System.out.println();
    }
  }
}





Getting a Proper URL from a File Object

 
import java.io.*;
import java.net.*;
public class FileURL {
  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);
  }
}





List all roots

 
import java.io.File;
public class SpaceChecker {
  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();
    }
  }
}