[Java] Creat file, directory in Java
Create a folder in the project :
File directory_1 = new File("new"); // khai bao 1 folder ten la new trong project directory_1.mkdir(); // lenh tao folder new tren o dia
With our first order only declared but not yet created, to create 1 folder must write the second order.
Create a folder in the path of any:
File directory_2 = new File(directory_1, "new1"); // tao thu muc new1 trong thu muc new vua tao directory_2.mkdir();
Create a file in the project:
File file = new File("test.txt"); // nam trong project try { file.createNewFile(); // tao file test.txt } catch (IOException e) { e.printStackTrace(); }
When creating the file may be some exceptions, we have to place order file.createNewFile(); trong try catch.
Create a file in the directory any similar Folder, we just need to add the path of the report file.
Intelligent Code flowers:
import java.io.File; import java.io.IOException; public class FileAndDirectory { public static void main(String[] args) { File directory_1 = new File("new"); // khai bao 1 folder ten la new trong project directory_1.mkdir(); // lenh tao folder new tren o dia File directory_2 = new File(directory_1, "new1"); // tao thu muc new1 trong thu muc new vua tao directory_2.mkdir(); //khai bao 3 file nam trong cac thu muc khac nhau File file_1 = new File("test.txt"); // nam trong project File file_2 = new File(directory_1,"test1.txt"); // nam trong directory_2 (new1) File file_3 = new File("/media/quan/DATA/Dropbox/STUDY/PROGRAM/Java/code/JavaBasic/new/new1","test2.txt"); // new la directory1, new1 la directory2 try { file_1.createNewFile(); // tao file test.txt file_2.createNewFile(); // tao file test1.txt file_3.createNewFile(); // tao file test2.txt } catch (IOException e) { e.printStackTrace(); } } }
Do yourself lotion should be used ubuntu /media/quan/DATA/…. If you use windows paths can be C:foldertext.txt
To get the path of the folder you can see all the instructions below:
Get the file path in Java - Get File Path in Java
Directories in Ubuntu - The path folder in Ubuntu
With windows: clickphai click the link and select Properties in the directory location
Recent Comments