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

Thread: Reading in entire contents of text file to one string

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading in entire contents of text file to one string

    Hi Guys ,

    i have a text file Content.txt which only contains the letter 'a'

    i want to create a method which will read in all contents of the text document and store it in one string

    i have been using

           ArrayList<String> al = new ArrayList<String>();
           int count =0;
           String inputtext = "";
           Object[] StringElements;
         try{
            FileInputStream fstream = new FileInputStream(InputFile);
            // 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) //loop through each line
                {
                // Print the content on the console
                al.add(count, strLine); // Store the text file in the string
                ++count; //incrament count
                }
            in.close();//Close the input stream
            StringElements = al.toArray(); //convert al array to object
     
            for (int a = 0; a < StringElements.length; a++) {
           inputtext += StringElements[a];
           }
     
     
            }
            catch (Exception e)
            {//Catch exception if any
            System.out.println("Error: " + e.getMessage());
            }
           return inputtext;




    however when i print out the string i get 2 charcters 1 is a weired apostrophie dot thing and the letter a . i have checked the text file and only 'a' is in there See pic below





    Do you guys know why i am getting this can you help


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in entire contents of text file to one string

    i have looked everywhere and that weired dot is n ot in my program at all i think the read in text file thing is boogerd

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in entire contents of text file to one string

    i have chop[ped down the method but i still get that strange charcter
        public static String Inputfile2String(String InputFile) {
     
            String inputtext = "";
     
            try {
                FileInputStream fstream = new FileInputStream(InputFile);  // 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) //loop through each line
                {              
                    inputtext += strLine; // Store the text file in the string
                }//end of while
     
            } catch (Exception e) {//Catch exception if any
                System.out.println("Error: " + e.getMessage());
            }
            return inputtext;
        }//end of Inputfile2String method

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Reading in entire contents of text file to one string

    Do something for us.

    After you get the String, do the String.charAt(0) method, cast it as an int, and print out the result.That should give us an idea of its ASCII values and then we can use the ASCII Table (Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion) to figure out what the heck it is.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in entire contents of text file to one string

    i think its 250 on the secound ascii table

    the results of

    [code]
    System.out.println("input ="+inputtext);

    System.out.println("it is"+inputtext.charAt(0));

    System.out.println("it is"+(int) inputtext.charAt(0));
    System.out.println("it is"+(byte) inputtext.charAt(0));
    [code]


    it is 
    it is65279
    it is-1


    alt 250 = and it seems pretty much the same character

    it wont allow me to paste the chacrter in from the netbeans output window

  6. #6
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in entire contents of text file to one string

    i can use this bit of code to get it work

         if (encrypt == true) {
                    if(inputtext.charAt(0)=='')
                    {
                    inputtextarray = inputtext.toCharArray();
                    inputtext = "";
                    for (int p = 1; p < inputtextarray.length; p++) // remove the first charcter from the input plaintext
                    {
                        inputtext += inputtextarray[p];
                    }
                    }
                }

    nopt all the time that charcter is found
    Last edited by fortune2k; December 6th, 2010 at 12:13 PM.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Reading in entire contents of text file to one string

    The character is a Unicode Byte Order mark (BOM). It is used to indicate the byte order of a text file.

    If you want information about it, I found this supposable fix: comp.lang.java.programmer | Google Groups
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in entire contents of text file to one string

    im strugglig to get my program to only read in the text which can be input on a english keyuboard

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,629
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Reading in entire contents of text file to one string

    Try this:
    BufferedReader br = new BufferedReader(new FileReader(InputFile));

    instead of:
     FileInputStream fstream = new FileInputStream(InputFile);  // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
     
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

    Unless of course you are using Streams for any particular reason. If you are trying to read the file from within its own jar, use this:
    BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(InputFile)));

    If you are trying to read from other jars or zip files, it will have to be accessed differently.

    EDIT:
    OR, use this:
    Last edited by aussiemcgr; December 6th, 2010 at 01:38 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading in entire contents of text file to one string

    I'd suggest using the Scanner and StringBuilder classes.

Similar Threads

  1. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 04:31 PM
  2. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 10:56 AM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 05:16 PM