Results 1 to 7 of 7
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Question 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();
        }
        
        
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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;
        }
        
    }

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    radix is the "type" 2 for binary, 8 for octal, 10 for decimal (the default), 16 for hexadecimal, etc, etc.

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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!

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    THANKS ALOT!
    Now its all working!
    I must say without you I would have given up on this XD

    Thanks alot for all help! And all you other guys who have helped, thanks alot u2 =)

Similar Threads

  1. Hello! and I need help. I dont know were to start
    By Fall0ut in forum New To Java
    Replies: 10
    Last Post: 05-19-2010, 06:26 PM
  2. 8 questions I dont understand while studying for SCJP
    By shankhas in forum Java Certification
    Replies: 5
    Last Post: 05-19-2010, 07:53 AM
  3. dont let me create simple class
    By itaipee in forum New To Java
    Replies: 5
    Last Post: 01-11-2009, 11:07 AM
  4. FileOutputStream gets NotSerializableException
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 09:38 PM
  5. FileOutputStream question...
    By SCS17 in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 05:30 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
  •