java文件如何写才不会丢帧_如何在java中故意破坏文件

注意:请不要评论这个问题。对于那些认为我这样做是为了“欺骗”的人;你错了,因为我已经不在学校了。另外,如果我是,我自己实际上试图欺骗,我只会使用已经为此创建的服务,而不是重新创建该程序。我接受了这个项目,因为我认为这可能很有趣,没有别的。在您投票之前,请先consider the value of the question it's self,而不是它的推测性用法,因为SO的目的不是判断,而仅仅是提供公共信息。

我正在开发一个假定故意损坏文件的程序(特别是.doc,txt或pdf,但其他程序也会很好)

我最初尝试这样做:

public void corruptFile (String pathInName, String pathOutName) {

curroptMethod method = new curroptMethod();

ArrayList corruptHash = corrupt(getBytes(pathInName));

writeBytes(corruptHash, pathOutName);

new MimetypesFileTypeMap().getContentType(new File(pathInName));

// "/home/ephraim/Desktop/testfile"

}

public ArrayList getBytes(String filePath) {

ArrayList fileBytes = new ArrayList();

try {

FileInputStream myInputStream = new FileInputStream(new File(filePath));

do {

int currentByte = myInputStream.read();

if(currentByte == -1) {

System.out.println("broke loop");

break;

}

fileBytes.add(currentByte);

} while (true);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(fileBytes);

return fileBytes;

}

public void writeBytes(ArrayList hash, String pathName) {

try {

OutputStream myOutputStream = new FileOutputStream(new File(pathName));

for (int currentHash : hash) {

myOutputStream.write(currentHash);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//System.out.println(hash);

}

public ArrayList corrupt(ArrayList hash) {

ArrayList corruptHash = new ArrayList();

ArrayList keywordCodeArray = new ArrayList();

Integer keywordIndex = 0;

String keyword = "corruptthisfile";

for (int i = 0; i < keyword.length(); i++) {

keywordCodeArray.add(keyword.codePointAt(i));

}

for (Integer currentByte : hash) {

//Integer currentByteProduct = (keywordCodeArray.get(keywordIndex) + currentByte) / 2;

Integer currentByteProduct = currentByte - keywordCodeArray.get(keywordIndex);

if (currentByteProduct < 0) currentByteProduct += 255;

corruptHash.add(currentByteProduct);

if (keywordIndex == (keyword.length() - 1)) {

keywordIndex = 0;

} else keywordIndex++;

}

//System.out.println(corruptHash);

return corruptHash;

}但问题是该文件仍然可以打开。当你打开文件时,所有的单词都被改变了(它们可能没有任何意义,甚至可能不是字母,但它仍然可以打开)

所以这里是我的实际问题:

有没有办法让一个文件如此腐败,以至于电脑根本不知道如何打开它(例如,当你打开它时,计算机会沿着“这个文件不被识别,并且不能被打开“)?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值