I have to make a program that reads input from a file, encodes it with random characters and integers into a random file and then decode the randomfile to print the original data from the input file in the console.
I did this for the encoding part:
public class Encoder implements IEncoder {
public void encode(String inputFileName, String outputFilePath) throws IOException{
File file = new File(inputFileName);
//load characters into the character array
char[] chars = {'a', 'b', 'c', 'd','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
RandomAccessFile randFile = new RandomAccessFile(outputFilePath, "rw");
ArrayList<Character> list = new ArrayList<Character>();
String k = "";
//scan input file, save into string
try {
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
k=k+scan.nextLine();
}
scan.close();
} catch (FileNotFoundException e) {
System.out.println("There was an issue with the file...");
}
//save data from the input file into the ArrayList
for(int i = 0; i < k.length(); i++) {
list.add(k.charAt(i));
}
//write each character into a binary file, along with a random integer n, followed by n random characters
for(int j = 0; j< list.size()-1; j++) {
int n = ThreadLocalRandom.current().nextInt(1, 20 + 1);
randFile.writeChar(list.get(j));
randFile.writeInt(n);
for (int m = 0; m < n; m++) {
int z = ThreadLocalRandom.current().nextInt(1, 11 + 1);
randFile.writeChar(chars[z]);
}
}
randFile.writeChar(list.get(list.size() - 1));
randFile.writeInt(-1);
randFile.close();
}
}
This is for the decoder.
public class Decoder implements IDecoder {
@Override
public void decode(String filePath) throws IOException {
//read the random access file
RandomAccessFile randFile = new RandomAccessFile(filePath, "r");
//create a string to print the output to console
String k ="";
//initialize the array list
ArrayList<Character> list = new ArrayList<Character>();
for(int i = list.size()-1 ; i> 0 ; i--) {
char c = randFile.readChar();
int n = randFile.readInt();
// int z = ThreadLocalRandom.current().nextInt(1, 20 + 1);
for(int m = 0; m < n; m++) {
// int x = ThreadLocalRandom.current().nextInt(1, 11 + 1);
k = k + c;
}
}
//print the output and close the random access file
System.out.println("The data is" + k);
randFile.close();
}
}
My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.
this is the question:
You are to write a Java program to encode and decode a text file. The encoder reads a message stored in a plain text file, encodes it, and stores it in a binary file. The decoder reads the binary file, decodes the message and prints it to the console.
The encoding algorithm works as follows:
• Each character c in the message is followed by a randomly-generated number n ranging from 1 to 20. n is the number of bytes of random data between c and the next character in the message. So, after writing c followed by n to the file, there should be n byte locations (with random data) before the next character c is written.
• The last character is followed by the number -1 which indicates the end of the message.
• Each character stored in the binary file occupies 2 bytes while each integer occupies 4 bytes. Random data is stored between the integer following each character and the next character.
My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.
The builder you are looking for is a StringBUilder .
Additionally you should look at the skipBytes method of your reader.
Putting those together, your decoder would look something like this (not accounting for edge cases):
public class Decoder implements IDecoder {
@Override
public void decode(String filePath) throws IOException {
//read the random access file
RandomAccessFile randFile = new RandomAccessFile(filePath, "r");
//create a string to print the output to console
StringBuilder builder = new StringBuilder();
while(true) {
char c = randFile.readChar();
builder.append(c);
int n = randFile.readInt();
if(n == -1) break;
randFile.skipBytes(n);
}
//print the output and close the random access file
System.out.println("The data is" + builder.toString());
randFile.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.