import java.io.FileOutputStream;
import java.util.Formatter;
public class ExceptionFormatter {
public static void main(String[] argv) throws Exception {
Formatter fmtCon = new Formatter(System.out);
Formatter fmtFile;
fmtFile = new Formatter(new FileOutputStream("test.txt"));
fmtCon.format("Ajout d'un nombre négatif: %(.2fn", -123.34);
fmtCon.format("%8s %8sn", "Nombre", "Carré");
for (int i = 1; i < 10; i++)
fmtCon.format("%8d %8dn", i, i * i);
fmtFile.format("Ajout d'un nombre positif: %.2fn", 3.14);
//Ecrire le résultat dans un fichier
fmtFile.format("%8s %8sn", "Nombre", "Cube");
for (int i = 1; i < 30; i++)
fmtFile.format("%8d %8dn", i, i*i*i);
fmtFile.close();
if (fmtFile.ioException() != null) {
System.out.println("Exception générée pendant l'écriture dans le fichier");
}
}
}
Afficher la date complete avec le fuseau horaire
Classe Formatter: Modification des symboles