我可以通过 Java 编程故意损坏文件吗?也可以加扰文件内容吗?
Can i porposely corrupt a file through Java programming ? Also is it possible to scramble a file contents ?
我想通过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
一个简单的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) 在任意位置打开 read/write 的文件
b) 获取一个随机位置,获取一个随机字节,将随机字节写入随机位置
C) 重复 B) scrambledByteCount
次
我想通过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
一个简单的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) 在任意位置打开 read/write 的文件
b) 获取一个随机位置,获取一个随机字节,将随机字节写入随机位置
C) 重复 B) scrambledByteCount
次