1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
class Choix{
public static void main(String[] arg) throws IOException {
// *************** première partie selection du fichier **********************
JFileChooser dialogue = new JFileChooser();
dialogue.showOpenDialog(null);
// première solution directe
// récupération du fichier sélectionné
System.out.println("Fichier choisi : " + dialogue.getSelectedFile());
// affichage
dialogue1.showOpenDialog(null);
System.out.println("Fichier choisi est: " + dialogue1.getSelectedFile());
///////// *************** seconde partie lecture du fichier *******************
File F3 = dialogue1.getSelectedFile();
FileInputStream fis =null;
FileOutputStream fos= null;
try{ ///instanciation des objets
fis = new FileInputStream (new File("test.txt"));
fos = new FileOutputStream (F3);
byte buf[] = new byte[8];
int n=0;
while ((n=fis.read(buf))>=0) {
fos.write(buf);
for (byte bit : buf) {
System.out.println("\t"+bit+"("+(char)bit+")");
System.out.println(" ");
buf= new byte[8];
}
}
}
catch (FileNotFoundException e)
{e.printStackTrace(); }
catch (IOException e)
{e.printStackTrace();}
finally
{
try
{if (fis!=null) fis.close(); }
catch (IOException e) {e.printStackTrace();};
}
}
} |
Partager