Results 1 to 6 of 6
  1. #1
    Landon is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default text file into a string?

    Hi,
    I need to turn a .txt file into a string? so I can then loop through and get the names out and their corresponding test scores. This is what i currently have, I need it to be in a string instead of printing out, once I got the string I'll work on writing the loops. Thanks.

    Java Code:
    import java.io.*;
    import java.util.*;
     
    public class ReadTextFileExample
    {
        public static void main(String[] args)
        {
            //Prompt user for filename
            System.out.println("Please type file name and press Enter: ");
            BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
            String filename = null;
            try 
            {
             filename = kbd.readLine();
            } 
            catch (IOException e) 
            {
             System.out.println("Filename was not correct.");
             System.exit(0);
            }
            
            File file = new File("filename");
            StringBuffer contents = new StringBuffer();
            BufferedReader reader = null;
     
            try
            {
                reader = new BufferedReader(new FileReader(filename));
                String text = null;
     
                // repeat until all lines are read
                while ((text = reader.readLine()) != null)
                {
                    contents.append(text).append(System.getProperty("line.separator"));
                }
            } 
            
            catch (FileNotFoundException e)
            {
                System.out.println("The filename entered was not found, program will now terminate.");
            } 
            
            catch (IOException e)
            {
                System.out.println("The filename entered was not found, program will now terminate.");
            } 
            
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.close();
                    }
                } catch (IOException e)
                {
                    System.out.println("The filename entered was not found, program will now terminate.");
                }
                
            }
            
            System.out.println(contents.toString());
        }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You actually already have the contents as a string.

    Notice this line:
    Java Code:
    System.out.println(contents.toString());

    That's what prints out the contents. Here, you can do anything with contents (which is a StringBuffer variable--read here for the API).

    Did you get this code from elsewhere? If so you should make sure you understand the purpose of each variable and block once using it.

  3. #3
    Landon is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the reading material, if I were to write
    Java Code:
    String newStr = contents.toString();
    would newStr then be a string with the contents of the .txt file that I could work with from there?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Landon View Post
    Thanks for the reading material, if I were to write
    Java Code:
    String newStr = contents.toString();
    would newStr then be a string with the contents of the .txt file that I could work with from there?
    What happens when you try it?

    kind regards,

    Jos

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    That is correct. ;)

  6. #6
    Landon is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Awesome, thanks a lot guys. Especially for reference material Zack.

Similar Threads

  1. get string from a text file
    By Kruptein in forum New To Java
    Replies: 11
    Last Post: 08-14-2009, 05:45 PM
  2. how to store string in text file
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2009, 07:21 AM
  3. How to write a string middle of a text file?
    By loggen in forum New To Java
    Replies: 5
    Last Post: 12-19-2008, 09:48 AM
  4. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 10:50 AM
  5. Searching a String from Text file using Swings .
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 4
    Last Post: 09-09-2008, 06:29 AM

Tags for this Thread

Posting Permissions

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