有关new File(URI uri)的问题,请各位指点一二! 我好像没有看到过用url来定义文件的吧!File的构造函数中没有这一个! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哦!你用的是jdk1.4吧!呵呵!没想到! 是JDK1.4里的,不知为何总出毛病??其实我想在tomcat环境下读取文件的相对路径,想用这个方法。总不能老有Class.getResourceAsStream(String s)吧。 请问:如何才能在tomcat4.0环境下设置文件的相对路径??比如我的文件在file文件夹下的话??? 我的程序放在硬盘上作测试,程序里读取文件的路径是采用的绝对路径,但是当我想要正式发布时,便将程序放在我机器上的tomcat运行环境里,这时程序仍不会出错,但很显然读文件时仍然指向的时之前作测试式的绝对路径上的文件。 请问这个问题该如何解决??? 我曾尝试着用相对路径如"../文件名"和用new File(URI uri);都不能读取到文件。 请指点我一个更好的办法。谢谢!! 请看源文件: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没有错误,编译也能正常通过,但找不到文件。 为什么不能声明ConcurrentHashMap<int,String>? javamail发送接收邮件,老问题。 大家有没有<<thinking in java>><<data structures,algorithms,applications in c++>> 如何生成按指定概率密度的随机对象序列? JAVA中用来画图的类 求助~~关于用jai制作图片水印的问题 一道简单题,送分的 static静态初始化的问题 拿到个OFFER,大家给个意见 新人空指针异常求助 不用继承,怎样得到一个类中的JLabel组件? 请教一句sql语句
其实我想在tomcat环境下读取文件的相对路径,想用这个方法。总不能老有Class.getResourceAsStream(String s)吧。
请问这个问题该如何解决???
我曾尝试着用相对路径如"../文件名"和用new File(URI uri);都不能读取到文件。
请指点我一个更好的办法。谢谢!!
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没有错误,编译也能正常通过,但找不到文件。