import java.io.*; import java.util.Scanner; /** * * @author jmkoz_000 */ public class JavaApplication5 { public static void odczyt1(String name) throws IOException { FileReader in = null; try { in = new FileReader(name); int znak = in.read(); while (znak != -1) { System.out.print((char) znak); znak = in.read(); } } catch (FileNotFoundException ex) { System.out.println(ex.toString()); } finally { if (in != null) { in.close(); } } } public static void odczyt2(String name) throws IOException { BufferedReader in = null; try { in = new BufferedReader(new FileReader(name)); String line; while((line = in.readLine())!=null){ System.out.println(line); } } catch (FileNotFoundException ex) { System.out.println(ex.toString()); } finally { if (in != null) { in.close(); } } } public static void odczyt3(String name) throws IOException { Scanner in = null; try { in = new Scanner(new FileReader(name)); int suma = 0; while(in.hasNext()){ if(in.hasNextInt()){ suma += in.nextInt(); } else{ in.next(); } } System.out.println(suma); } finally { if (in != null) { in.close(); } } } public static void zapis(String nazwa) throws IOException { FileWriter out = null; try{ out = new FileWriter(nazwa, true); out.write("Ala ma kota i psa\r\n"); } finally { if (out != null) { out.close(); } } } public static void zapis2(String nazwa) throws IOException { PrintWriter out = null; try{ out = new PrintWriter(new FileWriter(nazwa, true)); out.printf("abc %.2f def", Math.PI); } finally { if (out != null) { out.close(); } } } public static void main(String[] args) throws IOException { //odczyt2("src/javaapplication5/JavaApplication5.java"); //odczyt3("abc.txt"); zapis2("nowy.txt"); } }