【问题标题】:Can i porposely corrupt a file through Java programming ? Also is it possible to scramble a file contents ?我可以通过 Java 编程来破坏文件吗?也可以打乱文件内容吗?
【发布时间】:2018-10-02 12:56:20
【问题描述】:

我想通过 java 编程故意破坏一个文件。我听说您可以读取文件字节并将任何随机字节附加到它,但我还不知道如何实际实现它。 能否提供一个sn-p代码..

是否可以打乱文件的内容,使文件仍然可以打开,但里面的内容不再可读

【问题讨论】:

  • 如果附加字节,则内容仍然相同。那么对于附加的字节。你有什么想法?
  • 看看如何读写文件试试看?
  • @greenapps 我是 Java 新手,我想玩弄文件。
  • @JamesZ 感谢您的建议,我会试一试..

标签: java android file corrupt


【解决方案1】:

一个简单的RandomAccessFile 加扰器是这样的:

private static final String READ_WRITE = "rw";
private static final Random random = new Random();

public static void scramble(String filePath, int scrambledByteCount) throws IOException{
    RandomAccessFile file = new RandomAccessFile(filePath, READ_WRITE);

    long fileLength = file.getLength();
    for(int count = 0; count < scrambledByteCount; count++) {
        long nextPosition = random.nextLong(fileLength-1);
        file.seek(nextPosition);

        int scrambleByte = random.nextInt(255) - 128;
        file.write(scrambleByte);
    }

    file.close();
}

RandomAccessFile 正是它听起来的样子,一个可以在任何位置读取和写入的文件。您可以拨打randomAccessFile.seek(long position)选择职位。 Random 也就是它听起来的样子,一个随机数、字节等的工厂。 所以本质上 A)在任何位置打开文件进行读/写 b) 获取随机位置,获取随机字节,将随机字节写入随机位置 C) 重复 B) scrambledByteCount

【讨论】:

  • 非常感谢老兄..!!你是一个真正的救生员..!!
  • 没问题,伙计!
  • 如果我的答案让您满意,请您检查一下我的答案作为解决方案吗?
猜你喜欢
  • 2012-03-09
  • 1970-01-01
  • 2018-08-20
  • 2016-12-25
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
相关资源
最近更新 更多