import java.io.*;File homeDir = new File(System.getProperty("user.home"));试一试

解决方案 »

  1.   

    Shrewdcat(丧邦&灵猫&潇):
      如何获取当前软件的运行位置?我用user.dir获取的是用户当前目录。
      我这样试过了:将文件编译为file.exe放在f盘根目录下。
      在C:\>f:\test运行输出user.dir为C:\>。
      用f:\>test运行是,输出user.dir为f;\>。
    kennethd() :
      user.home返回的是C:\Documents and Settings\Administrator。
      如果换一个系统用户登录去启动这个程序是不是会获取到另外一个值呢?在不同的操作系统下获取不同的路径是可以的。
    我的理解是,程序获取的路径最好只根操作系统和机器硬盘分区有关。其他设置均不影响获取的路径最好。
      

  2.   

    应该没有,那是完全依赖于环境的
    java.version Java Runtime Environment version 
    java.vendor Java Runtime Environment vendor 
    java.vendor.url Java vendor URL 
    java.home Java installation directory 
    java.vm.specification.version Java Virtual Machine specification version 
    java.vm.specification.vendor Java Virtual Machine specification vendor 
    java.vm.specification.name Java Virtual Machine specification name 
    java.vm.version Java Virtual Machine implementation version 
    java.vm.vendor Java Virtual Machine implementation vendor 
    java.vm.name Java Virtual Machine implementation name 
    java.specification.version Java Runtime Environment specification version 
    java.specification.vendor Java Runtime Environment specification vendor 
    java.specification.name Java Runtime Environment specification name 
    java.class.version Java class format version number 
    java.class.path Java class path 
    java.library.path List of paths to search when loading libraries 
    java.io.tmpdir Default temp file path 
    java.compiler Name of JIT compiler to use 
    java.ext.dirs Path of extension directory or directories 
    os.name Operating system name 
    os.arch Operating system architecture 
    os.version Operating system version 
    file.separator File separator ("/" on UNIX) 
    path.separator Path separator (":" on UNIX) 
    line.separator Line separator ("\n" on UNIX) 
    user.name User's account name 
    user.home User's home directory 
    user.dir User's current working directory
      

  3.   

    如果你在当前软件的运行目录进行的话
    直接访问文件就行了,不必要获得这个目录阿如果要兼容不同的操作系统要使用java的跨平白机制,比如文件路径分隔符方法
      

  4.   

    to abcpl(蓝鸟):
      直接访问文件我知道。但是程序运行时生成的日志文件我必须放在一个目录下
      在当前目录下建立一个目录如何做到??
      

  5.   

    得到class存放的目录:private String getPath() {
        String className=getClass().getName();    if (!className.startsWith("/")) {
           className = "/" + className;
        }
        className = className.replace('.', '/');
        className = className + ".class";    java.net.URL classUrl =   getClass().getResource(className);    if (classUrl != null) {
          System.out.println("Entry Path in " + classUrl.getFile() );
          String path=classUrl.getPath();   //  该方法要求的JDK最低版本是1.3
          System.out.println( classUrl.getPath()) ;
          System.out.println( classUrl.getHost()) ;
          if (path.startsWith("/"))    path=path.substring(1) ;
          int cc=path.lastIndexOf(className);
          if (cc!=0) path=path.substring(0,cc) ;
          if (!path.endsWith("/")) path=path+"/";
            return path;
        } else {
          System.out.println(" Class '" + className +
            "' not found in  '" +
            System.getProperty("java.class.path") + "'");
            return null;
        }
    }