Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Web Application read from file and write to same file

Tags:

java

jsp

netbeans

Ok, I have a web application that I have written and I can read from a file that I have included in my source package in a new folder named "text". I am trying to write to that same file, but it does not work. It never writes to the file. Here is the code for my two methods:

public void fillItems() throws IOException{
    String path = "/OBrien_PROJ2/text/catalog.txt";
    BufferedReader br = new BufferedReader(new   InputStreamReader(getClass().getResourceAsStream(path))); 
    String text = null;
    while ((text=br.readLine())!=null){
       String[] itemArray = text.split(","); 
       // you might want to check array size
       items.add(new ItemBean (itemArray[0], itemArray[1], itemArray[2], itemArray[3], itemArray[4]));

    }

    br.close();
}



 public void createNewItem(String iD, String name, String description, String price, String quantity) throws IOException{
     String path = "/OBrien_PROJ2/text/catalog.txt";
     BufferedWriter bw = new BufferedWriter(new FileWriter(path));
     bw.write(iD + "," + name + "," + description + "," + price + "," + quantity);
     items.add(new ItemBean (iD, name, description, price, quantity));
     bw.flush();
     bw.close();
 }

If it matters, I am using NetBeans

like image 911
ssgtob1 Avatar asked Oct 19 '25 05:10

ssgtob1


2 Answers

Use getServletContext().getRealPath("/") to get the current path of a web application

You Would better use this code that I have written right now

<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.FileOutputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World!</h1>
    <%
        try{
        String file = getServletContext().getRealPath("/")+"text/test.txt";
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        bos.flush();
        bos.close();
        }
        catch(Exception ex){
            ex.printStackTrace();
            }
    %>
</body>

like image 111
Mohammed Alaghbari Avatar answered Oct 22 '25 02:10

Mohammed Alaghbari


The resource you read is not a file. It's a resource loaded by the classloader. When deployed, this resource will be read from inside a war file, and perhaps even from a jar file inside this war file.

Don't ever try to modify the contents of a webapp dynamically. Even if it is possible, it's an extremely bad idea, since any redeployment of the webapp would delete the creted or modified files. If you have to write somewhere, write to a database (preferrably), since it can easily be shared by multiple webapp instances, and handle concurreny natively), or to a file outside of the webapp.

BTW, the argument of the FileWriter constructor is a file path. So new FileWriter("/OBrien_PROJ2/text/catalog.txt") writes to the file /OBrien_PROJ2/text/catalog.txt on the file system (and not inside the webapp).

like image 42
JB Nizet Avatar answered Oct 22 '25 04:10

JB Nizet