Note that there are some explanatory texts on larger screens.

plurals
  1. POImporting Text File in Java Constructor
    primarykey
    data
    text
    <p>I am not sure where the problems is spawning from. I have two classes, one called testFriends class and another called GUIapp class. In each of the classes, my object is to import the file for use. In the testFriends class, I import the file in the main method, it parses it and everything works nicely. In my GUIapp class, I tried doing the same thing, but importing it in the constructor method and it tells me the file doesn't exists. Both of the files classes reside in the same src folder and I am using:</p> <pre><code>BufferedReader in = new BufferedReader(new FileReader(inputFile)); </code></pre> <p>where the inputFile is just a string "friends.txt".</p> <p>Here are the two classes:</p> <pre><code>//This class works public class temp { public static void main(String[] args) throws ParseException { try { System.out.println("hey"); BufferedReader in = new BufferedReader(new FileReader("friends.txt")); //create string buffer for reading String line = in.readLine(); //reading first line System.out.println(line); } catch (IOException e) { System.out.println("Fail Import."); } } } //////////The one below doesn't... public class GUIapp extends JApplet{ //** PANEL **// private JPanel outerPanel; //** Button **// private JButton button1; /* * Constructor for Getting all the friends set up */ public GUIapp() throws ParseException, FileNotFoundException{ BufferedReader in = new BufferedReader(new FileReader("friends.txt"));//Error Line } /* * Create Stuff */ public void createStuff() { outerPanel = new JPanel(); //create outer panel button1 = new JButton("Click Me"); outerPanel.add(button1,BorderLayout.SOUTH); } /* * Initialize Stuff */ public void init(){ createStuff(); //initialize create stuff this.add (outerPanel); } } </code></pre> <p>Any ideas why when both are working in the same directory, one can read while the other can't?</p> <p>Thanks,</p> <p>EDIT: Below is the exception thrown when I run the GUIapp Class:</p> <pre><code>java.io.FileNotFoundException: friends.txt (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:120) at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:79) at java.io.FileReader.&lt;init&gt;(FileReader.java:41) at GUIapp.&lt;init&gt;(GUIapp.java:50) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at sun.applet.AppletPanel.createApplet(AppletPanel.java:807) at sun.applet.AppletPanel.runLoader(AppletPanel.java:714) at sun.applet.AppletPanel.run(AppletPanel.java:368) at java.lang.Thread.run(Thread.java:680) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload