Results 1 to 11 of 11
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default How to write bytes to text file?

    Java Code:
    File myFile = new File("c://try.txt");
    		
    			FileOutputStream myStream = new FileOutputStream(myFile);
    			String mike = "mike";
    			byte[] myByteArray = mike.getBytes();
    			
    			myStream.write(myByteArray, 0, myByteArray.length);
    			
    			for(int i = 0; i<myByteArray.length;i ++)
    			{
    				System.out.println(myByteArray[i]);
    			}
    			myStream.close();
    When I run this code
    I get the text "Mike" in my try.txt file
    and I get

    109
    105
    107
    101

    on the console line.

    I want to write the bytes ( the 109, 105.. ) to the text file.

    How do I do that?

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to write bytes to text file?

    You don't write bytes to a text file, makes no sense. You write text to a text file. Otherwise perhaps you want write to a binary file.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to write bytes to text file?

    Thanks.

    so write() method of OutputStream gets bytes, converts them to appropriate text.

    read() method of InputStream reads texts, stores them as bytes.

    Correct ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: How to write bytes to text file?

    What does the API doc say for those methods?

    Everything in the computer is stored in bytes. It how those bytes are used that determines if they are part of an int or part of a String.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to write bytes to text file?

    Quote Originally Posted by Norm View Post
    It how those bytes are used that determines if they are part of an int or part of a String.
    Sorry ?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: How to write bytes to text file?

    Every thing is stored in the computer and on disk files as bytes. You can use the same byte value in an int and in a String.
    If you look at any byte from the computer you can not tell if the byte is from an int or from a String. The meaning of the contents of a byte depends on what the code is using it for.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to write bytes to text file?

    So a byte like 150 can be a character 'x' or an integer 4 ? ( For example ) Right ?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: How to write bytes to text file?

    Your examples are not correct.
    Try something like this to see what the int value of a char is:
    System.out.println((int)'x');

    or the char for an int:
    System.out.println((char) 119);
    Last edited by Norm; 01-14-2012 at 02:51 PM.

  9. #9
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: How to write bytes to text file?

    Thnaks.

  10. #10
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Cool Re: How to write bytes to text file?

    Write this code in beginning of main() function...'
    Java Code:
    File nfile=new File("D:/output.txt");
    PrintStream out=new PrintStream(new FileOutputStream(nfile));
    System.setOut(out);
    '
    Your output will be stored in text file.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: How to write bytes to text file?

    @suyog53 You're a little late on answering this 8 months old question.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. write in a text file at the end
    By ahmakki in forum New To Java
    Replies: 0
    Last Post: 02-06-2011, 04:56 PM
  2. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  3. How to write text file into Array
    By venkat.ravala in forum New To Java
    Replies: 13
    Last Post: 11-19-2009, 04:59 PM
  4. How to read first 9 bytes and write in...?
    By aRTx in forum New To Java
    Replies: 8
    Last Post: 03-26-2009, 02:54 PM
  5. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM

Posting Permissions

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