当前位置: 代码迷 >> Java相关 >> 简单的io问题
  详细解决方案

简单的io问题

热度:239   发布时间:2008-04-19 08:09:03.0
简单的io问题
程序:
import java.io.File;
public class TestFile
{
    public static void main(String args[])
    {
        String dirname="\\java";
        File f = new File(dirname);
        
        if(f.isDirectory())
        {
            System.out.println("*****dirtory of "+dirname+"******");
            String s[] = f.list();
            System.out.println("-----The file under the directory of "+dirname+"-----");
            for(int i = 0; i<s.length; i++)
              {
                  File f1 = new File(s[i]);
                  
                  if(f1.isFile())
                  {
                      
                     System.out.println(s[i]+"   绝对路径:"+f1.getAbsolutePath(f1));
                }
                     
              }
             System.out.println("-----The directory under the directory of "+dirname+"-----");
            for(int j = 0 ;j<s.length;j++)
            {
                File f1 = new File(s[j]);
                if(f1.isDirectory())
                {
                    
                    System.out.println(s[j]);
                    }
            }
                
        }
        else
         System.out.println(dirname+"is a File");    
        
    }
    
}

结果这样:
TestFile.java:21: 无法将 java.io.File 中的 getAbsolutePath() 应用于 (java.io.File)
           System.out.println(s[i]+"   绝对路径:"+f1.getAbsolutePath(f1));
                                                                        ^
1 错误
搜索更多相关的解决方案: java  

----------------解决方案--------------------------------------------------------
回复 1# 的帖子
“System.out.println(s[i]+"   绝对路径:"+f1.getAbsolutePath(f1));”中的getAbsolutePath方法是没有参数的,另外“String s[] = f.list();”调用List方法取得是“String dirname="\\java";”目录下的文件和目录名字的字符串而已,而不是该文件或目录的路径,用File f1 = new File(s[i]);无法创建Flie类的实例,自己查一下API吧
----------------解决方案--------------------------------------------------------
回复 1# 的帖子
大概改了一下:假设目录是 E:\Mydoc\
if(f.isDirectory())
        {
            System.out.println("*****"+dirname+"目录下******");
            String s[] = f.list();
            System.out.println("目录"+dirname+"下的文件是:");
            for(int i = 0; i<s.length; i++)
              {
                  File f1 = new File("e:\\Mydoc\\"+s[i]);
                  
                  if(f1.isFile())
                  {
                     
                     System.out.println("文件"+s[i]+"的绝对路径是:"+f1.getAbsolutePath());
                           
                  }
                     
              }
             System.out.println("目录"+dirname+"下的子目录是:");
            for(int j = 0 ;j<s.length;j++)
            {
                File f1 = new File("e:\\Mydoc\\"+s[j]);
                if(f1.isDirectory())
                {
                    
                    System.out.println(s[j]);
                    }
            }
               
        }
----------------解决方案--------------------------------------------------------
顶下一
----------------解决方案--------------------------------------------------------
  相关解决方案