Visualizzazione dei risultati da 1 a 10 su 10

Discussione: Rinominare file

Visualizzazione discussione

  1. #8
    Utente di HTML.it
    Registrato dal
    Feb 2007
    Messaggi
    4,157
    guarda ho un po ordinato il tuo codice separando i passi (e cercando di farti vedere che in questo modo loggando per bene è più facile capire in quale punto il problema si crea).

    codice:
    public void addPrenotazione(ArrayList<Posto> a){
            // Sarebbe meglio separare le operazioni, in realtà andrebbero in metodi differenti
            // credo che linee sia una variabile globale
            
            File out=new File("file1_agg.txt");
            File inFile=new File("file1.txt");
            try{
                 
                List<Posto> linee = loadFile(inFile);     
                // non ho capito che fai, lo sai tu, ma lascio stare 
                int i = 0,j =0;
                while(i<a.size()) {
                    if(a.get(i).fila==linee.get(j).fila && a.get(i).posto==linee.get(j).posto){
                        linee.get(j).flagDis+=1;
                        i++;
                        j++;
                    }
                    else {j++;}
                }
    
    
                writeFile(linee, out);
                
                if (!inFile.delete()) {
                    System.out.print("File Non Eliminato");
                    //qui dovresti fermarti visto che il file non eliminato non ti permette di fare rename. 
                }
    
    
    
    
                /*  CREO IL FILE DI AGGIORNAMENTO CON LA PRENOTAZIONE NUOVA */
                
                
                boolean Rinomina = out.renameTo(inFile);
                    if(!Rinomina){System.out.println("File o directory non rinominati. PRENOTAZIONE NON COMPLETATA.");}     
                }
                catch(IOException e){
                    // almeno all'inizio vedi tutto 
                    e.printStackTrace(); 
    
    
                }
            }
    
    
    
    
            private List<Posto> loadFile(File inFile) throws IOException {
    
    
                List<Posto> ret = new ArrayList<Posto>(); 
                Scanner scan = null; 
                 try{
                   
                    scan = new Scanner(inFile);
                        
                    int h=0;
                      while(scan.hasNext()){
                      //riempio l'arraylist con i dati del file
                      ret.add(new Posto(scan.next().charAt(0),scan.nextInt(),scan.nextInt()));
                      System.out.println(linee.get(h).fila+" "+linee.get(h).posto+" "+linee.get(h).flagDis);
                      h++;
                   }
                }finally{
                    if (scan != null){
                        try{
                            scan.close(); 
                        }catch(Exception e){
                            // fai quello che ti pare 
                        }
                    }
                }
                return ret; 
            }
    
    
            private void writeFile(List<Posto> linee, File out){
                 PrintStream scrivi = null; 
                try {
    
    
                    //APRO FILE DI OUTPUT
                    
                    scrivi = new PrintStream(out);
                    for(int k=0;k<linee.size();k++){
                        scrivi.println(linee.get(k).fila+" "+linee.get(k).posto+" "+linee.get(k).flagDis);
    
    
                    }
                    scrivi.flush(); // io lo faccio sempre per sicurezza
                 }finally{
                    if (scrivi != null){
                        try{
                            scrivi.close(); 
                        }catch(Exception e){
                            // fai quello che ti pare 
                        }
                    }
                }
                
            }
    ora sei sicuro che ad ogni giro il file di partenza è cancellato?
    perché guarda qui

    http://docs.oracle.com/javase/6/docs...ava.io.File%29



    public boolean renameTo(File dest)

    Renames the file denoted by this abstract pathname.Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

    Parameters:
    dest - The new abstract pathname for the named file
    Returns:true if and only if the renaming succeeded; false otherwise
    Throws:SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to either the old or new pathnames
    NullPointerException - If parameter dest is null
    Ultima modifica di valia; 23-12-2014 a 13:17
    RTFM Read That F*** Manual!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.