javabean里能访问文件吗?如果能文件应放在哪里

Yanbin_Q 2003-08-23 09:15:07
下面的那段代码执行就会出错

//ReadFile

package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class ReadFile extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{

PrintWriter out=res.getWriter();
out.println("Hello World");
try{
InputStream input=new FileInputStream("C:\\PkgClnup.log");
}
catch(FileNotFoundException e){
out.println(e.getMessage());
}
}
}

错误提示为
HTTP Status 503 - Servlet org.apache.catalina.INVOKER.test.ReadFile is currently unavailable
...全文
28 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
emoam 2003-08-28
  • 打赏
  • 举报
回复
谁说只访问绝对路径了?

下面这段程序就是访问相对路径的!读入input.xml文件写入到output.xml文件中

public static void main(String[] argc)
{
XML_CurParse xmlparse = new XML_CurParse();
Document doc = null;
DocumentBuilder builder = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
builder = factory.newDocumentBuilder();
doc = builder.parse("input.xml");
doc.normalize();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
xmlparse.Open_PrinterWrite("./output.xml");
xmlparse.WriteXmlFile(doc);
xmlparse.Close_PrinterWrite();
}
liad 2003-08-26
  • 打赏
  • 举报
回复
// TestProperties.java
package com.liad.beans;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileInputStream;

public class TestProperties
{
private String title;

public String getTitle() throws Exception
{
Properties props = new Properties();
InputStream in = TestProperties.class.getResourceAsStream("Notepad.properties");
props.load(in);
in.close();
return props.getProperty("Title");
}
}
--------------------------------------------------------------------------
# @(#)Notepad.properties 跟TestProperties.class在同一目录
#
# Resource strings for Notepad example

Title=Hello, world
--------------------------------------------------------------------------
<%-- testprops.jsp --%>
<%@ page import="com.liad.beans.TestProperties" %>
<html>
<head>
<title></title>
</head>
<body bgcolor="#ffffff">
<%
TestProperties tp = new TestProperties();
out.print(tp.getTitle());
%>
</body>
</html>
凋零的老树 2003-08-26
  • 打赏
  • 举报
回复
javabean只能访问绝对路径,因为javabean本身的路径是由调用它的JSP的路径决定
oopliu 2003-08-26
  • 打赏
  • 举报
回复
关注
beyondhuangjie 2003-08-25
  • 打赏
  • 举报
回复
javabean里怎么都用绝对路径啊,一点都没有实用价值,相对路径怎么搞?我都发了一个帖子问了。
Yanbin_Q 2003-08-25
  • 打赏
  • 举报
回复
如果改成下面的代码是可以读出文件C:\PkgClnup.log内容的,但如果用相对目录来访问文件呢?那基准目录在哪里呢

package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class ReadFile extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{

PrintWriter out=res.getWriter();
InputStream input=null;
out.println("Hello World");
try{
input=new FileInputStream("C:\\PkgClnup.log");
}
catch(FileNotFoundException e){
e.printStackTrace();
}

Reader fr=new InputStreamReader(input);
BufferedReader reader=new BufferedReader(fr);
String s=null;
while((s=reader.readLine())!=null){
out.println(s);
}
}
}
idilent 2003-08-23
  • 打赏
  • 举报
回复
不是访问文件出的错吧,你把访问文件的代码去了看看。
hj12 2003-08-23
  • 打赏
  • 举报
回复
关注
Yanbin_Q 2003-08-23
  • 打赏
  • 举报
回复
我就是看它能不能读
seaman0916 2003-08-23
  • 打赏
  • 举报
回复
我觉得你的代码倒没问题!你把它单独在浏览器里访问一下看看!

http://localhost:8080/虚拟目录/servlet/test.文件名 //注意test.文件名

我奇怪的是:你光读出来,怎么不处理?比如说输出呀,什么的!这是做什么? 哈哈

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧