Lecture/copie contenu d'1 fichier avec java

ratouch Messages postés 8 Date d'inscription   Statut Membre Dernière intervention   -  
KX Messages postés 19031 Statut Modérateur -
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?

1 réponse

  1. KX Messages postés 19031 Statut Modérateur 3 020
     
    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