Reading CSV file

It’s my first post so I want to say hello to all :smiley:

I have problem with reading csv file into my Vaadin application. I moved csv file to classes folder in WAR file.


package com.example.csvreadertest;

import java.io.FileReader;
import au.com.bytecode.opencsv.CSVReader;
import com.vaadin.Application;
import com.vaadin.ui.Window;

public class CsvreadertestApplication extends Application {

	public void init() {
		Window mainWindow = new Window("Csvreadertest Application");
		setMainWindow(mainWindow);
		String file = "test.csv";

		try {

			CSVReader r = new CSVReader(new FileReader(file));

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

	}

}

it’s test class, but it doesn’t work and I have no idea to fix it. I don’t know where I have to move csv file.

Exception:

20:46:45,653 ERROR [STDERR]
 java.io.FileNotFoundException: test.csv (The system cannot find the file specified)
20:46:45,654 ERROR [STDERR]
 	at java.io.FileInputStream.open(Native Method)
20:46:45,654 ERROR [STDERR]
 	at java.io.FileInputStream.<init>(FileInputStream.java:120)
20:46:45,655 ERROR [STDERR]
 	at java.io.FileInputStream.<init>(FileInputStream.java:79)
20:46:45,655 ERROR [STDERR]
 	at java.io.FileReader.<init>(FileReader.java:41)
20:46:45,655 ERROR [STDERR]
 	at com.example.csvreadertest.CsvreadertestApplication.init(CsvreadertestApplication.java:19)
20:46:45,655 ERROR [STDERR]
 	at com.vaadin.Application.start(Application.java:554)
20:46:45,656 ERROR [STDERR]
 	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1208)
20:46:45,656 ERROR [STDERR]
 	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:484)
20:46:45,656 ERROR [STDERR]
 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
20:46:45,657 ERROR [STDERR]
 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
20:46:45,657 ERROR [STDERR]
 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
20:46:45,658 ERROR [STDERR]
 	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
20:46:45,658 ERROR [STDERR]
 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
20:46:45,658 ERROR [STDERR]
 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
20:46:45,658 ERROR [STDERR]
 	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
20:46:45,659 ERROR [STDERR]
 	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
20:46:45,659 ERROR [STDERR]
 	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
20:46:45,659 ERROR [STDERR]
 	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
20:46:45,659 ERROR [STDERR]
 	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
20:46:45,660 ERROR [STDERR]
 	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
20:46:45,660 ERROR [STDERR]
 	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
20:46:45,660 ERROR [STDERR]
 	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
20:46:45,660 ERROR [STDERR]
 	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
20:46:45,661 ERROR [STDERR]
 	at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
20:46:45,661 ERROR [STDERR]
 	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
20:46:45,661 ERROR [STDERR]
 	at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
20:46:45,661 ERROR [STDERR]
 	at java.lang.Thread.run(Thread.java:662)

Welcome, but not a Vaadin question, methinks.

You should at least try to debug rather than post to the list. It seems the error is very clear, you just don’t know where to place the CSV file. I suggest you create a java.io.File first with the filename, then use it’s getAbsolutePath() to see where it thinks the file should be when you don’t specify anything. In general, it’s probably something in your web server’s home.

First, putting it in classes is not the right thing to do, because a CSV is not a class… Put them somewhere under /WEB-INF.
See
this previous message in the forum

With respect to your question: In order to read a file that has been included in a WAR, the proper way is to use the getResourceAsStream that is exposed by the ServletContext.
This will give you a Stream, and your application needs a Reader. So you need to do something like this

InputStream stream = servletContext.getResourceAsStream("/WEB-INF/resources/file.csv");
CVSReader myReader = new CVSReader(new InputStreamReader(stream));

Now, you ask, how does one get the ServletContext ? A utility method such as this one should do the trick (mine is located in my Application subclass).

     /**
     * Return the servlet container context
     */
    public ServletContext getServletContext() {
        ApplicationContext ctx = this.getContext();
        if (ctx == null) return null;
        final ServletContext sCtx = ((WebApplicationContext) ctx).getHttpSession().getServletContext();
        return sCtx;
    }

Thank you for advices. I moved CSV file to bin directory in JBoss server and it’s working. It was very hard for me to find how Vaadin app works in server architecture, but your solutions helped to get needed knowledge. Thank you again.

Vaadin works exactly like a standard J2EE application. J2EE is very logical, problem is knowing where to start. Vaadin hides the complexity for many of these things (see for example the FileResource class, but there are things where there is not much choice – you must learn what is behind the scenes). Happy that my post was useful to you.

BTW, a “bin” directory is not a good choice either. Traditionnally, bin is used for binary files meant to be executed by the computer. Files like your CSV are best placed in a “resources” directory. Or a “resources/templates” directory.