2011-12-28 63 views
1

我要读使用servlet.here文件是代码IAM using.but文件不读书用这个code.Always打印File contains null value-----------------文件阅读问题的servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    try { 
     response.setContentType("text/html"); 
     String filename = "D/root.properties"; 
     ServletContext context = getServletContext(); 

     InputStream inp = context.getResourceAsStream(filename); 
     if (inp != null) { 
      InputStreamReader isr = new InputStreamReader(inp); 
      BufferedReader reader = new BufferedReader(isr); 
      PrintWriter pw = response.getWriter(); 

      String text = ""; 

      while ((text = reader.readLine()) != null) {      
      } 
     } else { 
      System.out.println("File contains null value-----------------"); 
     } 
    } catch(Exception e) { 
     System.out.println("Rxpn............................................."+e); 
    } 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    doGet(request,response); 
} 
+0

检查文件路径和文件内容 – 2011-12-28 09:45:11

+0

文件内容和文件路径是否正确 – chinchu 2011-12-28 09:46:10

+0

这是你正在使用的绝对路径吗?另外,它是'D:/ root.properties'(你似乎缺少斜线)? – 2011-12-28 09:46:38

回答

4

javadoc救援:

java.net.URL中的getResource(java.lang.String中路径) 抛出java.net.MalformedURLException

返回映射到给定路径的资源的URL。

路径必须以/开头,并且相对于当前上下文根,或相对于Web应用程序的/ WEB-INF/lib目录内的JAR文件的/ META-INF/resources目录中的/ META-INF/resources目录解释。 在搜索/ WEB-INF/lib中的任何JAR文件 之前,此方法将首先在Web应用程序 的文档根目录中搜索请求的资源。 /WEB-INF/lib中的JAR文件的搜索顺序未定义。

如果要从Web应用程序中的资源读取数据,请使用上述路径。如果你想从文件系统读取,使用文件IO(和正确的文件名):new FileInputStream("D:/root.properties")

0

使用下面的代码。 有了这个,你可以阅读文件

File file = new File("Filepath"); 

    try { 
     if (file.exists()) { 
      BufferedReader objBufferReader = new BufferedReader(
        new FileReader(file)); 

      ArrayList<String> arrListString = new ArrayList<String>(); 
      String sLine = ""; 
      int iCount = 0; 

      while ((sLine = objBufferReader.readLine()) != null) { 
       arrListString.add(sLine); 
      } 
      objBufferReader.close(); 

      for (iCount = 0; iCount < arrListString.size(); iCount++) { 
       if (iCount == 0) { 
        createTable(arrListString.get(iCount).trim()); 
       } else { 
        insertIntoTable(arrListString.get(iCount).trim()); 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

将文件路径用作D:/ filename – 2011-12-28 09:47:05

0
public class Main { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws FileNotFoundException, IOException { 
     FileInputStream file = new FileInputStream("c:\\hi.txt"); 
     DataInputStream input = new DataInputStream(file); 
     BufferedReader br = new BufferedReader(new InputStreamReader(input)); 
     String text = ""; 
     while ((text = br.readLine()) != null) { 
      System.out.println(text); 
     } 
    } 
} 

请尝试上面的代码示例。我想在你的代码中找不到文件。请在上面的代码中提供文件路径并尝试。