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: How do I print out the whole of my text file?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I print out the whole of my text file?

    Here's my code:

    public static void main(String[]args){
     
                   try{
                    FileReader fileReader = new FileReader("data_file/Contact.txt");
                    BufferedReader in = new BufferedReader(fileReader);
                    String currentContact = in.readLine();
                                    while(currentContact != null) {
                        JOptionPane.showMessageDialog(null, "Contact: \n" + currentContact);
                   //     System.out.println("Contact:" + currentContact);
                        currentContact = in.readLine();
                    }}
                catch (IOException e) {
                    e.printStackTrace();
                           }}}

    Here are the information in my text files:

    Mini Toon $38.00 Qty: 8
    Gatsby $45.00 Qty: 5

    My program only show the first line and upon clicking ok (using the JOption.Pane), then it shows the 2nd line.

    I would like to know how do I print or show the entire text file - the 2 lines all at once.

    Thanks.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do I print out the whole of my text file?

    Add the contracts to a StringBuilder in the loop and display the JOptionPane after the loop.

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I print out the whole of my text file?

    Quote Originally Posted by PhHein View Post
    Add the contracts to a StringBuilder in the loop and display the JOptionPane after the loop.
    OK. I heed your advice and added the StringBuilder but I still get the txt in 2 goes; not appearing once in the JOption.
    try{
                    FileReader fileReader = new FileReader("data_file/Contact.txt");
                    BufferedReader in = new BufferedReader(fileReader);
                    String currentContact = in.readLine();
                    StringBuilder sb = new StringBuilder();  
                    while(currentContact != null) {                   
                        sb.append(currentContact);
                        JOptionPane.showMessageDialog(null, "Contact: \n" + currentContact);
                   //     System.out.println("Contact:" + currentContact);                    
                                currentContact = in.readLine();
                    }}
                catch (IOException e) {
                    e.printStackTrace();
                           }}}

    Did I miss out anything?

    --- Update ---

    Hi,

    I modified the code a little and this time round is better - the information will all come out at once. The bad news is that it only appeared on the 2nd time. The first JOptionPane Message still give me the same old one line and not all.

    Here's my code and I hope someone can tell me where I have gone wrong:

     
     try{
                    FileReader fileReader = new FileReader("data_file/Contact.txt");
                    BufferedReader in = new BufferedReader(fileReader);
                    String currentContact = in.readLine();
                    StringBuilder sb = new StringBuilder();  
                    while(currentContact != null) {   
                       StringBuilder current = sb.append(currentContact);
                        current.append(System.getProperty("line.separator"));
                        JOptionPane.showMessageDialog(null, "Contact  : \n" + current)  ;
                   //     System.out.println("Contact:" + currentContact);                    
                           currentContact = in.readLine();
                    }} 
                catch (IOException e) {
                    e.printStackTrace();
                           }}}

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  2. How do I copy a simple text-file to a new text-file?
    By get703 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 5th, 2012, 10:59 PM
  3. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM
  4. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM