0

I'm supposed to create a java applet project and in that I need to read data from .csv file and put it in a scrollable table. I tried to dothat and i get a FileNotFoundException exception.

Here is my code:

import java.applet.Applet;
import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

@SuppressWarnings("serial")
public class Test extends Applet
{  
   Table table;
   public void init()
   {
      try
      {
         table = new Table();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      this.add(table);
   }
}



class Table extends JPanel
{
   private String[][] productList;
   private JTable table;

   public Table() throws Exception
   {
      read();
      BorderLayout layout = new BorderLayout();
      layout.addLayoutComponent(new JScrollPane(table), BorderLayout.CENTER);

      setLayout(layout);
   }

   private void read() throws Exception
   {
      ArrayList<String[]> list = new ArrayList<String[]>();
      try
      {
         BufferedReader br = new BufferedReader(new FileReader("Products.csv"));
         String line = br.readLine();

         while ((line = br.readLine()) != null)
         {
            String[] toBeAdded = line.split(",");
            list.add(toBeAdded);
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }

      productList = new String[list.size()][3];
      for (int i = 0; i < productList.length; i++)
         for (int j = 0; j < productList[i].length; j++)
            productList[i][j] = list.get(i)[j];
   }
}

The products.csv is there in the root folder. I try to read in a normal non swing project and it works but not for this. Can anyone help me?

1 Answer 1

1

I can think of two potential problems:

  1. An applet can't read files located on the web server.
  2. If the file is packed inside a jar file, you need special code in order to read it. Specifically, you need a ClassLoader's getResourceAsStream.

For the second, the easiest way to get the ClassLoader's getResourceAsStream is to call Class's getResourceAsStream.

I haven't tested it, but something like this should work:

BufferedReader br = new BufferedReader(new InputStreamReader(Table.class.getResourceAsStream("/Products.csv")));

If that doesn't compile, change Table.class to this.getClass()

Note that the / is there to indicate that Products.csv is at the root of the .jar file. Otherwise, it will use the package name as a directory to look in (i.e. if the package was package this.is.my.boomstick;, it'd try to open this/is/my/boomstick/Products.csv)

Sign up to request clarification or add additional context in comments.

5 Comments

I tried it and it says that there is no such constructor BufferedReader(InputStream)
@HridayamBakshi Right, my mistake... you need to wrap it in an InputStreamReader first... I'll fix my answer in a second. Edit: Should be fixed now.
It is throwing a NullPointerException now
@AndrewThompson Well, the NPE is likely because Table.class.getResourceAsStream isn't finding the file in question, which would mean this answer is in some form wrong.
As far as I am concerned, the question was well answered in the suggestion to change from using a File, which will not work with an applet (especially not with a resource on the server!). Too often you get these things dragging on until the OP has successfully (loaded the resource and) deployed the applet - help desk style. My aim is to convince people to recognize that one task 'deploy applet successfully' is actually made up of many small steps. All said of course, 'IMO'. (+1 for your answer, BTW.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.