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 3 of 3

Thread: I need help with this file reader code

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help with this file reader code

      try{
       // Open the file that is the first 
       // command line parameter
       FileInputStream fstream = new FileInputStream("Inventory2.dat");
       // Get the object of DataInputStream
       DataInputStream in = new DataInputStream(fstream);
       BufferedReader br = new BufferedReader(new InputStreamReader(in));
       String strLine;
       //Read File Line By Line
       while ((strLine = br.readLine()) != null)   {
       // Print the content on the console
       ind.append(strLine);
       }
       //Close the input stream
       in.close();
         }catch (Exception e){//Catch exception if any
       System.err.println("Error: " + e.getMessage());
       }

    The code works but it does not display the information correctly. its in .dat file as:
    Item Number
    item Quantity
    Item Color

    but instead it shows up as :

    ItemNumberItemQuantityItemColor

    how do i change it to show up correctly?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help with this file reader code

    Where does the code print out what you show in the post?

    If you want endline characters between the lines read from the file so each line will display on a new line, you need to append the endline character("\n") to the String after each line that is read.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with this file reader code

    Thank you very much that helped. I should have seen that.

Similar Threads

  1. I am the most terrible file reader.....
    By seaofFire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2012, 07:42 PM
  2. FILE READER PROGRAM
    By shubhen in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: July 22nd, 2011, 02:31 AM
  3. Java IO File Reader Question???
    By liamShannon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 12th, 2011, 03:37 PM
  4. greetings and a file reader problem
    By chileshe in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 6th, 2009, 03:45 AM
  5. Code to read a character in the file
    By Truffy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 19th, 2009, 06:11 PM