1

I have a project that is supposed to read a file for lines of code, detects those lines of code and then gives the user the option to delete the code if it exists.

However, my code is running into some issues. It is unable to delete the file but gives the following error:

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

Here is my code:

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);
    }
   }
}  
Panda
  • 6,955
  • 6
  • 40
  • 55
  • 1
    The error is pretty straight forward, not sure what else you expect us to tell you besides: `The process cannot access the file because it is being used by another process.` That is, there is already another program that has the file open. – Matt Clark Dec 15 '18 at 02:22
  • 1
    https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being – ZhaoGang Dec 15 '18 at 02:22
  • So how can I change my code to stop the BufferedReader process on my virusfile2.txt? – Matthew Weis Dec 15 '18 at 02:40
  • Before trying to delete the file, close the `BufferedReader` object, you do this by calling `.close()` method on it's object, `br` in your case. – ImtiazeA Dec 15 '18 at 02:50
  • That does not work. I tried putting the br.close(); at the end of the try statement that initializes the BufferedReader and I still get the error. – Matthew Weis Dec 15 '18 at 03:09
  • Why do you need `Scanner filereader = new Scanner(virusfile2);` That might have been blocking the file. Possible to remove it if you are not using that line? – cokeby190 Dec 15 '18 at 04:21

2 Answers2

0

if the file is open , close it and then run your program

  • The file is not open. Everything works fine up to the point when it has to delete the file. It can read the file and then display the JOptionPane messages alerting them of the malicious code and asking if they want to delete them, but if you click yes, the exception appears and does not delete the file. – Matthew Weis Dec 15 '18 at 02:38
0

So, I found out that I was using two methods to open the file: The BufferedReader and the Scanner methods. I was closing the BufferedReader, but I wasn't closing the Scanner. Since the scanner was unnecessary in the first place, I just deleted it.