11 Replies Latest reply: Jul 18, 2008 6:13 PM by 843785 RSS

    help with printwriter class

      what i am trying to do is use the printwriter to write to a file and also name that file.

      i already have the the code of what i want to write, but what i can't seem to find is how do i take the file name that the user wants to use and implement it into the printwriter. i can write to a file if i name the file name in my program,b ut not from user input.

      can you guys point me in the right direction on this one?

        • 1. Re: help with printwriter class
          I'm not sure I understand fully, but you want to write to a file, but have that file named from user input? From console it would be something similar to:
          Scanner input = new Scanner(System.in); //Would need to import java.util.*;
          System.out.print("Enter file name: ");
          String filename = input.next(); // Reads a line of input from the user and stores it as a string
          try {
               PrintWriter pw = new PrintWriter(new File(filename)); // Creates a new output stream for writing to said file
          } catch (FileNotFoundException fnfe) { // Catches filenotfoundexceptions, which would be improper syntax naming, etc.
          If I'm off on what you were looking for, let me know.

          Edited by: Aeauvian on Jul 18, 2008 2:20 PM
          • 2. Re: help with printwriter class
            yeah that is right on the money

            but i am getting an error
            package conefilestorer;
            import java.io.File;
            import java.io.PrintWriter;
            import java.io.FileNotFoundException;
            import java.util.Scanner;
            public class ConeFileStorer {
                public static void main(String [] args)throws FileNotFoundException {
                 String fileName = null;
                    final int NUM_CONES = 7;
                    System.out.println("Enter the name of the input file");
                   Scanner keyIn = new Scanner(System.in);
                   fileName = keyIn.next();
                      PrintWriter fileOut = new PrintWriter(new File (fileName));
                  }catch(FileNotFoundException fnfe){
                    for (int count = 1; count <= NUM_CONES; count++)
                      IceCreamCone aCone = new IceCreamCone();
                      aCone.resetScoops(2 * count);
                      fileOut.println("Cone " + count + " has " +  <----------- symbol not found for fileOut.println
                             aCone.getScoops() + " scoops");
                   fileOut.close();  <---------- symbol not found same as above 
            any ideas on that?
            • 3. Re: help with printwriter class
              fileOut is undefined, because you don't define it.

              edit: sorry, I see now that you defined fileOut, but its definition is local to the try block in which it was defined. Try this:
              import java.io.*;
              public class PrintWriterExample {
                  public static void main(String[] args) throws IOException {
                      PrintWriter out = new PrintWriter(new File ("test.txt"));
              • 4. Re: help with printwriter class
                Your problem has to do with scope. You initialized fileOut inside a try/catch block, but just like if you were to initialize it in a for loop, anything outside that loop won't see it. One possible fix would be to put that for loop IN the try/catch block. Another option would be to initialize PrintWriter fileOut = null; before the try/catch block, but that would be problematic because if your try/catch block caugh an exception, it would try to write out to a null PrintWriter.

                Edited by: Aeauvian on Jul 18, 2008 3:35 PM Beat me :)
                • 5. Re: help with printwriter class
                  okay got it to run i put the for loop in the catch method, but i have a question

                  So where does this write to? or where at on my computer would this be?
                  • 6. Re: help with printwriter class
                    It, of course, depends on the string you enter for filename.
                    • 7. Re: help with printwriter class
                      oh so do i need to write c:\\mycone.txt

                      or something like that?
                      • 8. Re: help with printwriter class
                        got it never mind on that one
                        awesome once again you guys have helped me out

                        program works just like it should
                        thanks a lot guys
                        • 9. Re: help with printwriter class
                          You are asking: what strings can be passed to File's constructor? It can be a relative path or an absolute path.
                          • 10. Re: help with printwriter class
                            and if you're typing a file location on a windows system you don't have to escape a backslash like this
                            you can simply use a forward slash in it's place and java will interpret it.
                            • 11. Re: help with printwriter class
                              nice i didn't know about the forward slash. thanks for that tip