mika-el
3/19/2018 - 8:06 AM

Flux d'entrée

/*
 * Object File
 * import java.io.File;
 */
// Instanciation
File f = new File("file.txt");
// Méthodes
f.getAbsolutePath();
f.getName();
f.exists();
f.isDirectory();
f.isFile();
f.isDirectory();
f.listRoots();
f.listFiles();
f.delete();
f.mkdir();
// Lister les fichierS voisin
for(File file : f.listRoots()) {
  for(File nom : file.listFiles()) {
    nom.getName();
  }
}


/*
 * Lire un fichier avec FileInputStream OU BufferedReader
 * import java.io.File;
 */
  public static void main(String[] args) {
	FileInputStream fis = null;
	byte[] buf = new byte[8];
	int n = 0;
	
	BufferedReader br = null;
	String line;
	try {
		fis = new FileInputStream(new File("test.txt"));
		br = new BufferedReader(new FileReader("test.txt"));
		
		while ((n = fis.read(buf)) >= 0) {
            for (byte bit : buf) {
                System.out.print((char) bit);
             }
		}
		
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			if (fis != null) {
				fis.close(); 
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
  }