import java.io.*; /** * Titel: IO.java
* Buch: Grundkurs-Java für Wirtschaftsinformatiker. Vieweg 2007.
* Autoren: Klaus-Georg Deck und Herbert Neuendorf * Copyright: Copyright (c) 2007
* * @author Klaus-Georg Deck und Herbert Neuendorf
* @version 1.0
* */ public class IO { public static float round(float x, int digits) { int help = (int) Math.pow(10, digits); return ((float) Math.round(x * help)) / help; } public static double round(double x, int digits) { long help = (long) Math.pow(10, digits); return ((double) Math.round(x * help)) / help; } public static void advance(int n) { // gibt n Leerzeilen aus for (int i = 0; i < n; i++) writeln(); } public static void write(String s) { // gibt den String s aus System.out.print(s); System.out.flush(); } public static void write(Object o) { String str = (o==null)?"null":o.toString(); write( str ); } public static void write(int i) { // gibt die ganze Zahl i aus write("" + i); } public static void write(float f) { // gibt die Floatzahl f aus write("" + f); } public static void write(double d) { // gibt die Doublezahl d aus write("" + d); } public static void writeln(String s) { // gibt den String s aus + einen Zeilenvorschub write(s + '\n'); } public static void writeln(Object o) { write(o); writeln(); } public static void writeln(int i) { write(i); writeln(); } public static void writeln(float f) { write(f); writeln(); } public static void writeln(double d) { write(d); writeln(); } public static void writeln() { //macht einen Zeilenvorschub writeln(""); } public static String readString() { // liest einen vom Benutzer eingegebenen String (Zeile) ein try { return keyb.readLine(); } catch (IOException e) { throw new RuntimeException( e ); } } public static String promptAndReadString(String s) { // gibt den String s aus // und liest anschließend einen // vom Benutzer eingegebenen String ein. // Diese String wird zurückgegeben write(s); return readString(); } public static int readInt() { // liest eine vom Benutzer eingegebene Zahl ein return new Integer(readString()).intValue(); } public static int promptAndReadInt(String s) { // gibt den String s aus und liest anschließend die // vom Benutzer eingegebene Ganzzahl ein return new Integer(promptAndReadString(s)).intValue(); } public static char promptAndReadChar(String s) { // gibt den String s aus und liest anschließend den ersten // vom Benutzer eingegebenen Buchstaben ein return promptAndReadString(s).charAt(0); } public static double promptAndReadDouble(String s) { // gibt den String s aus und liest anschließend eine vom // Benutzer eingegebene Double-Zahl ein return (new Double(promptAndReadString(s)).doubleValue()); } public static float promptAndReadFloat(String s) { // gibt den String s aus und liest anschließend eine vom // Benutzer eingegebene Float-Zahl ein return (new Float(promptAndReadString(s)).floatValue()); } public static boolean promptAndReadBoolean(String s) { // gibt den String s aus und liest anschließend eine vom // Benutzer eingegebenen Booleschen Wert ein return (new Boolean(promptAndReadString(s)).booleanValue()); } private static BufferedReader keyb = new BufferedReader( new InputStreamReader(System.in)); public static void writeToFile(String filename, String content) { try { File datei = new File(filename); BufferedWriter out = new BufferedWriter(new FileWriter(datei)); if (content != null) out.write(content); out.close(); IO.writeln("Dateigroesse = " + datei.length()); } catch (IOException e) { throw new RuntimeException( e ); } } public static String readFromFile(String filename) { try { File datei = new File(filename); BufferedReader in = new BufferedReader(new FileReader(datei)); String content = ""; String result = ""; while ((content = in.readLine()) != null) { result = result + content + "\n"; } in.close(); return result; } catch (IOException e) { throw new RuntimeException( e ); } } public static void deleteFile(String filename) { try { File datei = new File(filename); if (datei != null) datei.delete(); } catch (Exception e) { throw new RuntimeException( e ); } } public static void main(String ag[]){ System.out.println(IO.promptAndReadInt("Eingabe: ")); } }