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'
'
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);
}
}
}
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.
Re: How to read and shuffle bytes from text file in Java....?'
Quote:
Originally Posted by
jps
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...'
Re: How to read and shuffle bytes from text file in Java....?'
-------------------------------------------------------------------------------------------------------------------------
Why 3rd and 4th part are giving little wrong output'
-------------------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------
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.
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....'
'
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);
}
}
}
'
:P:............................................'
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.
Re: How to read and shuffle bytes from text file in Java....?'
@jps..............'
Yes..............thanx ...'
will definitely luk for it...........'