首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正在读取目录名、文件名、文件长度和上次修改日期,JAVA

正在读取目录名、文件名、文件长度和上次修改日期,JAVA
EN

Stack Overflow用户
提问于 2015-02-18 22:35:12
回答 1查看 705关注 0票数 0

我正在尝试编写一个程序,它读取目录的名称作为命令行参数,并为目录中的每个文件打印以下信息:名称、长度和上次修改时间。

但不起作用,只显示相同文件的长度

代码语言:javascript
运行
复制
 import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.Date;


public class x2 {


static void  show(String str) {
       System.out.println(str);
     }



public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
           File file = new File(args[0]);

        //Here we check whether the given argument is a directory
         if(file.isDirectory()) {
           System.out.println("Contents of: " + args[0]);

          //Here we initialize array con with the content of the
          //directory
           String dirContent[] = file.list();

           // date field ??
           Date date = new Date(file.lastModified());
           SimpleDateFormat sdf=new SimpleDateFormat("dd:MM:yyyy kk:mm:ss");


          //Here we go through the content of the directory one by one
           for(int i=0; i<dirContent.length; i++) {

              //Here we create an object of class File with the ith 
              //member of array con
              File auxf = new File(args[0] + "/" + dirContent[i]);

                //Here we check whether each content is a directory
                if(auxf.isDirectory()) {

                  //Here we initialize array newCon with the content of 
                 //each sub-directory
                   String newCon[] = auxf.list();

                   // file lenght ????
                   long length = file.length();

                  //Here we check whether the sub-directory is empty or not
                   if(newCon.length>0){
                     System.out.println("Content of "+args[0] +"\\"+ dirContent[i]+"\\");
                     for(int j=0; j<newCon.length; j++)
                      System.out.println(args[0] +"\\"+ dirContent[i] + "\\" + newCon[j]+" file length:"
                            + length +sdf.format(date) );
                    }
                   else
                     System.out.println(dirContent[i] + " is an empty directory.");       
                } else
                 System.out.println(dirContent[i] + " is a file.");
              } 
            }

             else
              System.out.println(args[0] + " is a file.");


           }catch(Exception e) {

            System.out.println("Usage: java showDir file_or_dir_name");
           }


}

}

有没有人能帮我修改代码。

EN

回答 1

Stack Overflow用户

发布于 2015-02-18 22:46:35

您实际上是在根目录上调用file.length()

代码语言:javascript
运行
复制
public static void main(String[] args) {

  try {
    File file = new File(path);
    [...]
    for(int i=0; i<dirContent.length; i++) {
      File auxf = new File(path + "/" + aDirContent);
      [...]
      //Maybe you meant auxf.length() ?
      long length = file.length();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28586189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档