java - 我的 Java 程序出现 FileSystemException 错误

标签 java file process bufferedreader filesystemexception

我有一个项目,该项目应该读取文件中的代码行,检测这些代码行,然后为用户提供删除代码(如果存在)的选项。

但是,我的代码遇到了一些问题。无法删除该文件,但出现以下错误:

The process cannot access the file because it is being used by another process.

这是我的代码:

import java.util.Scanner;

import java.io.File;

import java.io.IOException;

import java.awt.Desktop;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.io.BufferedReader;

import javax.swing.JOptionPane;

import java.nio.file.*;

class mtd_Scan_Files{

  public static void main(String[] args)
  {

    try{
    double count = 0, countBuffer=0,countLine=0;
    Scanner scan = new Scanner(System.in);
    File virusfile2 = new File("F:\\Users\\Matthew\\virusfile2.txt");
    String name = "virusfile2.txt";
    BufferedReader br;
    boolean exists2 = virusfile2.exists();
    Scanner filereader = new Scanner(virusfile2);
    String maliciouscode = "deletesystem32";
    String maliciouscode2 = "downloadmoreram";
    String maliciouscode3 = "helloworld";
    String line = "";
    int x = 0;
    int y = 0;
    int z = 0; //These three integers are flag variables used to indicate if the malicious code exists
    try{     
      if(exists2 || virusfile2.exists()){   
        try{
          br = new BufferedReader(new FileReader(virusfile2));
          try{
            while((line = br.readLine()) != null){
              countLine++;
              String[] numberlines = line.split(" ");
              for(String word : numberlines){
                if(word.equals(maliciouscode)){
                  x++; //Increase the flag variable by one for every time the maliciouscode is found in the document
                }
                else if(word.equals(maliciouscode2)){
                  y++;//Increase the flag variable by one for every time the maliciouscode2 is found in the document
                }
                else if(word.equals(maliciouscode3)){
                  z++;//Increase the flag variable by one for every time the maliciouscode3 is found in the document
                }
              }
          }
        }
        catch(Exception e){
          e.printStackTrace();
        }
        }
        catch(Exception e){
          e.printStackTrace();
        }
  }

    else{
      JOptionPane.showMessageDialog(null, "virusfile2 does not exist on this computer", "Java Antivirus - DOES NOT EXIST!",
                                        JOptionPane.INFORMATION_MESSAGE);
    }

   }

    catch(Exception e){

      e.printStackTrace();

      }
    if(x == 0 && y == 0 && z == 0){

      JOptionPane.showMessageDialog(null, "This file is clean of malicious code", "Java Antivirus - CLEAN!",
                                        JOptionPane.INFORMATION_MESSAGE);
    }
    else{
      int m = JOptionPane.showConfirmDialog(null, 
             "virusfile2.txt contains the following malicious phrases:\n deletesystem32: " + x + "\n downloadmoreram: " + y + "\n hellowworld: " + z + "\n Do you want Java Antivirus to remove it?",
              "Java Antivirus - WARNING!", JOptionPane.YES_NO_OPTION);
      if(m == 0){
        try{
          virusfile2.delete();
          Path malwarepath = Paths.get("F:\\Users\\Matthew\\virusfile2.txt");
          Files.delete(malwarepath);
          JOptionPane.showMessageDialog(null, "Virus file was removed!", "Java Antivirus - REMOVED!",
                                        JOptionPane.INFORMATION_MESSAGE);
        }
        catch(Exception e){
          e.printStackTrace();
        }
      }
      else{
        JOptionPane.showMessageDialog(null, "Virus file was not removed!", "Java Antivirus - NOT REMOVED!",
                                        JOptionPane.INFORMATION_MESSAGE);
      }
    }
    }
    catch(FileNotFoundException e){
      JOptionPane.showMessageDialog(null, "virusfile2 does not exist on this computer", "Java Antivirus - DOES NOT EXIST!",
                                        JOptionPane.INFORMATION_MESSAGE);
    }
   }
}  

最佳答案

如果文件已打开,请将其关闭,然后运行您的程序

关于java - 我的 Java 程序出现 FileSystemException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53788999/

相关文章:

c# - 监视特定进程的网络性能

java - 不同 Java 版本的奇怪默认方法行为

java - 使用线程运行不属于事务的方法?

java - 在 fragment 上使用 setter 时出现 NullPointerException

javascript - Reactjs 读取属性文件?

JavaFX - 无法加载项目目录内的图像

ruby - 读取文件时如何避免被 UTF-8 BOM 绊倒

java - Akka 启动并访问 actors 返回的数据

linux - 休眠进程的 CPU 使用率很高

Linux I/O 到正在运行的守护进程/进程