java - Processing a file with JFileChooser -
hey guys running issue program. trying program show text files, , once user selects one, file information should displayed in textbox in gui. getting error:
filechooserdemo3.java:66: error: unreported exception ioexception; must caught or declared thrown while ((strline = br.readline()) != null) {
why happening? have catch statement.. help!
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.scanner; import java.io.*; class filechooserdemo3{ jlabel jlab; jbutton jbtnshow; jfilechooser jfc; jtextarea jta; jscrollpane scrollpane; filechooserdemo3() { //create new jframe container. jframe jfrm = new jframe("jfilechooser demo"); //specify flowlayout layout manager jfrm.setlayout(new flowlayout()); //give frame initial size jfrm.setsize(800,800); //end program when user closes application jfrm.setdefaultcloseoperation(jframe.exit_on_close); //create label show selected file jlab=new jlabel(); //create button show dialog jbtnshow = new jbutton("show file chooser"); //create textarea ability textwrap (p889-891) , scroll (hint: use jscrollpane) jtextarea textinput = new jtextarea(20, 40); textinput.setlinewrap(true); jscrollpane scrollpane = new jscrollpane(textinput); //create file chooser starting @ default directory jfc=new jfilechooser(); //show file chooser when show file chooser button pressed jbtnshow.addactionlistener(new actionlistener() { public void actionperformed(actionevent le) { //pass null parent. centers dialog on screen. int result = jfc.showopendialog(null); if(result==jfilechooser.approve_option){ jlab.settext("selected file is: " + jfc.getselectedfile().getname()); //get selected file stored file. try{ //do file processing here string strline; file selectedfile = jfc.getselectedfile(); fileinputstream in = new fileinputstream(selectedfile); bufferedreader br = new bufferedreader(new inputstreamreader(in)); while ((strline = br.readline()) != null) { textinput.append(strline + "\n"); } } catch(filenotfoundexception e){ system.out.println("exception"); } } else{ jlab.settext("no file selected."); } } }); //add show file chooser button , label content pane jfrm.add(jbtnshow); jfrm.add(jlab); //display frame jfrm.setvisible(true); } public static void main(string[] args){ //create gui on event dispatching thread. swingutilities.invokelater(new runnable() { public void run() { new filechooserdemo3(); } }); } }
you catching filenotfoundexception, need catch ioexception after try {} block.
programmatically, because readline declares throws ioexception. translated, it's saying after file opened, still encounter problem reading file.