Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

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

  1. #1
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Cool 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"
    '
    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'
    '
    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);
                }
          }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    How do you get the first section?
    I see flen/4 in the code, you had a plan going on there it looks like, but unfinished.

  3. #3
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

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

    Quote Originally Posted by jps View Post
    How do you get the first section?
    I see flen/4 in the code, you had a plan going on there it looks like, but unfinished.
    '

    It works....it prints the first quarter characters of original text file...'

  4. #4
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

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

    -------------------------------------------------------------------------------------------------------------------------
    Why 3rd and 4th part are giving little wrong output'
    -------------------------------------------------------------------------------------------------------------------------
    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
    ------------------------------------------------------------------------

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    First thing I would do is figure out where the space is coming from.
    To do that I would determine if there was a space on the beginning or end of my parts, and compare to see if there was any pattern to the results.
    Once you find where they are being added, you can look back and see how they got there in the first place.

  6. #6
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

    Cool 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....'
    '
    /*
     * 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);
            }
          }
        }
    '
    :............................................'

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    Glad you got it working.
    You still need to store the four parts in some way which can be shuffled. Plus to look at your code, there seems to be this pattern of similar things done many times in a row, surely some form of a loop could be used, and/or a method(s). Try to think in general terms and reusable code parts. For example if you had some group of things, or collection, and each one was different, but you wanted to do the same basic thing to each one, you could use a loop to go through each element of the collection, and call a method to modify each one from within the loop.

  8. #8
    Member suyog53's Avatar
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    37
    My Mood
    Daring
    Thanks
    2
    Thanked 1 Time in 1 Post

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

    @jps..............'
    Yes..............thanx ...'
    will definitely luk for it...........'

Similar Threads

  1. What is the best way to read from a text file?
    By Kerr in forum File I/O & Other I/O Streams
    Replies: 20
    Last Post: January 4th, 2012, 05:41 PM
  2. Read text file
    By cardamis in forum Java IDEs
    Replies: 2
    Last Post: November 4th, 2011, 11:59 PM
  3. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM

Tags for this Thread