重庆分公司,新征程启航

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

java目录的实现代码 java目录的实现代码怎么写

用java写一个代码实现: 如何列出某个目录下的所有子目录

/**

成都创新互联咨询电话:18980820575,为您提供成都网站建设网页设计及定制高端网站建设服务,成都创新互联网页制作领域10余年,包括生料搅拌车等多个领域拥有丰富的网站营销经验,选择成都创新互联,为网站保驾护航。

* 列出文件夹下的子文件夹名

* @param localRoot

* @throws content

*/

public static void list(String localRoot) throws Exception {

File[] fs = new File(localRoot).listFiles();

if ((fs == null) || (fs.length = 0)) {

System.out.println("空文件夹");

return;

}

for (File f : fs) {

if (f.isDirectory()) {

System.out.println("目录:"+ f.getName());

}

}

}

建立个class然后见个main方法调用一下就可以了!

怎么把指定文件存在指定目录下java

java写入文件到指定文件夹的方法主要有两种:利用PrintStream和利用StringBuffer

例如将文本“I'm the text to be write”写入到文件夹D:/test下,并命名为test.txt,则两种方式简单实现代码如下:

1. 利用PrintStream写文件

public void PrintStreamDemo(){

try {

FileOutputStream out=new FileOutputStream("D:/test.txt");

PrintStream p=new PrintStream(out);

p.println("I'm the text to be write");

} catch (FileNotFoundException e){

e.printStackTrace();

}

}

2. 利用StringBuffer写文件

public void StringBufferDemo() throws IOException{

File file=new File("D:/test.txt");

if(!file.exists())

file.createNewFile();

FileOutputStream out=new FileOutputStream(file,true);

StringBuffer sb=new StringBuffer();

sb.append("I'm the text to be write");

out.write(sb.toString().getBytes("utf-8"));

}

out.close();

}

提示:利用StringBuffer写文件可以设定使用何种编码,有效解决中文问题。

java 文件夹中如何创建文件夹?

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

}

}

希望对你有帮助。。。。仍有问题可以HI我。。。


分享题目:java目录的实现代码 java目录的实现代码怎么写
文章分享:http://cqcxhl.com/article/dopdpdp.html

其他资讯

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