Lecture/copie contenu d'1 fichier avec java

Fermé
ratouch Messages postés 7 Date d'inscription mercredi 2 février 2011 Statut Membre Dernière intervention 23 novembre 2011 - 20 mai 2011 à 02:49
KX Messages postés 16760 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 12 février 2025 - 20 mai 2011 à 07:20
Bonjour,
Je désire faire la lecture du contenu entier d'un fichier d'extension quelconque puis copier ce contenu dans un autre fichier. Et je veux utiliser java pour cette opération.
Comment ça se fait?



A voir également:

1 réponse

KX Messages postés 16760 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 12 février 2025 3 020
20 mai 2011 à 07:20
Voici une manière de faire :

public static void copyFile(String in, String out) throws IOException 
{
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = null;
    
    try 
    {
    	outChannel = new FileOutputStream(out).getChannel();
    	inChannel.transferTo(0, inChannel.size(), outChannel);
    } 
    catch (IOException e) 
    {
        throw e;
    }
    finally
    {
        if (inChannel != null) inChannel.close();
        if (outChannel != null) outChannel.close();
    }
}

NB. inChannel.size() "limite" l'utilisation de cette méthode à 8 Eo...
0