ืืื ืืืืงืช Java File
ืืืืืงื File ืืืืืจืช ืืืืืืช java.io ืืืื ืื ืขืืืืช ืืฉืืจืืช ืขื ืืจืืื. ืืืืงืช Java File ืืื ืื ืื ืืืืข ืขื ืงืืฆืื ืืกืคืจืืืช. ืืคื ืฉืืชื ืืืืข, ืืจืืช ืืขืจืืช ืืืคืขืื, ืงืืฆืื ืืกืคืจืืืช ืื ืืืืืืงืืื ืฉืื ืื. ืขื ืืืช ื-Java, ืฉื ืืื ืืชืืืจืื ืขื ืืื ืืืชื ืืืืงื File . ืืืื ืืชื ืชืืื ืืืืข ืื ืคืฉืื ืืืฉืชืืฉ ืืืืจืืืืช ืืื ืืืคื ืืช ืืงืืฆืื? ืงืืื ืื, ืื ืืืืฉื ืืงืืฆืื ืฉืื ื ืืืขืจืืืช ืืคืขืื ืฉืื ืืช.ืืื ื ืืืืงืืช ืงืืฆืื
ืชืืื ืื ืืืืืืืงื Java.io.File Class ืฆืจืื ืืืืฆื ืงืืืฅ ืื ืกืคืจืื, ื ืืื ืืืฉืชืืฉ ืืืื ืืืื ืืื ืืื ืืืฆืืจ ืืช ืืืืืืืงื: File(String pathname) ืืืงืจื ืื constructor ืืืฆืจ ืืืคืข ืืืฉ ืฉื File ืขื ืืื ืืืจืช ืฉื ืื ืชืื ืื ืชืื ืืืจืืืช ืืฉื ื ืชืื ืืืคืฉื. File(String myPath, String myString) ืื ืื ืื ืืืฆืจ ืืืคืข ืงืืืฅ ืืืฉ ืืืืจืืืช ืฉื ื ืชืื myPath ืืืืืจืืืช ืฉื ื ืชืื myString. ืงืืืฅ (ืืืจื ืฉื ืงืืืฅ, ืฉื ืืืจืืืช) ืืืฆืจ ืืืคืข ืืืฉ ืฉื ืงืืืฅ ืืชืื ืฉื ื ืชืื ืืืคืฉื ืฉื ืงืืืฅ ืืืืืจืืืช ืฉื ื ืชืื ืฉื. File(URI uri) ืืืงืจื ืื ืื ืื ืืืฆืจ ืืืคืข ืงืืืฅ ืืืฉ ืขื ืืื ืืืจืช ืืงืืืฅ ืื ืชืื: URI ืืฉื ื ืชืื ืืืคืฉื. URI ืืื ืืืืงืช Java ืืืืืฆืืช ืืคื ืื ืืืืืื ืืฉืืืื ืืืื (URI). ืื ื ืืืืื ืืื ืืื ืฉืื ืื ืฉื Java.io.File Class:import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
public class FileTest {
public static void main(String[] args) throws URISyntaxException {
File myFile1 = new File("d:\\MyCat");
File myFile2 = new File ("d:\\MyCat", "cat.txt");
File myFile3 = new File(myFile1, "cat.txt");
URI uri = new URI ("https://docs.oracle.com/javase/7/docs/api/java/net/URI.html");
File myFile4 = new File(uri);
}
}
ืฉืืืืช ืืืืืงืช ืงืืฆืื
ืืืืืงืช Java File ืืฉ ืืกืคืจ ืฉืืืืช ืืืืคืฉืจืืช ืื ืืชืคืขื ืงืืฆืื ืืกืคืจืืืช. ืืืื ื ืฉืงืื ืืื ืืื:-
boolean createNewFile() ืืืฆืจ ืงืืืฅ ืืืฉ ืื ืชืื ืืืืขืืจ ืืื ืื. ืืืืืจื True ืื ืืฆืืื, ืืืจืช False
-
boolean delete() ืืืืง ืกืคืจืืื ืื ืงืืืฅ ืื ืชืื ืืืืขืืจ ืืื ืื. ืืืืืจื ืืืช ืืืืจ ืืืืงื ืืืฆืืืช
-
boolean exists() ืืืืง ืื ืงืืืฅ ืื ืกืคืจืื ืงืืืืื ืื ืชืื ืฉืฆืืื ืืื ืื. ืื ืืงืืืฅ ืื ืืกืคืจืืื ืงืืืืื, ืืืืืจื true, ืืืจืช ืืืืืจื false
-
ืืืจืืืช getAbsolutePath() ืืืืืจื ืืช ืื ืชืื ืืืืืื ืขืืืจ ืื ืชืื ืฉืขืืจ ืืื ืื ืืืืืืืงื
-
ืืืจืืืช getName() ืืืืืจื ืืช ืืฉื ืืงืฆืจ ืฉื ืงืืืฅ ืื ืกืคืจืื
-
ืืืจืืืช getParent() ืืืืืจื ืืช ืืฉื ืฉื ืกืคืจืืืช ืืื
-
boolean isDirectory() ืืืืืจื true ืื ืื ืชืื ืื ืชืื ืืืื ืกืคืจืืื
-
boolean isFile() ืืืืืจื true ืื ืืฉ ืงืืืฅ ืื ืชืื ืื ืชืื
-
boolean isHidden() ืืืืืจื true ืื ืืกืคืจืืื ืื ืืงืืืฅ ืืืกืชืจืื
-
long length() ืืืืืจ ืืช ืืืื ืืงืืืฅ ืืืชืื
-
long lastModified() ืืืืืจื ืืช ืืืื ืฉืื ืงืืืฅ ืื ืกืคืจืื ืฉืื ื ืืืืจืื ื. ืืขืจื ืืืืฆื ืืช ืืกืคืจ ืืืืคืืืช ืืฉื ืืืช ืฉืืืคื ืืื ืขืืื ืืื ืืงืก
-
String[] list() ืืืืืจื ืืขืจื ืฉื ืงืืฆืื ืืกืคืจืืืช ืืฉื ื ืฉื ืืฆืืื ืืกืคืจืืื ืกืคืฆืืคืืช
-
File[] listFiles() ืืืืืจ ืืขืจื ืฉื ืงืืฆืื ืืชืชื ืกืคืจืืืช ืฉื ืืฆืืื ืืกืคืจืืื ืกืคืฆืืคืืช
-
boolean mkdir() ืืืฆืจ ืกืคืจืืื ืืืฉื ืืืืืืจ true ืื ืืฆืืื
-
boolean renameTo(File dest) ืืฉื ื ืฉื ืงืืืฅ ืื ืกืคืจืื
ืืื ืชืืื ืืช ืฉื ืืืืงืช Java File
-
ืื ืชืื, ืืืคืฉื ืื ืืืจืืืช, ืืืื ืืืืืช ืืืืื ืื ืืืกื. ื ืืชื ืืืืืจ ืืช ืืื ืฉื ื ืชืื ืืืคืฉื ืขื ืืื ืงืจืืื ืืืชืืื getParent() ืฉื ืืืชื ืืืืงื.
-
ืจืืฉืืช ืขืืื ื ืืืฆืืจ ืืืืืืงื ืฉื ืืืืืงื File , ืืืขืืืจ ืื ืฉื ืฉื ืงืืืฅ ืื ืกืคืจืืื. ืืขืจืืช ืืงืืฆืื ืืืืื ืืืืื ืืืืืืช ืขื ืคืขืืืืช ืืกืืืืืช ืขื ืืืืืืงื ืืขืจืืช ืืงืืฆืื ืืคืืขื, ืืืื ืงืจืืื, ืืชืืื ืืืืฆืืข. ืืืืืืช ืืื ื ืงืจืืืช ืืจืฉืืืช ืืืฉื.
-
ืืืคืขืื ืฉื ืืืืืงื File ืืื ื ื ืืชื ืื ืืฉืื ืื. ืื ืืืืจ ืฉืืจืืข ืฉืืชื ืืืฆืจ ืงืืืฅ, ืื ืชืื ืืืืคืฉื ืืืืืฆื ืขื ืืื ืืืืืืงื ืืงืืืฅ ืืขืืื ืื ืืฉืชื ื.
ืืืืืืืช ืืงืื ืืืชืชื ืงืืืฅ
ืืืื ื ืืฆืืจ ืชืืื ืืช ืฉืขืืืืช ืขื ืกืคืจืืืช. ืงืืื ืื ืื ืฆืจืื ืืืฆืืจ ืกืคืจืืื ืื ืชืื ืื ืชืื ืืื ืืืฆืืจ ืงืืืฅ ืืืฉ ืืืืืืง ืื ืืงืืืฅ ืืืกืคืจืืื ืงืืืืื.import java.io.File;
import java.io.IOException;
public class FileTest2 {
public static void main(String[] args) throws IOException {
//create a directory using mkdir() File class method
File dir = new File("d:\\MyDir");
boolean created = dir.mkdir();
if(created)
System.out.println("Folder has been created...");
else
System.out.println("Folder hasn't been created...");
File myFile = new File("d:\\MyDir\\cat.txt");
myFile.createNewFile();
System.out.println("File name: " + myFile.getName());
System.out.println("Parent folder: " + myFile.getParent());
if(myFile.exists())
System.out.println("File exists");
else
System.out.println("File not found");
}
}
ืื ื ืืคืื:
import java.io.File;
public class FileTest3 {
public static void main(String[] args) {
File dir = new File("d:\\MyDir");
File myFile = new File("d:\\MyDir\\cat.txt");
System.out.println("File name: " + myFile.getName());
System.out.println("Parent folder: " + myFile.getParent());
if (myFile.exists())
System.out.println("File exists");
else
System.out.println("File not found");
System.out.println("Absolute path: " + myFile.getAbsolutePath());
if (myFile.exists()) {
System.out.println("Is writable: " + myFile.canWrite());
System.out.println("Is readable: " + myFile.canRead());
System.out.println("Is a directory: " + myFile.isDirectory());
System.out.println("myFile Size in bytes = " + myFile.length());
}
}
}
ืืคืื ืฉื ืืชืืื ืืช ืืื:
GO TO FULL VERSION