2016-06-29 9 views
1

Ich bekomme einen Fehler in Zeile 58, die besagt, dass die Datei nicht gefunden wird, aber sollte der PrintStream die Datei nicht erstellt haben? (Hinweis: Es wird überhaupt keine Datei erstellt.)PrintStream erstellt keine Datei?

Vielen Dank!

WhyDoINeedToAddMoreDetailIAlreadySaidWhatINeedToSay

import java.io.*; 
 
import java.util.*; 
 
import java.text.*; 
 

 
public class DownloadsFolderCleanup { 
 
    public static String directory = "C:\\Users\\User\\Downloads"; 
 
    
 
    public static void main(String[] args) throws FileNotFoundException { 
 
     File[] files = (new File(directory)).listFiles(); 
 
     if (files.length - 1 == 0) { 
 
     System.out.println("Downloads folder is already empty!"); 
 
     } 
 
     else { 
 
     ArrayList<File> markedFiles = new ArrayList<File>(); 
 
     int numInstallers = 0, numIncompleteDownloads = 0; 
 
     for (File file : files) { 
 
      String fileName = file.getName(); 
 
      if (fileName.substring(fileName.length() - 4,fileName.length()).equals(".exe")) { 
 
       if (fileName.toLowerCase().contains("install") || fileName.toLowerCase().contains("setup")) { 
 
        markedFiles.add(file); 
 
        numInstallers++; 
 
        System.out.println('"' + fileName + '"' + " marked for deletion. Reason: Installer"); 
 
       } 
 
      } 
 
      if (fileName.length() > 22) { 
 
       if (fileName.substring(0,11).equals("Unconfirmed") && fileName.substring(fileName.length() - 11, fileName.length()).equals(".crdownload")) { 
 
        markedFiles.add(file); 
 
        numIncompleteDownloads++; 
 
        System.out.println('"' + fileName + '"' + " marked for deletion. Reason: Incomplete download"); 
 
       } 
 
      } 
 
     } 
 
     System.out.println("- - - - - - - - - - - - - - - - - - - -"); 
 
     System.out.println("Total # of files scanned: " + (files.length - 1)); 
 
     System.out.println("Total # of junk files found: " + markedFiles.size()); 
 
     System.out.println("Installers found: " + numInstallers); 
 
     System.out.println("Incomplete download files found: " + numIncompleteDownloads); 
 
     if (markedFiles.size() == 0) { 
 
      System.out.println("No junk files were found!"); 
 
     } 
 
     else { 
 
      System.out.print("Please confirm removal of all identified files. CANNOT BE UNDONE! Y/N: "); 
 
      Scanner input = new Scanner(System.in); 
 
      if (input.nextLine().equalsIgnoreCase("Y")) { 
 
       System.out.println("All marked files will be permanently deleted in 5 seconds."); 
 
       for (int c = 4; c > 0; c--) { 
 
        sleep1second(); 
 
        System.out.println(c + "..."); 
 
       } 
 
       sleep1second(); 
 
       DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm"); 
 
       Date date = new Date(); 
 
       PrintStream log = new PrintStream(new File(dateFormat.format(date) + " Download Folder Cleanup Log.txt")); 
 
       for (File file : markedFiles) { 
 
        System.out.println('"' + file.getName() + '"' + " deleted."); 
 
        file.delete(); 
 
        log.println(file.getName()); 
 
       } 
 
       log.close(); 
 
      } 
 
     } 
 
     } 
 
     System.out.println(); 
 
     System.out.println("Cleanup process complete!"); 
 
    } 
 
    
 
    public static void sleep1second() { 
 
     try { 
 
      Thread.sleep(1000); 
 
     } 
 
     catch(InterruptedException ex) { 
 
      Thread.currentThread().interrupt(); 
 
     } 
 
    } 
 
}

Antwort

0

Ich vermute, da Ihr DateFormat"MM/tt/HH: mm" enthält "/" (Schrägstriche), dieser Weg ist ungültig und da Sie eine Datei in einem Pfad erstellen, der dateFormat.format(date) enthält, kann die Datei nicht erstellt werden.

In Windows die folgenden Zeichen sind verboten:

* . "/\ [ ] : ; | = , 
+0

Yup, das es fest. Ich habe die verbotenen Charaktere vergessen. Vielen Dank! – Locke

+0

Gut zu helfen: D –