JAVA中如何得到文件路径
1.如何获得当前文件路径
常用:
字符串类型:Property("user.dir");
综合:
jl.test.base;
import java.io.File;
public class Test {
    public static void main(String[] args) throws Exception {
        System.out.println(
            Thread.currentThread().getContextClassLoader
().getResource(""));
        System.out.println(ClassLoader
().getResource(""));
        System.out.SystemResource(""));
        System.out.println(Resource(""));
        System.out.println(Resource("/"));
        System.out.println(new File("").getAbsolutePath());
        System.out.Property("user.dir"));
    }
}
2.Web服务中
(1).Weblogic
WebApplication的系统文件根目录是你的weblogic安装所在根目录。
例如:如果你的weblogic安装在c:\
那么,你的文件根路径就是c:\.
所以,有两种方式能够让你访问你的服务器端的文件:
a.使用绝对路径:
比如将你的参数文件放在c:\yourconfig\yourconf.properties
直接使用 new FileInputStream("yourconfig/yourconf.properties");
b.使用相对路径:
相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级
目录,将你的参数文件放在yourwebapp\yourconfig\yourconf.properties
这样使用:
new FileInputStream("./yourconfig/yourconf.properties");
这两种方式均可,自己选择。
(2).Tomcat
在类中输出Property("user.dir");显示的是%Tomcat_Home%/bin
(3).Resin
不是你的JSP放的相对路径,JSP引擎执行这个JSP编译成SERVLET
的路径为根.比如用新建文件法测试File f = new File("a.htm");
这个a.htmresin的安装目录下
(4).如何读相对路径哪?
Java文件中getResourcegetResourceAsStream均可
例:getClass().getResourceAsStream(filePath);//filePath可以
"/filename",这里的/代表web发布根路径下WEB-INF/classes
(5).获得文件真实路径
string  file_real_RealPath("mypath/filename"); 
通常使用RealPath("/"); 
3.文件操作的类
import java.io.*;
import java.*;
import java.util.*;
//import javax.swing.filechooser.*;
//import org.jr.swing.filter.*;
/**
* 此类中封装一些常用的文件操作。
* 所有方法都是静态方法,不需要生成此类的实例,
* 为避免生成此类的实例,构造方法被申明为private类型的。
* @since  0.1
*/
public class FileUtil {
  /**
  * 私有构造方法,防止类的实例化,因为工具类不需要实例化。
  */
  private FileUtil() {
  }
  /**
  * 修改文件的最后访问时间。
  * 如果文件不存在则创建该文件。
  * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这
些信息输出是否保留还在考
虑中。</b>
  * @param file 需要修改最后访问时间的文件。
  * @since  0.1
  */
  public static void touch(File file) {
    long currentTime = System.currentTimeMillis();
    if (!ists()) {
      println("file not found:" + Name());
      println("Create a new file:" + Name());
      try {
        if (ateNewFile()) {
        //  System.out.println("Succeeded!");
        }
        else {
        //  println("Create file failed!");
        }
      }
      catch (IOException e) {
      //  println("Create file failed!");
        e.printStackTrace();
      }
    }
    boolean result = file.setLastModified(currentTime);
    if (!result) {
    //  println("touch failed: " + Name());
    }
  }
  /**
  * 修改文件的最后访问时间。
  * 如果文件不存在则创建该文件。
  * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这
些信息输出是否保留还在考
虑中。</b>
  * @param fileName 需要修改最后访问时间的文件的文件名。
  * @since  0.1
java创建文件  */
  public static void touch(String fileName) {
    File file = new File(fileName);
    touch(file);
  }
  /**
  * 修改文件的最后访问时间。
  * 如果文件不存在则创建该文件。
  * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这
些信息输出是否保留还在考
虑中。</b>
  * @param files 需要修改最后访问时间的文件数组。
  * @since  0.1
  */
  public static void touch(File[] files) {
    for (int i = 0; i < files.length; i++) {
      touch(files);
    }
  }
  /**
  * 修改文件的最后访问时间。
  * 如果文件不存在则创建该文件。
  * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这
些信息输出是否保留还在考
虑中。</b>
  * @param fileNames 需要修改最后访问时间的文件名数组。
  * @since  0.1
  */
  public static void touch(String[] fileNames) {
    File[] files = new File[fileNames.length];
    for (int i = 0; i < fileNames.length; i++) {
      files = new File(fileNames);
    }
    touch(files);
  }
  /**
  * 判断指定的文件是否存在。
  * @param fileName 要判断的文件的文件名
  * @return 存在时返回true,否则返回false
  * @since  0.1
  */
  public static boolean isFileExist(String fileName) {
    return new File(fileName).isFile();
  }
  /**
  * 创建指定的目录。
  * 如果指定的目录的父目录不存在则创建其目录书上所有需要的父目录
  * <b>注意:可能会在返回false的时候创建部分父目录。</b>
  * @param file 要创建的目录
  * @return 完全创建成功时返回true,否则返回false
  * @since  0.1
  */
  public static boolean makeDirectory(File file) {
    File parent = ParentFile();
    if (parent != null) {
      return parent.mkdirs();
    }