如何读取和编辑序列化文件中的 txt? [英] How to I read and edit txt in a serialised file?

查看:90
本文介绍了如何读取和编辑序列化文件中的 txt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将文件反序列化回对象实例之前,我想恶意编辑文件中的txt.

Before de serialising the file back into an object instance, I want to maliciously edit the txt in the file.

    //FILE TAMPER
    //Lexical block: Tamper
    {
        String output = null;
        //Lexical block make output
        {
            LinkedList<String> lls = new LinkedList<String>();
            //Lexical block: Reader
            {
                BufferedReader br = new BufferedReader(new FileReader(fileString));
                while (br.ready()) {
                    String readLine = br.readLine();
                    lls.add(readLine);
                }
                br.close();
            }
            //Lexical block: manipulate
            {
                //Henry Crapper
                final String[] llsToArray = lls.toArray(new String[lls.size()]);
                for (int i = 0; i < llsToArray.length; i++) {
                    String line = llsToArray[i];
                    if (line.contains("Henry")) {
                        line = line.replace("Henry",
                                            "Fsekc");
                        llsToArray[i] = line;
                    }
                    if (line.contains("Crapper")) {
                        line = line.replace("Crapper",
                                            "Dhdhfie");
                        llsToArray[i] = line;
                    }

                    lls = new LinkedList<String>(Arrays.asList(llsToArray));
                }
            }

            //Lexical block: write output
            {
                StringBuilder sb = new StringBuilder();
                for (String string : lls) {
                    sb.append(string).append('\n');
                }
                output = sb.toString();
            }
        }
        //Lexical block: Writer
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileString));
            bw.write(output);
            bw.close();
        }
    }

但是编辑的文件不正确,并且有一些异常字符.

However the edited file isn't correct and has some unusual characters.

//Before
¨Ìsr&Snippets.Parsed.EmployeeSerialization0I
bankBalanceLnametLjava/lang/String;xp•Åt
Henry Crappe

//After
ÔøΩÔøΩsr&Snippets.Parsed.EmployeeSerialization0I
bankBalanceLnametLjava/lang/String;xpÔøΩÔøΩt
Fsekc Dhdhfie

我猜是某种不可读的字符问题还是什么?

I'm guessing there is some sort of non readable character issue or something?

推荐答案

包含序列化对象实例的文件是二进制文件:您不应使用 BufferedWriter 对其进行编辑.例如,使用 RandomAccessFile 对其进行编辑.

A file which contains a serialized object instance is a binary file: you should not edit it with a BufferedWriter. Edit it with a RandomAccessFile, for example.

如果您想知道为什么,Writer 中使用的字符集无法与字节进行一对一映射.保存所有文件也会改变意想不到的位置.

If you are wondering of why, the charset used in a Writer could not map one-to-one with a byte. Saving all the file would change also unexpected positions.

这篇关于如何读取和编辑序列化文件中的 txt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆