我试过了,getpath()不能得到目录

解决方案 »

  1.   

    给你个我自己写的程序做例子学习编程:
     // 我们有的时候知道一个大概的类名,但是不知道该类的全名,这个时候可以用
     // 这个类来解决。 // 方法如下: // 例如查找一个名字中包含String的类:C:\exam\myjavatool>java FindType String
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xml/utils/XMLString.cla
    ss
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/functions/FuncStr
    ing.class
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/objects/XString.c
    lass
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!org/apache/xpath/operations/String
    .class
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!sun/awt/CharsetString.class
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!sun/awt/print/PrintDialog$MediaStr
    ing.class
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!java/lang/String.class
    C:\JavaWork\jdk1.4b3\jre\lib\rt.jar!java/text/AttributedString.class
    C:\JavaWork\jakarta-ant-1.4.1\bootstrap\lib\ant.jar!org/apache/tools/a
    nt/taskdefs/Replace$NestedString.class完全由fightboy编写,大家都可以使用和修改,传播代码如下:import java.io.*;
    import java.util.regex.*;
    import java.util.*;
    import java.util.jar.*;
    import java.util.zip.*;public class FindType
    {    public void find(String fileName)
        {
            PrintWriter pw = null;
            try{
                tmpFile = File.createTempFile("Find.",".tmp",new File("."));
                pw = new PrintWriter(new FileOutputStream(tmpFile));
                tmpFile.deleteOnExit();
            }catch(Exception e)
            {
                System.err.println("Unable to create Temp file.");
                return;
            }
          
            String path = System.getProperties().getProperty("java.class.path");
            StringTokenizer st = new StringTokenizer(path,";");
            while(st.hasMoreTokens())
            {
              String part = st.nextToken();
              File partFile = new File(part);
              if(partFile.exists())
              {
                if(partFile.isDirectory())
                   findFileInDir(partFile,pw);
                else
                   findFileInJar(partFile,pw);
              }          
            }
            pw.close();
            try{
              InputStreamReader isr = new InputStreamReader(new FileInputStream(tmpFile));
              BufferedReader br = new BufferedReader(isr);
              findString(fileName,br);
              br.close();
            }catch(IOException e)
            {
                System.err.println("Unable to read Temp file.");
                return;
            }       
      }
      
      public void findString(String fileName,BufferedReader br) throws IOException
      { String curStr;
        String cmpStr;
        Pattern p;
        if(expr) p = Pattern.compile(fileName);
        else p = Pattern.compile(".*"+fileName+"*");
        while((curStr=br.readLine())!=null )
        {
          cmpStr=curStr.replace('/','.');
          cmpStr=cmpStr.replace('\\','.');
          cmpStr=cmpStr.replaceAll("\\.class","");
          cmpStr=cmpStr.replaceAll(".*!","");
    //      System.out.println(cmpStr);
          Matcher m = p.matcher(cmpStr);
          if(m.matches())
            System.out.println(curStr);
        }
      }
      public void findFileInDir(File dir,PrintWriter pw)
      {
            String as[] = dir.list();
            for(int j = 0; j < as.length; j++)
            {   File next = new File(""+dir+"\\"+as[j]);
                if(next.isFile())
                  { if(next.getAbsolutePath().endsWith(".class"))
                      pw.println(next.getAbsolutePath());
                  }
                else
                  {                
                    findFileInDir(next,pw);
                  }
            }
      }
      
      public void findFileInJar(File jarFile,PrintWriter pw)
      {
         try{   
            JarInputStream jis = new JarInputStream(new FileInputStream(jarFile));
            ZipEntry entry = jis.getNextEntry();
            while(entry != null)
            {   if(!entry.isDirectory())
                  { if(entry.getName().endsWith(".class"))
                      pw.println(jarFile.getAbsolutePath()+"!"+entry.getName());
                  }            
                jis.closeEntry();
                entry = jis.getNextEntry();
            }
         } catch (Exception e)
         {
            System.err.println("Error in File "+jarFile.getName());
         }
      }
      
        public static void main(String args[])
        {   
            FindType ft = new FindType();        
            
            String name = null;
            for(int i=0;i<args.length;i++)
            {
              if(args[i].equalsIgnoreCase("-e")) ft.expr=true;
           else if(args[i].equalsIgnoreCase("-cp"))
           { i++; 
             System.getProperties().setProperty("java.class.path",args[i]);
           }
           else
             name = args[i];         
            }
            
            if(name != null)
                ft.find(name);
            else
            {
         System.err.println("Usage: java FindType [-e] <fileName> [-cp classPath]");
         System.exit(1);
         }            
        }
        private File tmpFile;
        private boolean expr=false; 
    }
      

  2.   

    还是不行
    例如我要得到“my.txt”这个文件所在的目录
    File f = new File("my.txt");
    String d = f.getPath();
    String dd = f.getAbsoluteFile();打印d 和dd 结果都是“my.txt”
      

  3.   

    程序在jdk1.4下边编译运行通过!
      

  4.   

    (1)用“.”做目录建立一个文件,然后对这个文件用getAbsolutePath()得到
    绝对路经。
    (2)没必要
      

  5.   

    那就用这个吧
    刚才我试了一下
    可以用的getAbsolutePath()