Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Using the fileReader?

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using the fileReader?

    I am trying to read 3 lines from a .txt file and then write those lines to another created .txt file. The problem I have is that the compiler doesn't find my file. I have the .java and .class and the .txt file in the same exact location (a folder in my desktop). What am I doing wrong? Here's what I have started:
    import java.io.*; 
     
    public class FileCopyDemo0 { 
     
      public static void main(String args[]) throws IOException {
            File file = new File("ppp.txt");
    	FileReader fr = new FileReader("ppp.txt"); 
    	BufferedReader br = null;
     
        br = new BufferedReader(fr); 
        String s; 
        while((s = br.readLine()) != null) { 
          System.out.println("line read was: " + s);
        } 
        fr.close(); 
      } 
    }

    Also, why would I be getting a nullPointerException at the br = new BufferedReader(fr); line?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Using the fileReader?

    As it stands, your code will work if everything is sharing the same directory.
    Not sure if it could be a classpath issue, but for now make sure the name of your text file is ppp and not ppp.txt (with .txt extension too, i.e. make sure your file isnt ppp.txt.txt )
    Just a guess.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using the fileReader?

    omg thank you so much. The issue WAS that it was named "ppp.txt.txt". I wish I would have found this out so long ago.

    THANKS AGAIN!!!

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    32
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using the fileReader?

    you would require to place the text file ppp.txt under the root directory of the project or you need to provide the full path of the text file when you are creating File Object. For example FileReader fr = new FileReader("E:\ \JavaApplication2\ppp.txt"); or File file = new File("E:\ \JavaApplication2\ppp.txt ");

    Hope u satisfied with this.
    and thanks

Similar Threads

  1. my FileReader(i think) isn't performing properly..
    By weej25aya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2011, 10:06 PM
  2. Scanner/FileReader help
    By JohnTravoltaire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 27th, 2011, 11:28 PM
  3. FileReader help
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 5th, 2010, 07:29 AM
  4. [SOLVED] difference between fileReader and bufferReader
    By shadihrr in forum Java Theory & Questions
    Replies: 6
    Last Post: June 8th, 2010, 01:26 AM
  5. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 12:12 AM