Java/JDK 6/File
Содержание
Create a file and change its attribute to readonly
<source lang="java">
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()); }
}
</source>
Creates a file and sets it to read-only.
<source lang="java">
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 ()); }
}
</source>
File Class Enhancements
<source lang="java">
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
</source>
Get the free space
<source lang="java">
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(); } }
}
</source>
Get the total space
<source lang="java">
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(); } }
}
</source>
Get the usable space
<source lang="java">
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(); } }
}
</source>
Getting a Proper URL from a File Object
<source lang="java">
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); }
}
</source>
List all roots
<source lang="java">
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(); } }
}
</source>