Input/Output and Streams in Java
Alex Liu
Software Developer Engineer
File může představovat soubor i adresářmkdir() pro adresáře, createNewFile() pro soubory)
Fileimport java.io.File;
File reprezentující adresářFile newDirectory = new File("myDirectory");
.mkdir() vytvoří skutečný adresář v souborovém systémuboolean created = newDirectory.mkdir();
true, pokud byl adresář úspěšně vytvořenfalse, pokud adresář již existuje nebo jej nelze vytvořit.listFiles() vypíše soubory v adresáři$$
// Creates a `File` object representing the directory `myDirectory`
File dir = new File("myDirectory");
//Retrieves an `array` of `File` objects representing the contents of `myDirectory`
File[] files = dir.listFiles();
array) objektů File, pokud adresář existujenull, pokud adresář neexistuje// Creates a `File` object for `sample.txt` inside `myDirectory`
File file = new File("myDirectory/sample.txt");
// Retrieves the relative path of the file as a String
String path = file.getPath();
System.out.println(path);
Předpokládáme, že myDirectory již existuje; jinak vytvoření nebo zápis souboru selže
Výstup:
myDirectory/sample.txt
$$
// Creates a `File` object for `sample.txt` inside `myDirectory`
File file = new File("myDirectory/sample.txt");
// Retrieves the absolute path of the file as a String
String absPath = file.getAbsolutePath();
System.out.println(absPath);
/user/home/myDirectory/sample.txt
Input/Output and Streams in Java