重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java添加文件夹的代码,java中如何创建文件夹

如何用JAVA代码创建一个文件夹

File类里面有两个方法可以实现:

创新互联建站长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为建邺企业提供专业的网站建设、成都网站制作建邺网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

一个是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代码在linux系统下建立文件夹

java

是跨平台的开发语言,建立文件夹的方式是一样的

File

file

=

new

File("/usr/local/java");

file.mkdirs();

这样就行了

如何用java在指定目录下创建一个文件夹

具体的创建方法参照下面的实例:

public class FileTest {

public static void main(String[] args) {

// 根据系统的实际情况选择目录分隔符(windows下是,linux下是/)

String separator = File.separator;

String directory = "myDir1" + separator + "myDir2";

// 以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的

// linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用\来表示反斜杠)

// String directory = "myDir1/myDir2";

String fileName = "myFile.txt";

// 在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件

File f = new File(directory,fileName);

if(f.exists()) {

// 文件已经存在,输出文件的相关信息

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();

}

}

}

}

在Java工程下,用java代码创建文件夹

参考下面代码,说明已在代码中注释:

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class WriteFile {

public static void main(String[] args) {

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;

try {

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.mkdirs();

}

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");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (out != null) {

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

Java项目添加文件夹选项是什么意思?

Java项目添加文件夹选项的意思是:你的java代码写的不全,必须要把空文件夹的情况写入才行。

java项目创建包以及调试运行的方法:

1、首先我们在桌面找到eclipse,双击将其打开。

2、在这里我已经创建了一个名为helloworld的java项目,但其下还为创建任何的包以及文件。下边我们开始创建第一个包路径。一般来说java代码都是写在src文件夹下,选中src右击鼠标创建一个包路径,选择new然后选择package。

3、在创建包路径界面,我们需要给我们的包进行命名。命名规则一般为域名.公司名.工程名.模块名……假如需要建立一个百度地图的应用包ditu.baidu.com,那我们的包名即为com.baidu.ditu。命名结束后点击Finish。

4、我们可以看到包的路径已经建好了。

5、接下来我们需要在包里面建立java文件,右击包名称,选择new-〉class。

6、java文件的命名规则为单词首字母大写,如果多个单词则每个首字母都需要大写。在新建java文件页面,我们看到下方有个设置项为public static void main (string[] args),这个选项是对该java类自动创建一个主函数。我们将其选中,并点击Finish。

7、可以看到在新创建的这个类中已经自动创建了一个主函数,以及类的包路径都已经自动的引入。在左侧试图中可以查看到新创建的java类。

8、那eclipse中如何对java工程进行调试呢?这里我们通过一个最简单也是初学者入门就接触的一个实例叫'hell oworld!'。我们在主程序中添加一行代码,也是最常见的输出命令。改行代码的意思为当程序运行时将‘测试输出!’输出到控制台。

9、接下来我们再java类中右击鼠标,选择run as-〉java  application。

10、接下来我们可以看到‘测试输出!’的字样在控制台打印出来了。这样一个完整的java调试就结束了。

Java IO 问题 创建一个新文件夹。 并且写入一个文件

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) {

e.printStackTrace();

return;

}

try {

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) {

try {

in.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以字符为单位读取文件,常用于读文本,数字等类型的文件

*/

public static void readFileByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

try {

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) {

e.printStackTrace();

}

try {

System.out.println("以字符为单位读取文件内容,一次读多个字节:");

// 一次读多个字符

char[] tempchars = new char[30];

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;

} else {

System.out.print(tempchars[i]);

}

}

}

}

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以行为单位读取文件,常用于读面向行的格式化文件

*/

public static void readFileByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

try {

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++;

}

reader.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 随机读取文件内容

*/

public static void readFileByRandomAccess(String fileName) {

RandomAccessFile randomFile = null;

try {

System.out.println("随机读取一段文件内容:");

// 打开一个随机访问文件流,按只读方式

randomFile = new RandomAccessFile(fileName, "r");

// 文件长度,字节数

long fileLength = randomFile.length();

// 读文件的起始位置

int beginIndex = (fileLength 4) ? 4 : 0;

// 将读文件的开始位置移到beginIndex位置。

randomFile.seek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

// 将一次读取的字节数赋给byteread

while ((byteread = randomFile.read(bytes)) != -1) {

System.out.write(bytes, 0, byteread);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (randomFile != null) {

try {

randomFile.close();

} catch (IOException e1) {

}

}

}

}

/**

* 显示输入流中还剩的字节数

*/

private static void showAvailableBytes(InputStream in) {

try {

System.out.println("当前字节输入流中的字节数为:" + in.available());

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

String fileName = "C:/temp/newTemp.txt";

ReadFromFile.readFileByBytes(fileName);

ReadFromFile.readFileByChars(fileName);

ReadFromFile.readFileByLines(fileName);

ReadFromFile.readFileByRandomAccess(fileName);

}

}

复制代码

5、将内容追加到文件尾部

public class AppendToFile {

/**

* A方法追加文件:使用RandomAccessFile

*/

public static void appendMethodA(String fileName, String content) {

try {

// 打开一个随机访问文件流,按读写方式

RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");

// 文件长度,字节数

long fileLength = randomFile.length();

//将写文件指针移到文件尾。

randomFile.seek(fileLength);

randomFile.writeBytes(content);

randomFile.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* B方法追加文件:使用FileWriter

*/

public static void appendMethodB(String fileName, String content) {

try {

//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件

FileWriter writer = new FileWriter(fileName, true);

writer.write(content);

writer.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

String fileName = "C:/temp/newTemp.txt";

String content = "new append!";

//按方法A追加文件

AppendToFile.appendMethodA(fileName, content);

AppendToFile.appendMethodA(fileName, "append end. \n");

//显示文件内容

ReadFromFile.readFileByLines(fileName);

//按方法B追加文件

AppendToFile.appendMethodB(fileName, content);

AppendToFile.appendMethodB(fileName, "append end. \n");

//显示文件内容

ReadFromFile.readFileByLines(fileName);

}

}


分享文章:java添加文件夹的代码,java中如何创建文件夹
网页链接:http://cqcxhl.cn/article/hesjpg.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP