I have Netbeans 7.1.2 running and I am trying to access some text files within a servlet:
package com.optimizations.cutting;
@WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
public class Servlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("in servlet "+System.currentTimeMillis());
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
DataManager dm = new DataManager();
SheetInfo si = dm.loadSheetInfoCSV("sheetInfo.csv");
ArrayList<Piece> pieces = dm.loadPiecesCSV("res/pieces4.csv");
....
I have placed the sheetInfo.csv and the pieces4.csv files everywhere i could think of, trying to acces them with a backslash ahead ( /sheetInfo.csv or /res/pieces4.csv )
when i say “everywhere i could think of” i mean : current directory (source packages), next to Servlet.java and all the other files I created (including DataManager.java which uses it). I also did the "Add folder" in the Properties window->Sources->Package Folder. (added 2 folders, just to make sure ). So my dear files are in 3 places all at once:
-
src/java/com/optimizations/cuttingnext toServlet.javaandDataManager.java -
src/java/res -
src/resources
but i stll get theSEVERE: java.io.FileNotFoundException: resources/pieces4.csv (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:97) at java.io.FileReader.<init>(FileReader.java:58) at com.optimizations.cutting.DataManager.loadPiecesCSV(DataManager.java:98)`
I have also restarted the server (Glassfish 3.1.2)
(maybe this seems silly but I also need to know where and how should i place my files so they can be accessed from both the client and the server – my servlet will create some images(.jpg) and store them (where?) and will send the filenames back to a .jsp which will then show them in a colorbox)
thanks in advance.
edit
added some more lines of the error and
the call in DataManager.java:
public SheetInfo loadSheetInfoCSV(String filename){
....
br = new BufferedReader( new FileReader(filename));
String strLine = "";
//read comma separated file first line
if ((strLine = br.readLine()) != null)
....
The exception suggests that you were using
FileInputStreamto get anInputStreamof it. This is not the right way when the resource concerns a classpath resource (all places where you attempted to put it in are part of the classpath). You should get a classpath resource as a classpath resource usingClassLoader#getResourceAsStream(), not as a local disk file system resource usingFileInputStream.If the resource file
foo.extis placed in the same package as the class where you’re trying to load the resource (i.e. theDataManagerclass), then you can just use get anInputStreamof it by its sole filename as follows:Or when you’re inside the
staticcontext:If the resource
foo.extis placed in a different package than the class where you’re trying to load the resource, e.g. thecom.examplepackage, then you can get anInputStreamof it by its classpath-relative path as follows, where the leading slash/takes you to the root of the classpath:Also here, the
getClass()can be substituted by an arbitratyFoo.class, as long as the class is loaded by the sameClassLoaderwhich has access to the same package structure.An alternative is to use the context
ClassLoaderas obtained from the current thread. It has access to everything. You only can’t specify a classpath-relative path, it is always relative to the root of the classpath (so no leading slash/should be used):