java 递归读取文件_java:利用递归遍历整个文件夹并写入文件

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.text.DecimalFormat;

public class FilePath {

public static void main(String[] args) {

//写入文件路径

String writePath = "D:/bingdu.com.doc";

//读取文件路径

String path = "E:/G";

fileterInfo(path);

writerFile(writePath, path);

}

// 读取文件信息

public static void fileInfo(String path) {

File file = new File(path);

// String fileName = path.substring(path.lastIndexOf("/")+1);

if (file.exists()) {

System.out.println(file.getName() + "文件已存在!");

System.out.println("file.getName:" + file.getName());

System.out

.println("file.getAbsolutePath:" + file.getAbsolutePath());

try {

System.out.println("file.getCanonicalPath:"

+ file.getCanonicalPath());

System.out.println("file.getCanonicalPath:"

+ file.getCanonicalFile());

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("file.getParent:" + file.getParent());

System.out.println("file.getPath:" + file.getPath());

System.out

.println("file.getAbsoluteFile:" + file.getAbsoluteFile());

System.out.println("file.getClass:" + file.getClass());

System.out.println("file.getParentFile:" + file.getParentFile());

} else {

System.out.println(file.getName() + "文件不存在!");

// System.out.println(file.exists() ? fileName + "文件已存在!" : fileName

// + "文件不存在!");

}

}

// 读取文件目录

public static String fileterInfo(String path) {

fileCount = 0;

directoryCount = 0;

sizes = 0;

content = "";

/*

* System.out.println(path + ">>>详细信息如下:/n");

* System.out.println("总文件个数:" + fileCount + " 个");

* System.out.println("总目录个数:" + directoryCount + " 个");

* System.out.println("总文件大小:" + new DecimalFormat("0.##").format(sizes)

* + " MB(兆)");

*/

return path + ">>>详细信息如下:/r/n/r/n" + fileterInfoToo(path)

+ "/r/n总文件个数:" + fileCount + " 个" + "/r/n总目录个数:"

+ directoryCount + " 个" + "/r/n总文件大小:"

+ new DecimalFormat("0.##").format(sizes) + " MB(兆)/r/n"

+ new DecimalFormat("0.##").format(sizes / 1024)

+ " GB(千兆)/r/n";

}

static int fileCount, directoryCount;

static double sizes;

static String content;

// 递归读取文件

public static String fileterInfoToo(String path) {

File file = new File(path);

String size = "";

if (file.exists() && file.isDirectory()) {

/*

* String[] arrStr = file.list(); for (String string : arrStr) {

* System.out.println(string); }

* System.out.println("总文件个数:"+arrStr.length);

*/

File[] files = file.listFiles();

for (File file2 : files) {

if (file2.isFile()) {

/*

* System.out.println(file2.exists() ? file2.getParent() +

* "-->" + file2.getName() : "");

*/

try {

if (file2.length() < 1024)

size = String.valueOf(file2.length()) + " B(字节)";

else if (file2.length() < 1024 * 1024)

size = new DecimalFormat("0.##")

.format(((double) file2.length() / 1024))

+ " KB(千字节)";

else if (file2.length() < 1024 * 1024 * 1024)

size = new DecimalFormat("0.##")

.format(((double) file2.length() / 1024 / 1024))

+ " MB(兆)";

else

size = new DecimalFormat("0.##")

.format(((double) file2.length() / 1024 / 1024 / 1024))

+ " GB(千兆)";

content += file2.getPath() + "的文件大小:" + size

+ file2.length() + " Bytes/r/n";

} catch (Exception e) {

e.printStackTrace();

}

sizes += ((double) file2.length() / 1024 / 1024);

fileCount++;

} else if (file2.isDirectory()) {

fileterInfoToo(file2.getPath());

directoryCount++;

}

}

}else { // System.out.println(path + "文件目录不存在!");

content = path +"文件目录不存在!";

}

return content;

}

// 写入文件

public static void writerFile(String writePath, String path) {

try {

BufferedWriter bWriter = new BufferedWriter(new FileWriter(

writePath));

bWriter.write(fileterInfo(path));

bWriter.flush();   bWriter.close();   System.out.println("Over");  } catch (IOException e) {   e.printStackTrace();  } }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值