简体   繁体   English

servlet中的文件读取问题

[英]File reading issue in servlet

I have to read a file using servlet.here is the code iam using.but file is not reading using this code.Always printing File contains null value----------------- : 我必须使用servlet读取文件。这是代码iam using.but文件不使用此代码读取。始终打印File contains null value-----------------

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);
}

javadoc to the rescue : javadoc救援:

java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException java.net.URL getResource(java.lang.String path)抛出java.net.MalformedURLException

Returns a URL to the resource that is mapped to the given path. 返回映射到给定路径的资源的URL。

The path must begin with a / and is interpreted as relative to the current context root, or relative to the /META-INF/resources directory of a JAR file inside the web application's /WEB-INF/lib directory. 路径必须以/开头,并且被解释为相对于当前上下文根,或者相对于Web应用程序的/ WEB-INF / lib目录中JAR文件的/ META-INF / resources目录。 This method will first search the document root of the web application for the requested resource, before searching any of the JAR files inside /WEB-INF/lib. 在搜索/ WEB-INF / lib中的任何JAR文件之前,此方法将首先在Web应用程序的文档根目录中搜索所请求的资源。 The order in which the JAR files inside /WEB-INF/lib are searched is undefined. 未定义搜索/ WEB-INF / lib中的JAR文件的顺序。

If you want to read from a resource in the web app, use a path as indicated above. 如果要从Web应用程序中的资源中读取,请使用上面指示的路径。 If you want to read from the file system, use file IO (and the correct file name): new FileInputStream("D:/root.properties") 如果要从文件系统中读取,请使用文件IO(和正确的文件名): new FileInputStream("D:/root.properties")

Use following code. 使用以下代码。 With this you can read file 有了它,你可以读取文件

    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();
    }
 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);
        }
    }
}

Please try the above code sample. 请尝试上面的代码示例。 I think in your code , file is not found. 我认为在您的代码中,找不到文件。 Please give the file path in the above code and try. 请在上面的代码中给出文件路径并尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM