我必须制作一个程序,从文件中读取输入,用随机字符和整数将其编码为随机文件,然后解码随机文件以在控制台中打印输入文件中的原始数据。
我为编码部分做了这个:
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();
}
}
这是针对解码器的。
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();
}
}
我的主要问题是我无法找到一种方法来存储随机文件中的字符而跳过所有这些随机的东西。
这是问题:
您将编写一个 Java 程序来对文本文件进行编码和解码。编码器读取存储在纯文本文件中的消息,对其进行编码,然后将其存储在二进制文件中。解码器读取二进制文件,解码消息并将其打印到控制台。
编码算法的工作原理如下:
• 消息中的每个字符 c 后跟一个随机生成的数字 n,范围从 1 到 20。n 是 c 和消息中下一个字符之间的随机数据的字节数。因此,在将 c 后跟 n 写入文件后,在写入下一个字符 c 之前应该有 n 个字节位置(带有随机数据)。
• 最后一个字符后跟数字-1,表示消息的结束。
• 二进制文件中存储的每个字符占用2 个字节,而每个整数占用4 个字节。随机数据存储在每个字符后面的整数和下一个字符之间。
我的主要问题是我无法找到一种方法来存储随机文件中的字符而跳过所有这些随机的东西。
您正在寻找的构建器是StringBUilder。
此外,您应该查看阅读器的skipBytes方法。
把它们放在一起,你的解码器看起来像这样(不考虑边缘情况):
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();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句