Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By jim829

Thread: Print text to a file.txt

  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Print text to a file.txt

    I'm not getting text in the Pledge.txt file. Do I need to assign the pledge to a String variable?

    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class ReadMe {
    
    	public static void main(String[] args) throws IOException{
    		File file = new File("Pledge.txt");
    		PrintWriter output = new PrintWriter(file);
    		try{
    			if(file.exists()){
    			output.write("I pledge allegiance to the Flag of the United States of America, and to "
    					+ "the republic for which it stands: one Nation under God, indivisible, with "
    					+ "Liberty and Justice for all.");
    		}
    			else
    				System.out.println("File Does Not Exist!");
    				System.exit(0);
    		}
    		finally{
    			output.close();
    		}
    		
    		Scanner input = new Scanner(new File("Pledge.txt"));
    		//System.out.println(input.next());
    	try{
    		RandomAccessFile read = new RandomAccessFile("Pledge.txt", "r");
    		read.seek(124);
    		System.out.println("Letter 124: " + read.read());
    		read.seek(135);
    		System.out.println("Letter 135: " + read.read());
    		read.close();
    	}
    		catch(FileNotFoundException ex){
    			ex.printStackTrace();
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Print text to a file.txt

    What is written to the file? Can you open it in a hex editor to see?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Print text to a file.txt

    Quote Originally Posted by Norm View Post
    What is written to the file? Can you open it in a hex editor to see?
    The file is blank. Hex editor? Do you know of a hex editor I can use for mac?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Print text to a file.txt

    The file is blank.
    What does that mean? The file is full of spaces? How many spaces are there? Or do you mean empty?

    This is a problem with code that didn't use {}s with if and else statements and with poorly placed {}s
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Print text to a file.txt

    Meaning that there were zero bytes.

    Brackets, freakin brackets. I put them in and viola! So, to take something away from this post, System.exit(0) wipes any content from a program or file?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Print text to a file.txt

    System.exit(0) wipes any content from a program or file
    If the output buffers have not been written to the disk yet, System.exit() could end the program BEFORE they are written.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Print text to a file.txt

    The problem is with your braces. Your nesting is incorrect. You need a brace after the else which precedes your finally clause. Then you need
    one more after your finally clause. And System.exit(0) does not wipe your file. You never did the close which would normally flush the output buffer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Print text to a file.txt

    The console output returns ' ? ' ... I'm trying to return the char at each position in the txt file.

    Java Code:
    try{
    		RandomAccessFile read = new RandomAccessFile("Pledge.txt", "r");
    		read.seek(124);
    		System.out.println("\nLetter 124: " + read.readChar());
    		read.seek(135);
    		System.out.println("Letter 135: " + read.readChar());
    		read.close();
    	}
    		catch(FileNotFoundException ex){
    			ex.printStackTrace();
    		}

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Print text to a file.txt

    The ? probably means the char is not printable. What do you expect to printed?
    Cast the char to an int by adding (int) before the readChar() to see what is being read. The output will allow you to see what was read.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Print text to a file.txt

    Quote Originally Posted by Norm View Post
    The ? probably means the char is not printable. What do you expect to printed?
    Cast the char to an int by adding (int) before the readChar() to see what is being read. The output will allow you to see what was read.
    I did what you said and got this result:

    Letter 124: 28516
    Letter 135: 26978

    Is this the UNICODE representation of the txt string?

    I would like the char at each pointer location to show. By using the seek method we are to seek out the location in the string and return that char in the console.
    Last edited by javaStooge; 05-02-2014 at 05:34 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Print text to a file.txt

    A char is two bytes. Was the file written to have 2 bytes per char? That would be as unicode.
    Try using the read() method that reads one byte vs 2 bytes and print that. Read the byte into an int variable and print that variable as int and also print it cast to char (char) to see what is read and what format it is in.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Print text to a file.txt

    Did you do what I said in post #7. I took you original code, made those changes, and it worked.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Print text to a file.txt

    Quote Originally Posted by jim829 View Post
    Did you do what I said in post #7. I took you original code, made those changes, and it worked.

    Regards,
    Jim
    Yes, I added the additional brace to the else and the program prints the text to the file perfectly. Now the problem is returning a char to the console instead of some number or '?' depending on which (wrong) code I use. I looked at the oracle website and when it comes to Random Access Files, the readChar() should return a char from the text file.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Print text to a file.txt

    the readChar() should return a char from the text file.
    See the API doc for that method.
    Was the file written to hold char values?
    Look at the file's properties. How many bytes does it have?
    edit the file and count the number of characters.
    If the file is in unicode there will be 2 times the bytes in the file as the number of characters shown in an editor.
    Last edited by Norm; 05-02-2014 at 06:18 PM.
    javaStooge likes this.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Print text to a file.txt

    Ok, I printed out numbers too. But just now, I cast the value to char and got readable lowercase letters.

    Java Code:
             System.out.println("Letter 124: " + (char)read.read());
             read.seek(135);
             System.out.println("Letter 135: " + (char)read.read());
             read.close();
    Regards,
    Jim
    javaStooge likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Print text to a file.txt

    See the API doc for how the two read methods differ.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Print text to a file.txt

    I'm just curious, why did readChar() not work? Is it because of what Norm said about whether or not the file was written to hold char values, since it is a string?

    Thank you very much! My brain is turning into a black hole, trying to absorb everything and just scrambling all the data!

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,017
    Rep Power
    6

    Default Re: Print text to a file.txt

    Probably because you did not write you file in unicode characters which are 16 bits wide. readChar() reads a 16 bit character. Read, reads a single byte. The latter is what you wanted based on how you wrote the file. I suspect that is due to the platform's default character encoding.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Cannot print text from a file to a JtextArea
    By stevenfriz in forum New To Java
    Replies: 11
    Last Post: 02-07-2012, 03:51 AM
  2. how to print text file
    By Johnson in forum New To Java
    Replies: 2
    Last Post: 05-09-2011, 08:21 PM
  3. Text file into an array then Print
    By Mrussell9 in forum New To Java
    Replies: 0
    Last Post: 05-06-2011, 02:28 PM
  4. trying to print a text file.
    By IYIaster in forum New To Java
    Replies: 1
    Last Post: 10-12-2009, 07:55 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM

Posting Permissions

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