Results 1 to 18 of 18
  1. #1
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Wink How to read and shuffle bytes from text file in Java....?'

    There is a text file containing some text for which the following conditions are true:
    > text file contains no new line, all text is in single line'
    > length of text file is multiple of 4...(length%4==0)'
    '
    Now, how can i split the contents of text file into 4 equal parts and shuffle those parts'
    For e.g. text file contains text as
    "abcdefghijklmnopqrstuvwx"
    '
    Total length=24.........so 4 parts means 6,6,6,6
    part1-abcdef
    part2-ghijkl
    part3-mnopqr
    part4-stuvwx
    '
    part1,part2,part3,part4 are shuffled as part3,part1,part4,part2'
    '
    so i want output as
    "mnopqrabcdefstuvwxghijkl"
    '
    Can someone help me with the logic or which technique should I use?'
    Thanx in advance.....'

  2. #2
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: How to read and shuffle bytes from text file in Java....?'

    Basically you have to read in the file. The various mechanisms Java has usually provide methods where you can specify how many bytes you want to read at once.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: How to read and shuffle bytes from text file in Java....?'

    Is this an ascii file?
    That is, is each character in the file exactly 1 byte?
    In which case there's a method on File which will tell you the size of the file in bytes and then, as JavaAdviser says, there are methods on things like FileReader for reading. Though I don't think you can specify the amount to be read, so you'll still need to track it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: How to read and shuffle bytes from text file in Java....?'

    Yes you can. The FileReader, among other, inherits an overloaded version of read() where you can specify a buffer, its size and an offset. :-)

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

    Default Re: How to read and shuffle bytes from text file in Java....?'

    @JavaAdviser....................................'
    how can we implement the read() method of FileReader...
    Can you just explain me the format.............................'
    '
    I didnt get you properly'

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

    Default Re: How to read and shuffle bytes from text file in Java....?'

    @Tolls...'
    Yes.....each character is exactly 1 byte.....'

  7. #7
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: How to read and shuffle bytes from text file in Java....?'

    You don't have to implement it yourself. The Java API already provides the means to do that.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: How to read and shuffle bytes from text file in Java....?'

    Quote Originally Posted by JavaAdviser View Post
    Yes you can. The FileReader, among other, inherits an overloaded version of read() where you can specify a buffer, its size and an offset. :-)
    Yes it does, but as there's a reason it returns the number of bytes read.
    Which is because there is no guarantee it will fill the buffer.
    So, no you can't.
    API.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: How to read and shuffle bytes from text file in Java....?'

    I'm relatively new with Java, so there may be a better way, but...
    I was able to do this by using FileReader and StringBuffer. Reading it into a char[6] array named cbuf, appending cbuf to the StringBuffer, then adding the StringBuffer to an ArrayList<String>. Then you use Collections.shuffle(array); You then end up with an array of [stuvwx,abcdef,ghijkl,mnopqr], or however it shuffled.

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

    Cool Re: How to read and shuffle bytes from text file in Java....?'

    I tried on this technique where the bytes from text file are to be splitted'
    I am able to calculate the part1 of the original bytes...'
    However, stuck on how to calculate the other 3 parts'
    Java Code:
    import java.io.*;
    public class split_contents
    { 
    	public static void main(String[] args) throws FileNotFoundException, IOException
         	{
            	File nfile=new File("D:/new_file.txt");
            	PrintStream out=new PrintStream(new FileOutputStream(nfile));
            	System.setOut(out);
    
            	File file = new File("D:/old_file.txt");
            	{
    			FileInputStream fin = new FileInputStream(file);
    
                		int flen=(int)(file.length());
                		int firstpart=(flen/4);  //Firstpart
                		byte fileContent[] = new byte[firstpart];
               		fin.read(fileContent);
    			String strFileContent = new String(fileContent);
               		System.out.print(strFileContent);
            	}
          	}
    }
    '
    Last edited by suyog53; 09-20-2012 at 08:30 AM.

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: How to read and shuffle bytes from text file in Java....?'

    You need to loop, reading a chunk each time.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    kuldeepsingh is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Re: How to read and shuffle bytes from text file in Java....?'

    For get text form a file there are many methods are use in java in .IO package first one import the Io package and use these method like ad object.getString() method. By using the loop in java we can access complete text of file.



    GetMore

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: How to read and shuffle bytes from text file in Java....?'

    What on earth are you on about?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: How to read and shuffle bytes from text file in Java....?'

    You need to loop, reading a chunk each time.
    For get text form a file there are many methods are use in java in .IO package first one import the Io package and use these method like ad object.getString() method. By using the loop in java we can access complete text of file.
    '
    But if i use loop, only first part will get printed each time...'

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

    Default Re: How to read and shuffle bytes from text file in Java....?'

    -------------------------------------------------------------------------------------------------------------------------
    Why 3rd and 4th part are giving little wrong output'
    -------------------------------------------------------------------------------------------------------------------------
    Java Code:
    public class split_contents
        { 
          public static void main(String[] args) throws FileNotFoundException, IOException
          {
            File nfile=new File("D:/new_file.txt");
            PrintStream out=new PrintStream(new FileOutputStream(nfile));
            System.setOut(out);
           
            File file = new File("D:/old_file.txt");
            {
                FileInputStream fin = new FileInputStream(file);
                int flen=(int)(file.length());
                int last_x=(flen/4);
                //-------------------------------------------1----------------------------------------
                int part1=last_x;
                byte fileContent1[] = new byte[part1];
                fin.read(fileContent1);  //create string from byte array
                String strFileContent1 = new String(fileContent1);
               //------------------------------------------2---------------------------------------
                int part2=last_x+1;
                byte fileContent2[]=new byte[part2];
                fin.read(fileContent2);  //create string from byte array
                String strFileContent2 = new String(fileContent2);
                //------------------------------------------3---------------------------------------
                int part3=(2*last_x)+1;
                byte fileContent3[]=new byte[part3];
                fin.read(fileContent3);  //create string from byte array
                String strFileContent3 = new String(fileContent3);
               //------------------------------------------4---------------------------------------
                int part4=(3*last_x)+1;
                byte fileContent4[]=new byte[part4];
                fin.read(fileContent4);  //create string from byte array
                String strFileContent4 = new String(fileContent4);
                //-----------------------------print---------------------------------------------
                System.out.print(strFileContent3);
                System.out.print(strFileContent1);
                System.out.print(strFileContent4);
                System.out.print(strFileContent2);
            }
          }
        }
    ------------------------------------------------------------------------
    The 3rd and 4th part are giving extra blank spaces along with the text'
    Output:
    nopq3rstuvwx4 abcdef1 ghijklm2
    Needed Output:
    nopq3rstuvwx4abcdef1ghijklm2
    ------------------------------------------------------------------------

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

    Default Re: How to read and shuffle bytes from text file in Java....?'

    What on earth are you on about?
    Creating a project for which.........part of that is the post that I created...'
    '
    The Java provides the way to shuffle the characters in many ways...'
    But all of them are shuffled in a way that they cannot be brought back to original text again...'
    '
    That limits my project...'
    So....i am trying to shuffle the text using some specific logic so that I can get back my original text again...'
    '
    I tried the above code........but 85% of the output is correct....'
    So...........this post..............................'

  17. #17
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,928
    Rep Power
    19

    Default Re: How to read and shuffle bytes from text file in Java....?'

    Sorry, that was directed at kuldeepsingh.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: How to read and shuffle bytes from text file in Java....?'


    ---------------
    [S O L V E D]
    ---------------

    '
    I tried a lot and succeeded in creating the scrambling text with logic....'
    '
    Here it is....'
    '
    Java Code:
    /*
     * IMAGE SCRAMBLING PROJECT
     * Conversion - Part V of Encryption
     * Author: Su
     * Description: Divide file into 4 parts and scramble them
     */
    
    package imagescrambler.imagescrambler_encrypt;
    
    import java.io.*;
         
        public class e5_scramble
        { 
          public static void main(String[] args) throws FileNotFoundException, IOException
          {
            File nfile=new File("D:/newfile.txt");
            PrintStream out=new PrintStream(new FileOutputStream(nfile));
            System.setOut(out);
           
            File file = new File("D:/oldfile.txt");
            {
                FileInputStream fin = new FileInputStream(file);
                int flen=(int)(file.length());
                int last_x=(flen/4);
                int part1=last_x;
                int part2=last_x;
                int part3=last_x;
                int part4=last_x;
                String strFileContent1,strFileContent2,strFileContent3,strFileContent4;
                //-------------------------------------------1----------------------------------------
                {
                    byte fileContent1[] = new byte[part1];
                    fin.read(fileContent1);  //create string from byte array
                    strFileContent1 = new String(fileContent1);
                }
               //------------------------------------------2---------------------------------------
                {
                    byte fileContent2[]=new byte[part2];
                    fin.read(fileContent2);  //create string from byte array
                    strFileContent2 = new String(fileContent2);
                }
                //------------------------------------------3---------------------------------------
                {
                    byte fileContent3[]=new byte[part3];
                    fin.read(fileContent3);  //create string from byte array
                    strFileContent3 = new String(fileContent3);
                }
               //------------------------------------------4---------------------------------------
                {
                    byte fileContent4[]=new byte[part4];
                    fin.read(fileContent4);  //create string from byte array
                    strFileContent4 = new String(fileContent4);
                }
                    //-----------------------------print---------------------------------------------
                System.out.print(strFileContent3);
                System.out.print(strFileContent1);
                System.out.print(strFileContent4);
                System.out.print(strFileContent2);
            }
          }
        }
    '
    ............................................'

Similar Threads

  1. How to write bytes to text file?
    By fatabass in forum New To Java
    Replies: 10
    Last Post: 09-18-2012, 07:46 PM
  2. Why can't I read all the bytes from a text file ?
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 01-16-2012, 09:17 PM
  3. Replies: 5
    Last Post: 10-17-2008, 02:13 PM
  4. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Tags for this Thread

Posting Permissions

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