java - Passing FileWriter as a parameter to a method -


i'm sure there simple answer question, here go.

i'm trying use filewriter write text file. program reads text in existing file, specified user , asks whether print text console or new file, named user.

i believe problem passing filewriter "fileorconsole" method. not passing or declaring filewriter in "fileorconsole" method correctly? file created nothing written it.

here code:

import java.io.*; import java.util.*;  public class reader {  public static void main(string[] args) throws ioexception {     scanner s = null, input = new scanner(system.in);     bufferedwriter out = null;      try {         system.out.println("would read file?");         string answer = input.nextline();          while (answer.startswith("y")) {             system.out.println("what file read from?");             string file = input.nextline();             s = new scanner(new bufferedreader(new filereader(file)));              system.out                     .println("would print file output console or file?");             fileorconsole(input.nextline(), s, input, out);             system.out                     .println("\nwould read file again?");             answer = input.nextline();         }         if (!answer.equalsignorecase("yes")) {             system.out.println("goodbye!");         }      } catch (ioexception e) {         system.out.println("error! file not found!");         // e.printstacktrace();     } {         if (s != null) {             s.close();         }         if (out != null) {             out.close();         }     } }  public static void fileorconsole(string response, scanner s, scanner input,         bufferedwriter out) {     if (response.equalsignorecase("console")) {         while (s.hasnext()) {             system.out.println(s.nextline());         }     } else if (response.equalsignorecase("file")) {         system.out.println("name of output file?");         response = input.nextline();         try {             out = new bufferedwriter(new filewriter(response));         } catch (ioexception e) {             e.printstacktrace();         }         while (s.hasnext()) {             try {                 out.write(s.nextline());                 out.newline();             } catch (ioexception e) {                 e.printstacktrace();             }         }         try {             out.close();         } catch (ioexception e) {             e.printstacktrace();         }     } else {         system.out.println("sorry, invalid response. file or console?");         response = input.nextline();         fileorconsole(response, s, input, out);     }   } } 

you make classic error forgetting parameters passed value in case of java value of reference. thing assignment

out = new bufferedwriter(new filewriter(response)); 

actually not change variable declared in main() stays null

bufferedwriter out = null; , in skips close() if(out==null) , buffered , no flush nothing written file. got out.close(); in side fileorconsole method call

or

do out = new bufferedwriter(new filewriter(response)); outside of it. choose :-)


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -