lecture fichier

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInput {

  public static void main(String[] args) {

    File file = new File("C:\\Test.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);
       // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);


 int i = 0;
 int j=0;
 String [][] Tab ;
      // dis.available() returns 0 if the file does not have more lines.
   
      int nbmax=0;
      String[] TEST = new String[20];
      while (dis.available() != 0) {
      TEST[j]=dis.readLine();
      System.out.println(TEST[j]);

      if(nbmax < TEST[j].length())
      {
      nbmax=TEST[j].length();
      }
         j++;
          }
      Tab = new String[j][2];
      for (i=1;i<j;i++)
      {
      Tab[i][0]= TEST[i].substring(0, 1);
      Tab[i][1]= TEST[i];
      }
      for (i=1;i<j;i++)
      {
      System.out.println("La phrase \""+Tab[i][1]+"\" se débute par "+Tab[i][0]);
      }
 // dispose all the resources after using them.
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Commentaires

Posts les plus consultés de ce blog

factorielle

testthread

tri avec multithread