有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓我可以通过Java编程破坏文件吗?还有可能扰乱文件内容吗?

我想通过java编程故意破坏一个文件。我听说您可以读取一个文件字节并向其添加任意随机字节,但我还不知道如何实际实现它你能提供一段代码吗

is it possible to scramble the contents of a file so that the file can be still opened but the content inside is no more readable


共 (1) 个答案

  1. # 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