Java 文件列表检查器是否阻止文件的删除和命名?
在下面的代码中,创建了一些输出,一些数字。其中一个数字是哈希值。哈希值是从文件夹中计算出来的 在计算文件夹时,它似乎被限制为删除、添加和命名文件。这是正常行为,还是可以在TaskStartPart或TaskPart类的代码中进行更改Java 文件列表检查器是否阻止文件的删除和命名?,java,file,Java,File,在下面的代码中,创建了一些输出,一些数字。其中一个数字是哈希值。哈希值是从文件夹中计算出来的 在计算文件夹时,它似乎被限制为删除、添加和命名文件。这是正常行为,还是可以在TaskStartPart或TaskPart类的代码中进行更改 import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class RestartTest {
StringBuilder sb;
String dtf = "============================";
String hexRes2 = "";
int i1 = 0;
int i2 = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException, IOException, NoSuchAlgorithmException {
// TODO code application logic here
new RestartTest().startApp();
}
public void startApp() throws InterruptedException, IOException, NoSuchAlgorithmException {
TaskStart startTask = new TaskStart();
startTask.startCalc();
}
class TaskStart {
public void startCalc() throws InterruptedException, IOException, NoSuchAlgorithmException {
while(!Thread.currentThread().isInterrupted()) {
i1 = (int) (Math.random() * 1000);
System.out.println("Value 1: " + i1);
new TaskStart2().startCalc2();
new TaskStartPart().calculHash();
dateiSpeichern(i1,i2,"");
}
}
}
class TaskStart2 {
public void startCalc2() throws InterruptedException, IOException {
i2 = (int) (Math.random() * 1000);
System.out.println("Value 2: " + i2);
dateiSpeichern(i1,i2,"");
}
}
class TaskStartPart {
public void calculHash() throws InterruptedException, IOException, NoSuchAlgorithmException {
try {
DigestInputStream digestInputStream=null ;
MessageDigest messageDigest=MessageDigest.getInstance("SHA-512") ;
digestInputStream=new DigestInputStream(new TaskPart(new File("C:\\Users\\win7p\\Documents/t")),messageDigest) ;
//System.out.println("Path :" + direc.toString()) ;
while(digestInputStream.read()>=0) ;
//System.out.print("\nsha-512 sum=") ;
for(byte b: messageDigest.digest()) {
hexRes2 += String.format("%02x",b);
} sb = new StringBuilder(hexRes2);
dateiSpeichern(0,0,sb.substring(hexRes2.length() - 128,hexRes2.length())); System.out.println(sb.substring(hexRes2.length() - 128,hexRes2.length()));
digestInputStream.close();
} catch (IOException ex ) {ex.printStackTrace();}
}
}
class TaskPart extends InputStream {
private File mFile ;
private List<File> mFiles ;
private InputStream mInputStream ;
public TaskPart(File file) throws FileNotFoundException {
mFile=file ;
if(file.isDirectory()) {
mFiles=new ArrayList<File>(Arrays.asList(file.listFiles())) ;
Collections.sort(mFiles) ;
mInputStream=nextInputStream() ;
} else {
mFiles=new ArrayList<File>() ;
mInputStream=new FileInputStream(file) ;
}
}
@Override
public int read() throws IOException {
int result=mInputStream==null?-1:mInputStream.read() ;
if(result<0 && (mInputStream=nextInputStream())!=null)
return read() ;
else return result ;
}
protected String getRelativePath(File file) {
return file.getAbsolutePath().substring(mFile.getAbsolutePath().length()) ;
}
protected InputStream nextInputStream() throws FileNotFoundException {
if(!mFiles.isEmpty()) {
File nextFile=mFiles.remove(0) ;
return new SequenceInputStream(
new ByteArrayInputStream(getRelativePath(nextFile).getBytes()),
new TaskPart(nextFile)) ;
}
else return null ;
}
}
private void dateiSpeichern(int i1, int i2, String hexR) throws InterruptedException, IOException {
try {
String tF = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(new Date().getTime());
try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\win7p\\Documents/hashLog.txt", true))) {
writer.append(tF);
writer.newLine();
writer.append(dtf);
writer.newLine();
writer.append("Hash Value: ");
//If(hexR.length() == alHash.get(0))
//alHash.add(hexR);
writer.append(hexR);
writer.newLine();
writer.append("-----");
writer.append("Value 1:");
String si1 = Integer.toString(i1);
writer.append(si1);
writer.newLine();
writer.append("*****");
writer.append("Value 2:");
String si2 = Integer.toString(i2);
writer.append(si2);
writer.newLine();
writer.flush();
writer.close();
}
} catch(IOException ex) {System.out.print("konnte Datei nicht speichern");}
catch(NullPointerException nex) {System.out.println("no Log-File, try again...");}
} }
导入java.io.BufferedWriter;
导入java.io.ByteArrayInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.SequenceInputStream;
导入java.security.DigestInputStream;
导入java.security.MessageDigest;
导入java.security.NoSuchAlgorithmException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.Date;
导入java.util.List;
公共类重启测试{
为某人做准备;
字符串dtf=“========================================”;
字符串hexRes2=“”;
int i1=0;
int i2=0;
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)抛出InterruptedException、IOException、NoSuchAlgorithmException{
//此处的TODO代码应用程序逻辑
新重新启动测试().startApp();
}
public void startApp()引发InterruptedException、IOException、NoSuchAlgorithmException{
TaskStart startTask=新的TaskStart();
startTask.startCalc();
}
类任务开始{
public void startCalc()引发InterruptedException、IOException、NoSuchAlgorithmException{
而(!Thread.currentThread().isInterrupted()){
i1=(int)(Math.random()*1000);
System.out.println(“值1:+i1”);
新任务start2().startCalc2();
新建TaskStartPart().calculHash();
dateiSpeichern(i1,i2,“”);
}
}
}
类TaskStart2{
public void startCalc2()引发InterruptedException,IOException{
i2=(int)(Math.random()*1000);
System.out.println(“值2:+i2”);
dateiSpeichern(i1,i2,“”);
}
}
类TaskStartPart{
public void calculthash()抛出InterruptedException、IOException、NoSuchAlgorithmException{
试试{
DigestInputStream DigestInputStream=null;
MessageDigest=MessageDigest.getInstance(“SHA-512”);
digestInputStream=new digestInputStream(新任务部件(新文件(“C:\\Users\\win7p\\Documents/t”)),messageDigest;
//System.out.println(“路径:+direc.toString());
while(digestInputStream.read()>=0);
//系统输出打印(“\nsha-512 sum=”);
for(字节b:messageDigest.digest()){
hexRes2+=String.format(“%02x”,b);
}sb=新的StringBuilder(hexRes2);
dateiSpeichern(0,0,sb子字符串(hexRes2.length()-128,hexRes2.length());System.out.println(sb子字符串(hexRes2.length()-128,hexRes2.length());
digestInputStream.close();
}catch(IOException ex){ex.printStackTrace();}
}
}
类TaskPart扩展InputStream{
私有文件文件;
私人名单文件;
私有输入流mInputStream;
公共任务部件(文件)引发FileNotFoundException{
mFile=文件;
if(file.isDirectory()){
mFiles=newarraylist(Arrays.asList(file.listFiles());
集合。排序(mFiles);
mInputStream=nextInputStream();
}否则{
mFiles=newarraylist();
mInputStream=新文件输入流(文件);
}
}
@凌驾
public int read()引发IOException{
int result=mInputStream==null?-1:mInputStream.read();
如果(结果我想我已经找到问题了
在类的方法protected InputStream nextInputStream()中,TaskPart Extended InputStream是一个列表私有列表文件;
问题是,列表仍处于填充状态,因此在调用该方法后,需要在calculhash()中使用mFiles.clear()清除该列表
使文件不再列在该流中,并被阻止
谢谢你如果calculHash()在方法方面,我希望当流是打开的时,JVM会在操作系统级别设置限制,以防止使用打开的流开发文件。这意味着这是JVM的正常行为。也许有可能编写一些代码来避免这种情况。想想你问的问题:删除流的底层文件jvm正在使用什么?流不能在运行时被中断并再次启动。至少calculhash方法只能在需要时启动。我理解你的意思,为什么要投否决票。Thanks@efekctive这是一个小小的改变。你觉得我找到的解决方案怎么样?看来你已经绕过了操作系统阻塞