Results 1 to 11 of 11
Thread: How to write bytes to text file?
- 01-14-2012, 12:17 AM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
How to write bytes to text file?
When I run this codeJava 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();
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.
-
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.
- 01-14-2012, 12:41 AM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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 ?
- 01-14-2012, 01:55 AM #4
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.
- 01-14-2012, 09:12 AM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
- 01-14-2012, 12:42 PM #6
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.
- 01-14-2012, 02:36 PM #7
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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 ?
- 01-14-2012, 02:46 PM #8
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.
- 01-14-2012, 02:47 PM #9
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: How to write bytes to text file?
Thnaks.
- 09-18-2012, 07:13 PM #10
Member
- Join Date
- Sep 2012
- Location
- Navi Mumbai
- Posts
- 30
- Rep Power
- 0
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.
- 09-18-2012, 07:46 PM #11
Similar Threads
-
write in a text file at the end
By ahmakki in forum New To JavaReplies: 0Last Post: 02-06-2011, 04:56 PM -
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 05:31 AM -
How to write text file into Array
By venkat.ravala in forum New To JavaReplies: 13Last Post: 11-19-2009, 04:59 PM -
How to read first 9 bytes and write in...?
By aRTx in forum New To JavaReplies: 8Last Post: 03-26-2009, 02:54 PM -
DES algorithm (Read and Write bytes to file)
By JoaoPe in forum Advanced JavaReplies: 6Last Post: 07-29-2008, 03:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks