Trying to print the lines after writing them to an ouput file (java) -
i have problem. i'm asked use method print output file. start off began filling output file lines..then when tried read them , print them on screen there slight problem. used 'debug' option , problem turned out within ' line = input.nextline()' line of code don't know why..i mean that's how read output file...help appreciated.
here's work far:
import java.util.*; import java.io.*; public class problem_3 { public static void main(string[] args) { printwriter outfile = null; file f1 = new file("try.txt"); try { outfile = new printwriter("try.txt"); outfile.println("first line!"); outfile.println("second line!"); outfile.println("third line!"); cat(f1); } catch (exception e) { outfile.print("(1) exception: " + e.getmessage()); // no such element exception } outfile.close(); } /* * outfile = new printwriter(f1); outfile.println("line 1"); * outfile.println("line 2"); outfile.println("line 3"); outfile.print(""); * cat(f1); } catch (exception e) { system.out.println("(1)exception: " + * e.getmessage()); } outfile.close(); } */ public static void cat(file file) throws filenotfoundexception { scanner input = null; string line = ""; input = new scanner(new filereader(file)); //line = input.next(); // line = input.nextline();// line calls exception in main method while ((line != null)) { system.out.println("in while loop"); system.out.println("line 323" + line); return; } input.close(); } }
modify code be:
try { outfile = new printwriter("try.txt"); outfile.println("first line!"); outfile.println("second line!"); outfile.println("third line!"); outfile.close(); } catch (exception e) { outfile.print("(1) exception: " + e.getmessage()); // no such element exception } cat(f1);
you can't read written before close file.
Comments
Post a Comment