0

I have the following method:

  public static void main(String[] args)
  {
      DataWriter.addScore(2, DataWriter.score(8.5,2.25, 170.0);
   }

And this class, DataWriter:

   public class DataWriter
    {
            public static int findTextInFile(String s) //returns an int representing the line number of String s
            {
                try
                {
                int ln = 0;
                File data = new File("data.txt");

                    String line = null;
                    Scanner fileInput = new Scanner(data);
                    while(fileInput.hasNextLine())
                    {
                    String prevLine = fileInput.nextLine();
                    ln++;
                    if(prevLine.equals(s))
                    {
                     //   br.close(); 
                        return ln;
                    }

                }
               //br.close();
            }
            catch(Exception e) { System.out.println(e); } 

            return 0;

        }
        public static void addScore(int prompt, String stats) //Calls the WriteAfterNthLine method with a prompt number and stats string.
            {
                try
                {
                    switch(prompt)
                    { 
                        case 1:
                            writeAfterNthLine("data.txt", stats, findTextInFile("NORMAL"));
                            break;
                        case 2:
                            writeAfterNthLine("data.txt", stats, findTextInFile("SMALL"));
                            break;
                        case 3:
                            writeAfterNthLine("data.txt", stats, findTextInFile("NUMBERS"));
                            break;
                        case 4:
                            writeAfterNthLine("data.txt", stats, findTextInFile("COMMON WORDS"));
                            break;
                        case 5:
                            writeAfterNthLine("data.txt", stats, findTextInFile("OTHER"));
                            break;
                        default:
                            System.out.println("ERROR INVALID SUBMISSION"); 
                        break;
                }
                }
                catch(Exception e)
                {
                    System.out.println(e);
                }
            }
   public static void writeAfterNthLine(String filename, String text, int lineno) throws IOException //Writes String to filename on line lineno
        {
        //TODO:Document this method.
            File file = new File(filename); 
            File tmpDir = new File("C:/Users/jacobw4107/Documents/EZType"); //This is the current directory.
            System.out.println("Made temporary Directory " +tmpDir);
            File temp = File.createTempFile("temp-file-name", ".tmp", tmpDir);
            BufferedReader br = new BufferedReader(new FileReader( file ));
            PrintWriter pw =  new PrintWriter(new FileWriter( temp ));
            String line;
            int lineCount = 0;
            while ((line = br.readLine()) != null) {
                pw.println(line);
                System.out.println(line);
                if(lineCount==lineno){
                    pw.println(text);
                }
                lineCount++;
            }
            br.close();
            pw.close();
            File old = new File("data.old");
            if(old.exists())
                System.out.println(old.delete()); 
            System.out.println(file.renameTo(new File("data.old")));
            System.out.println(temp.renameTo(new File("data.txt")));
    }
           }

When I call the writeAfterNthLine method by itself, everything functions correctly. However, Running the main method above causes the final two lines of the writeAfterNthLine method to fail. The final two and renameto lines fail, and return false. If I try to manually rename data.txt, I am given the following error:

The action can't be completed because the file is open in Java(TM) Platform SE binary.

So which method is locking the file?

I am running Windows 10 Education version 1703. I do not have administrator privileges.

LonelyPyxel
  • 111
  • 1
  • 8

1 Answers1

0

Try:

public static void addScore(int prompt, String stats) //Calls the WriteAfterNthLine method with a prompt number and stats string.
 {
   String /*Object ...??*/ tmp = null;
   try {
     switch(prompt) { 
       case 1:
         tmp = findTextInFile("NORMAL");
         break;
       case 2:
         tmp = findTextInFile("SMALL");
         break;
       case 3:
         tmp = findTextInFile("NUMBERS");
         break;
       case 4:
         tmp = findTextInFile("COMMON WORDS");
         break;
       case 5:
         tmp = findTextInFile("OTHER");
         break;
       default:
         System.out.println("ERROR INVALID SUBMISSION"); 
         break;
     }// end-switch
   } catch(Exception e) {// end-try
     System.out.println(e);
   }
   if(tmp != null) {
     writeAfterNthLine("data.txt", stats, tmp); // ;)
   }
 }//end-addScore

And please close your resources (Streams/Readers/Buffers/Writers/Scanners(!)/Connections etc...)!:

And to your actual question: I assume the leak in your un-closed Scanner (in findInTextInFile)..and the deadlock in the chained invocation: writeFile(..., readFile()) ...of the same file! ...but "other/orphan processes" can also be an issue.

xerx593
  • 12,237
  • 5
  • 33
  • 64