[英]Trouble decoding the data from the random file
我必須編寫一個程序,從文件中讀取輸入,使用隨機字符和整數將其編碼為隨機文件,然后對隨機文件進行解碼,以從控制台中的輸入文件中打印出原始數據。
我在編碼部分做了這個:
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后跟一個從1到20的隨機生成的數字n。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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.