[英]Trouble decoding the data from the random file
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. 您将编写一个Java程序来编码和解码文本文件。 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. •消息中的每个字符c后跟一个从1到20的随机生成的数字n。n是c和消息中下一个字符之间的随机数据的字节数。 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. 因此,在将c紧跟着n写入文件之后,在写入下一个字符c之前应该有n个字节的位置(带有随机数据)。
• The last character is followed by the number -1 which indicates the end of the message. •最后一个字符后跟数字-1,表示消息的结尾。
• Each character stored in the binary file occupies 2 bytes while each integer occupies 4 bytes. •二进制文件中存储的每个字符占用2个字节,而每个整数占用4个字节。 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 . 您要寻找的构建器是StringBUilder 。
Additionally you should look at the skipBytes method of your reader. 另外,您应该查看阅读器的skipBytes方法。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.