zoukankan      html  css  js  c++  java
  • //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() + " ";// 隔行
    System.out.println(tab
    + childFiles[i].getAbsolutePath());
    filePrint(listFileStr);
    if (childFiles[i].isDirectory())
    {// 如果是目录的话,则调用自身
    TreeName(childFiles[i].getPath(), tab + " ");
    }
    }
    }

    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("该文档存在" + ' ');
    file.delete();
    System.out.println("重新建立");
    } else
    {
    System.out.println("该文档不存在,新建...");
    try
    {
    file.createNewFile();// 文件新建操作
    } catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }
    }

  • 相关阅读:
    Oracle死锁只会回滚跟死锁有关的那条SQL,而不会回滚整个事务
    Mysql安装过程(linux:2.6.18-194.el5,Mysql:)
    格式化分区,报/dev/sdb1 is apparently in use by the system; will not make a filesystem here!
    安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined
    mysql-5.5.25-winx64在win7 x64 免安装配置
    insert遭遇阻塞
    linux中轻松使用backspace和上下按键
    排序
    MySQL
    基于Boost的同步TCP通信
  • 原文地址:https://www.cnblogs.com/ghostTao/p/3660441.html
Copyright © 2011-2022 走看看