Results 1 to 9 of 9
  1. #1
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Question How to read first 9 bytes and write in...?

    Hi,

    How to read 9 bytes from a txt file and write in reverse in other txt file?

    repeat until have bytes in first txt file.
    reverse first 9'th, reverse second 9'th ...


    pls help me...

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

    Default

    See the API docs for FileInputStream and its read(array) method, then create another array of the same length and iterate through the first array (either forwards or backwards) placing the elements into the second array (in the opposite direction), then see the API doc for FileOutputStream and its write(array) method.

  3. #3
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default pls help i need it today...

    Hi,

    I have done that for String but can anyone do the same thing for FILE

    this is not fore reverse but for cryptography

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

    Default

    And, so do it again, as you will simply be working on one string (already in array form) after another with a read and write around each array. No difference.

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

    Default

    P.S. did you even attempt to read the API docs pointed to, or did you simply see that there was no code included and so ignored everything else?

  6. #6
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default do it for file, pls...

    that is the code for String (sorry)
    Java Code:
    public class Det6enc
    {	
    	
    	public static String mix(String str)
    	{
    		String enc = "";
    		char[] c = new char[9];
    		char[] b = new char[9];
    		
    		int numro = ((str.length()-(str.length())%9));
    	
    		for(int i=0; i<numro; i=i+9)
    		{
    			for(int j=0; j<9; j++)
    			{
    				b[j] = str.charAt(j+i);
    			}
    			c[0] = b[1];	c[1] = b[5];	c[2] = b[2];	
    			c[3] = b[3];	c[4] = b[6];	c[5] = b[8];	
    			c[6] = b[7];	c[7] = b[0];	c[8] = b[4];
    			
    			
    			for(int k=0; k<9; k++)
    			{
    				enc = enc + c[k];
    			}	
    		}
    		for(int i=0; i<str.length()-numro; i++)
    		{
    			enc = enc + str.charAt((str.length()-(str.length())%9)+i);
    		}
    		
    		return enc;	
    	}
    	
    	public static void main (String[] args) 
    	{
    		String str = "ARTAN POTERA";
    		System.out.println (mix(str));
        }	
    }

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

    Default

    Okay? And now?

    Try to do something with the information I've already given you.

  8. #8
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default

    i have done but if ther is any way to do it more simply pls coment
    that is my code:
    Java Code:
    import java.io.*;
    public class CopyBytes {
        public static void main(String[] args) throws IOException {
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
            	String enc = "";
    			char[] c = new char[9];
    			char[] b = new char[9];
            	File f1 = new File("plaintext.txt");
            	File f2 = new File("ciphertext.txt");
                in = new FileInputStream(f1);
                out = new FileOutputStream(f2);
                int numro = ((int)(f1.length()-(f1.length())%9));
                System.out.println (numro);
                     
                int shto;
    			for(int i=0; i<numro; i=i+9)
    			{	
    				b[0] = (char)in.read();	b[1] = (char)in.read();	b[2] = (char)in.read();	
    				b[3] = (char)in.read();	b[4] = (char)in.read();	b[5] = (char)in.read();	
    				b[6] = (char)in.read();	b[7] = (char)in.read();	b[8] = (char)in.read();
    				
    				c[0] = b[1];	c[1] = b[5];	c[2] = b[2];	
    				c[3] = b[3];	c[4] = b[6];	c[5] = b[8];	
    				c[6] = b[7];	c[7] = b[0];	c[8] = b[4];
    				
    				for(int k=0; k<9; k++)
    				{
    					out.write( c[k]);
    				}
    					
    			}
    			while ((shto = in.read()) != -1) 
    			{
                    out.write(shto);
                }
    
            } 
            	finally 
            	{
                if (in != null) 
                {
                    in.close();
                }
                if (out != null) 
                {
                    out.close();
                }
            }
        }
    }

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

    Default

    What did I say about the read(array) method?
    Java Code:
    byte[] c = new byte[9];  // you did say "bytes", right?
    byte[] b = new byte[9];
    in = new FileInputStream("plaintext.txt");
    out = new FileOutputStream("ciphertext.txt");
    while(true) {
        int byteRead = 0;
        int totalRead = 0;
        while ((totalRead < b.length) && ((byteRead = in.read(b, totalRead, b.length - totalRead)) >= 0)) {
            totalRead += byteRead;
        }
        if (byteRead < 0) {
            break;
        }
        for (int i=0; i<b.length; i++) {
            c[c.length - (i+1)] = b[i];
        }
        out.write(c);
    }
    Now, study that code, read the API docs, and answer the following questions:

    What does this
    Java Code:
    ((byteRead = in.read(b, totalRead, b.length - totalRead)) >= 0)
    do? And why is it done that way?

    What is this
    Java Code:
    if (byteRead < 0) {
            break;
        }
    for?

    What does this
    Java Code:
    c[c.length - (i+1)] = b[i];
    do? And why is it written that way? Specifically, why do you need to use (i + 1)?
    Last edited by masijade; 03-26-2009 at 03:56 PM.

Similar Threads

  1. How Read and Write XMl files using Java
    By tjs in forum SWT / JFace
    Replies: 0
    Last Post: 02-23-2009, 01:19 PM
  2. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:11 PM
  3. Replies: 5
    Last Post: 10-17-2008, 03:13 PM
  4. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 04:46 PM
  5. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 01:15 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
  •