Lecture/copie contenu d'1 fichier avec java

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. Modérateur
    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...