我好像没有看到过用url来定义文件的吧!
File的构造函数中没有这一个!

解决方案 »

  1.   

    哦!你用的是jdk1.4吧!呵呵!没想到!
      

  2.   

    是JDK1.4里的,不知为何总出毛病??
    其实我想在tomcat环境下读取文件的相对路径,想用这个方法。总不能老有Class.getResourceAsStream(String s)吧。
      

  3.   

    请问:如何才能在tomcat4.0环境下设置文件的相对路径??比如我的文件在file文件夹下的话???
      

  4.   

    我的程序放在硬盘上作测试,程序里读取文件的路径是采用的绝对路径,但是当我想要正式发布时,便将程序放在我机器上的tomcat运行环境里,这时程序仍不会出错,但很显然读文件时仍然指向的时之前作测试式的绝对路径上的文件。
        请问这个问题该如何解决???
        我曾尝试着用相对路径如"../文件名"和用new File(URI uri);都不能读取到文件。
        请指点我一个更好的办法。谢谢!!
      

  5.   

    请看源文件:package counter;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import java.net.*;public class CounterServlet extends HttpServlet {
        private static final String CONTENT_TYPE = "text/html; charset=GBK";
        private Properties proper=new Properties();
        private int count;
        private static final String IP="http:///localhost:8080";
        private URI uri=null;    public void init() throws ServletException {
            super.init();
        }    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
            this.process(request,response);
        }
        protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
            this.process(request,response);
        }
        protected void process(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
            response.setContentType("text/html;charset=gb2312");
            try{
                this.uri=new URI(this.IP+"/bologu/file/visitedcount.props");
            }catch(URISyntaxException us){}
            DataInputStream in=new DataInputStream(
                                    new BufferedInputStream(
                                        new FileInputStream(this.uri)));
            proper.load(in);
            if(proper.getProperty("count")!=null) this.count=Integer.parseInt(proper.getProperty("count"));
            this.count++;
            String s="#访问量统计\n"+"count="+this.count;
            PrintStream note=new PrintStream(
                                new BufferedOutputStream(
                                    new FileOutputStream(new File(this.uri))));
            note.println(s);
            String outCount=Integer.toString(this.count);
            PrintWriter out=response.getWriter();
            StringBuffer sb=new StringBuffer(Integer.toString(this.count));
            out.println(sb.toString());
            out.flush();
            in.close();
            note.close();
            out.close();
        }
    }
    路径bologu/file/visitedcount.props没有错误,编译也能正常通过,但找不到文件。