Results 1 to 14 of 14
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Reading and writing to a file

    For this assignment just read a file and write to that same file and add the number 1 to each word, not numerically string wise. Punctuation doesn't matter. It works sort of, however when I run it to a text file it just makes the txt file blank. Any ideas? Thanks, oh and I can't change echo at all! I have 3 classes, writerDriver(main), echo, and writer. They are in order respectively.

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    public class WriterDriver {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            String fileName;
          Scanner nameReader = new Scanner(System.in);
          System.out.println("Enter a file name");
         fileName = nameReader.nextLine();
         Scanner scan = new Scanner(new FileReader(fileName));
         Writer d = new Writer(fileName);
         d.readLines();
    
         scan.close();
        }
    
    }
    Java Code:
     import java.util.Scanner;
      import java.io.*;
    
      public class Echo{
        String fileName; // external file name
        Scanner scan; // Scanner object for reading from external file
    
        public Echo(String f) throws IOException
        {
         fileName = f;
         scan = new Scanner(new FileReader(fileName));
       }
    
       public void readLines(){ // reads lines, hands each to processLine
         while(scan.hasNext()){
          processLine(scan.nextLine());
         }
         scan.close();
       }
    
       public void processLine(String line){ // does the real processing work
         System.out.println(line);
      }
     }
    Java Code:
     import java.util.Scanner;
      import java.io.*;
    import java.util.StringTokenizer;
    public class Writer extends Echo {
         PrintWriter writer = new PrintWriter(fileName);
        public Writer(String f) throws IOException {
        super(f);
    }
    
    
         public void readLines(){ // reads lines, hands each to processLine
        super.readLines();
        scan.close();
    
       }
    
        public void processLine(String line){ // does the real processing work
         StringTokenizer st = new StringTokenizer(line);
         while (st.hasMoreTokens()) {
             writer.print(st.nextToken+"1");
             writer.println("");
            
         }
    
     writer.close();
      }
    
    
    
    
    }
    Last edited by jigglywiggly; 03-08-2009 at 01:01 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    And your question is?

  3. #3
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    When I write to a file, like say C:\test.txt and inthere was "testing this thing" It should go to "testing1 this1 thing1" but instead I just get a blank text document,

  4. #4
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh also to help anyone, the error probably lies in the writer class.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    7

    Default

    Java Code:
     writer.print(st.nextToken+"1");
    This is not going to compile, because the syntax is incorrect. Is that the problem you're having?

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Well, since it's a PrintWriter, it's correct.

    You're real problem is that you are attempting to read and write, to the same file. Well, when you open an outputstream to a file it overwrites the original file, so your reader has nothing to read.

    You need to write to a new file, then delete the original and rename the new one to the old name.

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Okay if I wanted to write to a new file how would I go about doing that? I am a bit confused now, also it's okay if the new file is in the same directory.

  8. #8
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    I tried doing this
    Java Code:
    public class Writer extends Echo {
         PrintWriter writer = new PrintWriter("anything.txt");
        public Writer(String f) throws IOException {
        super(f);
    }
    But it doesn't do anything I thought it would create anew txt file called anything.txt? Anyone got any suggestions?

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    And it should. Are you looking in the right place?

    Add a
    Java Code:
    System.out.println(System.getProperty("user.dir"))
    right before that "new PrintWriter" statement to find out where that file is being created.

    Edit: Actually, since that statement is part of the class definition, you can't add that statement before, so add that statement as part of your constructor.

  10. #10
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    OOoh thanks for that tip
    C:\Users\JigglyWiggly\Documents\NetBeansProjects\W riterDriver

    It put the output there but still there is nothing in that file??? This is driving me insane lol.

  11. #11
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Wait wait it worked! haha you are the best

  12. #12
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Ehh one problem only the first line gets sent? like if I have a text file that goes like

    hello i jacked your wallet
    sorry bout that

    It would become hello1 i1 jacked1 your1 wallet1

    The second line does not go through? Any ideas?

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Because you are closing the writer after writing the first line. Remove the writer.close from processLine and put it at the end of readLines.

  14. #14
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Okay you are a life saver and I also added a [code]writer.println(""); after processline to account for new lines. You guys are awesome, seriously. It all works wooohooo.
    Final code:

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    public class WriterDriver {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            String fileName;
          Scanner nameReader = new Scanner(System.in);
          System.out.println("Enter a file name");
         fileName = nameReader.nextLine();
         Scanner scan = new Scanner(new FileReader(fileName));
         Writer d = new Writer(fileName);
         d.readLines();
    
         scan.close();
        }
    
    }
    Java Code:
     import java.util.Scanner;
      import java.io.*;
    
      public class Echo{
        String fileName; // external file name
        Scanner scan; // Scanner object for reading from external file
    
        public Echo(String f) throws IOException
        {
         fileName = f;
         scan = new Scanner(new FileReader(fileName));
       }
    
       public void readLines(){ // reads lines, hands each to processLine
         while(scan.hasNext()){
          processLine(scan.nextLine());
         }
         scan.close();
       }
    
       public void processLine(String line){ // does the real processing work
         System.out.println(line);
      }
     }
    Java Code:
     import java.util.Scanner;
      import java.io.*;
    import java.util.StringTokenizer;
    public class Writer extends Echo {
         
        PrintWriter writer = new PrintWriter("something.txt");
    
        public Writer(String f) throws IOException {
          super(f);
          System.out.println(System.getProperty("user.dir"));
    }
    
    
         public void readLines(){ // reads lines, hands each to processLine
        super.readLines();
        scan.close();
    writer.close();
       }
    
        public void processLine(String line){ // does the real processing work
         StringTokenizer st = new StringTokenizer(line);
         while (st.hasMoreTokens()) {
             writer.print(st.nextToken()+"1");
             writer.print(" ");
             
             
            
         }
    writer.println("");
     
      }
    
    
    
    
    }

Similar Threads

  1. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 03:05 PM
  2. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 04:34 AM
  3. writing/reading a Structure to/from a file
    By Farzaneh in forum New To Java
    Replies: 1
    Last Post: 08-31-2008, 02:21 PM
  4. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 11:41 AM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •