[Java的] 创建的文件, 在Java目录
在项目中创建文件夹 :
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
随着第一个订单,我们只宣告但尚未建立, 创建 1 文件夹必须添加的第二顺序.
在任何路径创建文件夹:
File directory_2 = new File(directory_1, "new1"); // tao thu muc new1 trong thu muc new vua tao directory_2.mkdir();
创建项目文件:
File file = new File("test.txt"); // nam trong project
try {
file.createNewFile(); // tao file test.txt
} catch (IOException e) {
e.printStackTrace();
}
在创建文件时可能出现少数例外,我们必须把订单 file.createNewFile(); 仲尝试捕捉.
在目录中创建文件 任何类似的创建目录, 我们只需要添加在声明文件的路径.
艺术品代码:
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();
}
}
}
因为我用Ubuntu是如此坎坷 /media/quan/DATA/…. 如果您使用的是Windows路径可能 Ç:foldertext.txt
要获得文件夹的路径,你可以看到所有的指示:
获取Java中的文件路径 - 在Java中获取文件路径
在Ubuntu文件夹路径 - 在Ubuntu的路径文件夹
与Windows: 以上的链接位置clickphai鼠标选择属性



最新评论