0

I've the below project structure in my eclipse.

enter image description here

and my code in servlet is as below.

File entityFile = new File(getServletContext().getContextPath() + "/EntityList/entities.txt");
FileWriter fout = new FileWriter(entityFile);
fout.write("The Content");
fout.close();

here basically I'm trying to write to a file available at /EntityList/entities.txt, but when I run this, I get the exception as below.

SEVERE: Servlet.service() for servlet [com.luis.servlets.WriteEntityToAFile] in context with path [/LUISWebUI] threw exception java.io.FileNotFoundException: \LUISWebUI\EntityList\entities.txt (The system cannot find the path specified)

I know that I'm going wrong with the path, Can someone please put me in the right direction.

Update

Apologies for the confusion.

I'm sending some data from jsp to servlet to write to the entities.txt file. I'm able to capture it in servlet(Cross checked it by doing sysout).

user3872094
  • 2,853
  • 4
  • 23
  • 50

2 Answers2

1

First of all I think you have a typo problem, try /EntitiesList/entities.txt instead of /EntityList/entities.txt.

Also, move the /EntitiesList/entities.txt under /WEB-INF/ for example, so that your servlet class can access it.

You can read a more detailed explanation in this SO answer.

Edit:

About writing to file: your application will be packaged inside a WAR file so you won't be able to write to it, only read from it (more about this here).

But you can just use this way of creating directly a file outside the WAR and using this location to write your content (before that, make sure you have appropriate rights):

File entityFile = new File(getServletContext().getContextPath() + "entities.txt");
FileWriter fOut = new FileWriter(entityFile);
fOut.write("The Content");
fOut.close();

Or if you want the directory also, you'll have to execute some additional steps, create it first then specify the file name inside it where you want to write:

 File entityFolder = new File(getServletContext().getContextPath() + "EntitiesList");
 entityFolder.mkdir();
 File entityFile = new File(entityFolder, "entities.txt");
 FileWriter fOut = new FileWriter(entityFile);
 fOut.write("The Content");
 fOut.close();
aUserHimself
  • 1,551
  • 2
  • 18
  • 26
  • how will I write to the entities.txt, sorry for the confusion, updated my question. – user3872094 Aug 04 '17 at 09:02
  • This thing is still giving me the same file not found exception, moved it to the `WEB-INF` and the updated path is `File entityFile = new File(getServletContext().getContextPath() + "/WEB-INF/EntitiesList/entities.txt");` – user3872094 Aug 04 '17 at 09:23
0
  1. First there is Typo error in your path
  2. Second you should use getResourceAsStream() for loading file for writing.

Code example:

  InputStream input = getServletContext().
                              getResourceAsStream("/WEB-INF/EntityList/entities.txt");

  Files.copy(InputStream input , Path target) 
  //Or Files.copy(Path source, OutputStream out)

It seem easier to use FileInputStream, but it is better to use ResourceStream.
Read here https://stackoverflow.com/a/2161583/8307755
https://stackoverflow.com/a/2308224/8307755

Yogi
  • 1,536
  • 7
  • 20
  • I'm writing to the file that is refereed as input(as per your code), how can I do it?sorry for the confusion, updated my question. – user3872094 Aug 04 '17 at 08:57