Friday, 15 May 2015

Importing Text File in Java Constructor -



Importing Text File in Java Constructor -

i not sure problems spawning from. have 2 classes, 1 called testfriends class , called guiapp class. in each of classes, object import file use. in testfriends class, import file in main method, parses , works nicely. in guiapp class, tried doing same thing, importing in constructor method , tells me file doesn't exists. both of files classes reside in same src folder , using:

bufferedreader in = new bufferedreader(new filereader(inputfile));

where inputfile string "friends.txt".

here 2 classes:

//this class works public class temp { public static void main(string[] args) throws parseexception { seek { system.out.println("hey"); bufferedreader in = new bufferedreader(new filereader("friends.txt")); //create string buffer reading string line = in.readline(); //reading first line system.out.println(line); } grab (ioexception e) { system.out.println("fail import."); } } } //////////the 1 below doesn't... public class guiapp extends japplet{ //** panel **// private jpanel outerpanel; //** button **// private jbutton button1; /* * constructor getting friends set */ 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); } }

any ideas why when both working in same directory, 1 can read while other can't?

thanks,

edit: below exception thrown when run guiapp class:

java.io.filenotfoundexception: friends.txt (no such file or directory) @ java.io.fileinputstream.open(native method) @ java.io.fileinputstream.<init>(fileinputstream.java:120) @ java.io.fileinputstream.<init>(fileinputstream.java:79) @ java.io.filereader.<init>(filereader.java:41) @ guiapp.<init>(guiapp.java:50) @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:39) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:27) @ java.lang.reflect.constructor.newinstance(constructor.java:513) @ java.lang.class.newinstance0(class.java:355) @ java.lang.class.newinstance(class.java:308) @ sun.applet.appletpanel.createapplet(appletpanel.java:807) @ sun.applet.appletpanel.runloader(appletpanel.java:714) @ sun.applet.appletpanel.run(appletpanel.java:368) @ java.lang.thread.run(thread.java:680)

at run time applet doesn't have file looking for. create sure file nowadays @ run time.

please add together method code can file

public void readfile(string filetoread){ string line; url url = null; try{ url = new url(getcodebase(), filetoread); } catch(malformedurlexception e){} try{ inputstream in = url.openstream(); bufferedreader bf = new bufferedreader(new inputstreamreader(in)); // business logic here } catch(ioexception e){ e.printstacktrace(); } }

/* * constructor getting friends set */

public guiapp() throws parseexception, filenotfoundexception{ readfile("friends.txt") }

for more details @ images javase

to create image object uses a.gif image file under imgdir, applet can utilize next code:

image image = getimage(getcodebase(), "imgdir/a.gif");

java

No comments:

Post a Comment