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

猎犬99

我必须制作一个程序,从文件中读取输入,用随机字符和整数将其编码为随机文件,然后解码随机文件以在控制台中打印输入文件中的原始数据。

我为编码部分做了这个:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从plist文件解码数据

来自分类Dev

无法解码数据 Objective-C

来自分类Dev

解码PHP 7.2中的编码数据

来自分类Dev

在 Scala 中编码和解码数据的最快方法

来自分类Dev

试图迅速从Youtube Api流行视频中解码数据,并“希望解码Array <Any>但找到字典”

来自分类Dev

在Windows临时文件中,使用file_put_contents使用base 64解码数据创建的0 KB文件

来自分类Dev

如何从列表中提取解码数据?

来自分类Dev

mcrypt解码数据类型

来自分类Dev

PHP json 使用 foreach 解码数据

来自分类Dev

在 result_array() 中解码数据库结果中存储的 JSON 值

来自分类Dev

我该如何在mysql字段中以这种格式解码数据?

来自分类Dev

我可以使用以Java编码的php中的base64解码数据吗?

来自分类Dev

无法从PHP中的文件解码JSON

来自分类Dev

从PHP中的.json文件解码json数据

来自分类Dev

Swift-解码数组JSON数据的数组

来自分类Dev

从RS232串口编码/解码数据

来自分类Dev

ajax成功函数未返回json解码数据

来自分类Dev

将来自 API 的解码数据用于算法

来自分类Dev

SWIFT 可解码数据传输

来自分类Dev

从串口解码数据(带整数的字符串)

来自分类Dev

Python解码utf-8编码数据

来自分类Dev

如何使用委托传递通用可解码数据

来自分类Dev

如何在php中显示json解码数组?

来自分类Dev

PHP解码数组

来自分类Dev

listFiles(),isDirectory()方法无法读取Java 1.4中的未编码数据

来自分类Dev

是否将INI文件中的base64编码数据加载回TPicture?

来自分类Dev

在 python 中读取我的 csv 文件生成编码数据

来自分类Dev

无法从 Nethereum 解码的输入数据中获取可读整数

来自分类Dev

为什么某些.wav文件无法在Firefox中解码

Related 相关文章

热门标签

归档