//java递归调用输出一个目录下的所有子目录及文件名称 (最新增加)

 

//java递归调用输出一个目录下的所有子目录及文件名称
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Test
{
public static void main(String[] args) throws IOException
{
Delete();
System.out.print("输入你选择的地址: ");
Scanner scan = new Scanner(System.in);
String filename = scan.next();
System.out.print("开始录入: ");
System.out.println("————————");
new Test().TreeName(filename, "");
System.out.println("总共访问的文件有: " + count);
}

private String listFileStr;
private static int count;

public void TreeName(String path, String tab) throws IOException
{
File file = new File(path);
File[] childFiles = file.listFiles();// 找出所有子目录
for (int i = 0; childFiles != null && i < childFiles.length; i++)
{
count++;
listFileStr = childFiles[i].getAbsolutePath() + "\n";// 隔行
System.out.println(tab
+ childFiles[i].getAbsolutePath());
filePrint(listFileStr);
if (childFiles[i].isDirectory())
{// 如果是目录的话,则调用自身
TreeName(childFiles[i].getPath(), tab + "\t");
}
}
}

private void filePrint(String str) throws IOException
{
byte[] buffer = str.getBytes();
// 通过字节数组来将控制台中写入的数据再次读入文件夹中
FileOutputStream outputStream = new FileOutputStream(
"Newoutput.doc", true);
// 定义FileOutputStream对象时增加了一个参数true
// 用于一行一行写入,防止覆盖上一行的内容
outputStream.write(buffer);
outputStream.close();
}

private static void Delete()
{
File file = new File("Newoutput.doc");// 文件名称
if (file.exists())
{
System.out.print("该文档存在" + '\t');
file.delete();
System.out.println("重新建立");
} else
{
System.out.println("该文档不存在,新建...");
try
{
file.createNewFile();// 文件新建操作
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}

转载于:https://www.cnblogs.com/ghostTao/p/3660441.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值