martedì 20 marzo 2012

Java stampare contenuto file come Stringa

Un semplice metodo per stampare il contenuto di un file come Stringa


public static String getStringFileContent(File f) throws IOException {
  StringBuffer sb=new StringBuffer();
  if(!f.exists())
   throw new IllegalArgumentException("Attenzione file non esistente");
  BufferedReader bf=new BufferedReader(new FileReader(f));
  String l=null;
  do
  {
    l=bf.readLine();
    if(l!=null)
    sb.append(l);
  }
  while(l!=null);
  bf.close();
  return sb.toString();
 }

Nessun commento:

Posta un commento