0
public void removeLine(String s) throws IOException, FileNotFoundException{ 

    File tempFile = new File("temp.txt"); 
    FileInputStream reader = new FileInputStream(sharkFile); 
    Scanner scanner = new Scanner(reader); 
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile, true)); 

    String currentLine; 
    while(scanner.hasNextLine()){ 
     currentLine = scanner.nextLine(); 
     String trimmedLine = currentLine.trim(); 
     System.out.println(trimmedLine); 
     trimmedLine.equals(sharkName); 
     if(trimmedLine.equals(sharkName)) continue; 
     writer.write(currentLine + System.getProperty("line.separator")); 
    } 

    scanner.close(); 
    scanner = null; 
    reader.close(); 
    writer.flush(); 
    writer.close(); 
    writer = null; 
    System.gc(); 
    if(!sharkFile.delete()){ 
     System.out.println("Could not delete file d"); 
     return; 
    } 
    if(!tempFile.renameTo(sharkFile)){ 
     System.out.println("Could not rename file"); 
     return; 
    } 
} 

Je suis passé par de nombreux threads sur stackoverflow et j'ai implémenté ces changements, mais mon fichier ne sera pas supprimé. Appréciez l'aide.pourquoi mon fichier ne supprime pas, peu importe pourquoi je le fais?

+5

Avez-vous vérifié les permissions des utilisateurs? Quelle erreur obtenez vous? – johan855

+0

Attrapez-vous l'exception d'ES quelque part le cas échéant? –

+0

Utilisez 'Files.delete()'; Au moins, vous obtiendrez une exception significative en cas d'échec. Aussi, pourquoi 'System.gc()'? – fge

Répondre

0

Utilisez le code ci-dessous, renommer le fichier avant de supprimer, il est apparaît que vous accédez nom de fichier après suppression:

try { //rename file first     
    tempFile.renameTo(sharkFile); 

    } catch (Exception e) { 
        JOptionPane.showMessageDialog(null, "Unable to rename.");       
        } 

try {      
    sharkFile.delete();        
    } 
catch(Exception e) { 
        JOptionPane.showMessageDialog(null, "Unable to delete.");      
        }      
+0

comment cela pourrait-il aider? Comment utiliser un RandomAccessFile mieux qu'un BufferedReader? –

1

L'API File est notoirement faible à expliquer pourquoi quelque chose échouent, par exemple File.delete() renvoie simplement un boolean, et la valeur false ne peut pas expliquer pourquoi.

Utilisez plutôt la nouvelle API Path.

Veuillez également (SVP!) Utiliser try-with-resources.

Scanner est lent, il est donc préférable d'utiliser BufferedReader, et pour écrire les lignes avec des retours à la ligne, utilisez un PrintWriter.

Path sharkPath = sharkFile.toPath(); 
Path tempPath = Paths.get("temp.txt"); 
Charset cs = Charset.defaultCharset(); 
try (BufferedReader reader = Files.newBufferedReader(sharkPath, cs); 
    PrintWriter writer = new PrintWriter(Files.newBufferedWriter(tempPath, cs, StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE)))) { 
    for (String currentLine; (currentLine = reader.readLine()) != null;) { 
     String trimmedLine = currentLine.trim(); 
     System.out.println(trimmedLine); 
     if (! trimmedLine.equals(sharkName)) 
      writer.println(currentLine); 
    } 
} 
Files.delete(sharkPath); // throws descriptive exception if cannot delete 
Files.move(tempPath, sharkPath); // throws exception if cannot move