File类里面有两个方法可以实现:
一个是mkdir():创建此抽象路径名指定的目录.
另外一个是mkdirs(): 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录.
比如你想在A文件夹创建一个B文件夹,并在B文件夹下创建c和D文件夹,可以用下面的代码实现:
import java.io.File;
public class Test {
public static void main(String args[]) {
File file = new File("D:\\A\\B\\C");
file.mkdirs();
file = new File("D:\\A\\B\\D");
file.mkdir();
}
java
是跨平台的开发语言,建立文件夹的方式是一样的
File
file
=
new
File("/usr/local/java");
这样就行了
具体的创建方法参照下面的实例:
public class FileTest {
public?static?void?main(String[]?args)?{
//?根据系统的实际情况选择目录分隔符(windows下是,linux下是/)
String?separator?=?File.separator;
//?以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的
//?linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用\来表示反斜杠)
String?fileName?=?"myFile.txt";
//?在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件
File?f?=?new?File(directory,fileName);
if(f.exists())?{
//?文件已经存在,输出文件的java添加文件夹的代码相关咨询
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
}?else?{
//?先创建文件所在的目录
f.getParentFile().mkdirs();
try?{
//?创建新文件
f.createNewFile();
}?catch?(IOException?e)?{
System.out.println("创建新文件时出现了错误...");
e.printStackTrace();
参考下面代码,说明已在代码中注释:
import?java.io.File;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.text.SimpleDateFormat;
import?java.util.Date;
public?class?WriteFile?{
writeFile();
public?static?void?writeFile(){
SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
String?content?=?sdf.format(new?Date());
System.out.println("现在时间:"?+?content);
FileOutputStream?out?=?null;
File?file;
String?rootFile?=?"D:\\tests\\license";
file?=?new?File(rootFile);
if?(!file.exists())?{
/*
file.mkdirs():创建没有存在的所有文件夹
file.mkdir():创建没有存在的最后一层文件夹
例如:在硬盘上有D://test?文件夹,但是现在需要创建D://test//license//save,这个时候就需要使用file.mkdirs()而不能使用file.mkdir(),另外这两个方法都是仅仅能创建文件夹,不能创建文件,即使创建D://test//license//save//systemTime.dat如果使用该方法创建的SystemTime.dat也是一个文件夹?,而不是文件
*/
File?fileDat?=?new?File(rootFile?+?"\\systemFile.dat");
if(!fileDat.exists()){
//创建文件?不是文件夹,在程序中这这一步没有必要,因为
new?FileOutputStream(fileDat);该语句有创建文件的功能
fileDat.createNewFile();//
out?=?new?FileOutputStream(fileDat);
byte[]?contentInBytes?=?content.getBytes();
out.write(contentInBytes);
out.flush();
out.close();
System.out.println("Done");
}?finally?{
if?(out?!=?null)?{
Java项目添加文件夹选项的意思是:你的java代码写的不全,必须要把空文件夹的情况写入才行.
java项目创建包以及调试运行的方法:
①.、首先我们在桌面找到eclipse,双击将其打开.
①.0、此时此刻呢我们可以看到'测试输出!'的字样在控制台打印出来了.这样一个完整的java调试就结束了.
public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件.
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
in.close();
} catch (IOException e) {
return;
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
} catch (IOException e1) {
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
public static void readFileByChars(String fileName) {
Reader reader = null;
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行.
// 但如果这两个字符分开显示时,会换两次行.
// 所以呢,屏蔽掉\r,或者屏蔽\n.否则,将会多出很多空行.
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
reader.close();
} catch (Exception e) {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
(tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i charread; i++) {
if (tempchars[i] == '\r') {
continue;
System.out.print(tempchars[i]);
if (reader != null) {
* 以行为单位读取文件,常用于读面向行的格式化文件
public static void readFileByLines(String fileName) {
BufferedReader reader = null;
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
* 随机读取文件内容
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
// 将读文件的开始位置移到beginIndex位置.
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节.
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
if (randomFile != null) {
randomFile.close();
* 显示输入流中还剩的字节数
private static void showAvailableBytes(InputStream in) {
System.out.println("当前字节输入流中的字节数为:" + in.available());
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
复制代码
public class AppendToFile {
* A方法追加文件:使用RandomAccessFile
public static void appendMethodA(String fileName, String content) {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
//将写文件指针移到文件尾.
randomFile.seek(fileLength);
randomFile.writeBytes(content);
* B方法追加文件:使用FileWriter
public static void appendMethodB(String fileName, String content) {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
String content = "new append!";
//按方法A追加文件
AppendToFile.appendMethodA(fileName, content);
AppendToFile.appendMethodA(fileName, "append end. \n");
//显示文件内容
//按方法B追加文件
AppendToFile.appendMethodB(fileName, content);
AppendToFile.appendMethodB(fileName, "append end. \n");
以上就是土嘎嘎小编为大家整理的java添加文件夹的代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!