繁体   English   中英

无法解码随机文件中的数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM