Results 1 to 7 of 7
- 09-13-2009, 10:08 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
FileOutputStream, I dont know what Im doing wrong...
Hi!
I got this code, that first uses FileInputStream to get the files pure content
and put it all in one string like: "1100101011101010101" for example..
Now it can do that!
But once I use FileOutputStream and do wright for each one of those 1s and 0s
then it messes it up!
Casue if my txt file contained the text: "hello hjdskfhdsjkfh"
then the txt, after I'v run this, will look like: " "
Just alot of spaces! And sometimes it looks like ÄÖÅÅ..
It messes it all up..
Can you help me find the wrong thing?
Heres my code:
PHP Code:/* The reader of binary works. * But the enq_one is failing Big time! */ import java.util.Scanner; import java.io.*; public class myapp { public static void main(String[] args) throws IOException { String string = ""; int z = 0; try{ // TODO, add your application code //out.println("Wellcome to talk!"); File filey = new File("C:\\Users\\Andreas\\Desktop\\PhoneReg.txt"); FileInputStream file = new FileInputStream(filey); int max = file.available(); for (int x = 0; x<max;x++){ int value = file.read(); string = string+Integer.toBinaryString(value); max = file.available(); z++; } System.out.println(string); System.out.println("Amount of bites: "+z); //String enq1 = enqryt_one("0000111"); //System.out.println(enq1); replace(string,filey); } catch (Exception e){ System.out.println(e); } } private static void replace(String string, File filey)throws IOException { FileOutputStream file = new FileOutputStream(filey); String[] list = string.split(""); for (int x = 0; x < list.length;x++){ //System.out.println(list[x]); if (!list[x].equals("")){ //System.out.println("pl"); file.write(Integer.valueOf(list[x]).intValue()); } } file.close(); } }
- 09-14-2009, 07:49 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
First of all, when using the toBinaryString from Integer, make sure you left pad each of the bytes to eight digits as that method will only provided as many digits as needed to get the value. IOW if the value of the byte is 5 then toBinaryString returns "101", but, for it to be full byte in binary it should be "00000101".
Then, when you go to write them back, make sure you are "reading" that "binary string" in chunks of eight, then use Integer.valueOf(string, radix) ie. Integer.valueOf("00000101", 2), then cast that int to a byte and then write that byte.
- 09-14-2009, 07:57 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Thanks masijade! It really helped =)
Only one problem remaining..
Idk what radix is..
and my script is now actually wrighting some text in the txt file.
Tho it doesn't wright the correct text..
It wrights chines/japanese signs or just random letters...
Heres my script so far with the adjustments made:
PHP Code:/* The reader of binary works. * But the enq_one is failing Big time! */ import java.util.Scanner; import java.io.*; public class myapp { public static void main(String[] args) throws IOException { String string = ""; int z = 0; try{ // TODO, add your application code //out.println("Wellcome to talk!"); File filey = new File("C:\\Users\\Andreas\\Desktop\\PhoneReg.txt"); FileInputStream file = new FileInputStream(filey); int max = file.available(); for (int x = 0; x<max;x++){ int value = file.read(); String value2 = get_full(Integer.toBinaryString(value)); string = string+value2+","; max = file.available(); z++; } System.out.println(string); System.out.println("Amount of bites: "+z); //String enq1 = enqryt_one("0000111"); //System.out.println(enq1); replace(string,filey); } catch (Exception e){ System.out.println(e); } } private static String get_full(String v){ int length = v.length(); for (int x = 1; x < 9;x++){ if (x > v.length()){ v = v+"0"; } } return v; } private static void replace(String string, File filey)throws IOException { FileOutputStream file = new FileOutputStream(filey); String[] list = string.split(","); for (int x = 0; x < list.length;x++){ //System.out.println(list[x]); if (!list[x].equals("")){ //System.out.println("pl"); file.write(Integer.valueOf(list[x]).intValue()); } } file.close(); } private static String enqryt_one(String string){ String[] values = string.split(""); String newString = ""; String lastValue = ""; int lastNumber = 0; System.out.println(values.length); boolean written = false; for (int x = 0; x < values.length;x++){ String numb = values[x]; written = false; if (numb.equals(lastValue)){ ++lastNumber; written = true; if (x+1<values.length){ if (lastNumber>2 && !values[x+1].equals(numb)){ newString = newString +"$"+lastNumber+"%"+numb+"."; lastNumber = 0; written = true; } } else { if (lastNumber != 0){ newString = newString +"$"+lastNumber+"%"+numb+"."; lastNumber = 0; written = true; } } } if (written == false){ if (x+1<values.length){ if (!values[x+1].equals(numb)){ newString = newString+numb; lastNumber = 0; } } else { newString = newString+numb; lastNumber = 0; } } lastValue = numb; } //newString = newString + values[values.length-1]; return newString; } }
- 09-14-2009, 09:13 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
radix is the "type" 2 for binary, 8 for octal, 10 for decimal (the default), 16 for hexadecimal, etc, etc.
- 09-14-2009, 09:31 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Okej, now I'v also added 2 to the valueOf method..
I have also made so that the binary string doesnt end with ","
And made it easier incase u wish to test.
So now If u wana test it just change the EnqryptFile to you wished file..
But still, this doesnt work!
It messes up the letters...
Code here:
PHP Code:import java.util.Scanner; import java.io.*; public class myapp { public static void main(String[] args) throws IOException { String string = ""; int z = 0; String EnqryptFile = "C:\\Users\\Andreas\\Desktop\\PhoneReg.txt"; try{ // TODO, add your application code //out.println("Wellcome to talk!"); File filey = new File(EnqryptFile); FileInputStream file = new FileInputStream(filey); int max = file.available(); for (int x = 0; x<max;x++){ int value = file.read(); max = file.available(); String value2 = get_full(Integer.toBinaryString(value)); if (x<max){ string = string+value2+","; } else { string = string+value2; } z++; } System.out.println(string); System.out.println("Amount of bites: "+z); //String enq1 = enqryt_one("0000111"); //System.out.println(enq1); replace(string,filey); } catch (Exception e){ System.out.println(e); } } private static String get_full(String v){ int length = v.length(); for (int x = 1; x < 9;x++){ if (x > v.length()){ v = v+"0"; } } return v; } private static void replace(String string, File filey)throws IOException { FileOutputStream file = new FileOutputStream(filey); String[] list = string.split(","); for (int x = 0; x < list.length;x++){ //System.out.println(list[x]); if (!list[x].equals("")){ //System.out.println("pl"); file.write(Integer.valueOf(list[x],2).intValue()); } } file.close(); } }Last edited by Addez; 09-14-2009 at 09:34 PM. Reason: W00T?! I was gona delete the top codes! Not this one!
- 09-14-2009, 10:23 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because it should be left padded (i.e. "0" + v) and you are right padding (i.e. v + "0"). See the first reply again.
But, to tell you the truth, if you are separating the "bytes" with commas anyway, there is no reason to pad it anymore.
- 09-14-2009, 10:46 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Similar Threads
-
Hello! and I need help. I dont know were to start
By Fall0ut in forum New To JavaReplies: 10Last Post: 05-19-2010, 06:26 PM -
8 questions I dont understand while studying for SCJP
By shankhas in forum Java CertificationReplies: 5Last Post: 05-19-2010, 07:53 AM -
dont let me create simple class
By itaipee in forum New To JavaReplies: 5Last Post: 01-11-2009, 11:07 AM -
FileOutputStream gets NotSerializableException
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 09:38 PM -
FileOutputStream question...
By SCS17 in forum New To JavaReplies: 2Last Post: 07-07-2008, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks