2012-08-07 38 views
9

Mi chiedo perché ottengo questo avviso con la nuova eclisse Juno nonostante penso di aver chiuso correttamente tutto. Potresti dirmi perché ho ricevuto questo avvertimento nel seguente pezzo di codice?Eclipse Juno: valore non assegnato chiudibile

public static boolean copyFile(String fileSource, String fileDestination) 
{ 
    try 
    { 
     // Create channel on the source (the line below generates a warning unassigned closeable value) 
     FileChannel srcChannel = new FileInputStream(fileSource).getChannel(); 

     // Create channel on the destination (the line below generates a warning unassigned closeable value) 
     FileChannel dstChannel = new FileOutputStream(fileDestination).getChannel(); 

     // Copy file contents from source to destination 
     dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); 

     // Close the channels 
     srcChannel.close(); 
     dstChannel.close(); 

     return true; 
    } 
    catch (IOException e) 
    { 
     return false; 
    } 
} 

risposta

16

se si sta eseguendo su Java 7, è possibile utilizzare i nuovi blocchi try-con-risorse come così, e i flussi saranno chiuse automaticamente:

public static boolean copyFile(String fileSource, String fileDestination) 
{ 
    try(
     FileInputStream srcStream = new FileInputStream(fileSource); 
     FileOutputStream dstStream = new FileOutputStream(fileDestination)) 
    { 
     dstStream.getChannel().transferFrom(srcStream.getChannel(), 0, srcStream.getChannel().size()); 
     return true; 
    } 
    catch (IOException e) 
    { 
     return false; 
    } 
} 

Non sarà necessario in modo esplicito vicino i canali sottostanti. Tuttavia, se non si sta usando Java 7, si dovrebbe scrivere il codice in un modo vecchio ingombrante, con blocchi finally:

public static boolean copyFile(String fileSource, String fileDestination) 
{ 
    FileInputStream srcStream=null; 
    FileOutputStream dstStream=null; 
    try { 
     srcStream = new FileInputStream(fileSource); 
     dstStream = new FileOutputStream(fileDestination) 
     dstStream.getChannel().transferFrom(srcStream.getChannel(), 0, srcStream.getChannel().size()); 
     return true; 
    } 
    catch (IOException e) 
    { 
     return false; 
    } finally { 
     try { srcStream.close(); } catch (Exception e) {} 
     try { dstStream.close(); } catch (Exception e) {} 
    } 
} 

vedere quanto migliore è la versione Java 7 è :)

+0

Questo funziona ma vorrei ora come rimuovere questo avviso senza utilizzare questa funzione! E perché non è possibile dichiarare direttamente il FileChannel nelle risorse. EDIT: hai appena risposto alla mia domanda ma perché non chiudi il fileChannel? – Abbadon

+0

Quando chiudi lo stream, chiuderà il canale. Non è necessario chiuderlo esplicitamente. – Strelok

+0

Ho completamente perso il fatto che (per il codice java7) la dichiarazione del nuovo FileInputStream e OutputStream si verifica prima di aprire le parentesi per il try {}. Immagino che tu abbia menzionato questo chiamandoli un blocco try-with-resources. Dopo aver corretto ciò, gli avvertimenti sono scomparsi. Lo adoro! –

4

Si dovrebbe sempre vicino a finally perché se un aumento un'eccezione, non sarà possibile chiudere le risorse.

FileChannel srcChannel = null 
try { 
    srcChannel = xxx; 
} finally { 
    if (srcChannel != null) { 
    srcChannel.close(); 
    } 
} 

Nota: anche se si mette un ritorno nel blocco catch, il blocco finally sarà fatto.

+0

Con questa soluzione ho ottenere un IOException non gestito! – Abbadon

+0

Bene È stato un esempio, inserire un blocco 'catch (IOException ioe)' .... –

3

eclipse ti avvisa del FileInputStream e FileOutputStream che non puoi più fare riferimento.

Problemi correlati