Facebook
From Social Butterfly, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 219
  1. import java.io.*;
  2. import java.util.Scanner;
  3. /**
  4. *
  5. * @author jmkoz_000
  6. */
  7. public class JavaApplication5 {
  8. public static void odczyt1(String name) throws IOException {
  9. FileReader in = null;
  10. try {
  11. in = new FileReader(name);
  12. int znak = in.read();
  13. while (znak != -1) {
  14. System.out.print((char) znak);
  15. znak = in.read();
  16. }
  17. } catch (FileNotFoundException ex) {
  18. System.out.println(ex.toString());
  19. } finally {
  20. if (in != null) {
  21. in.close();
  22. }
  23. }
  24. }
  25. public static void odczyt2(String name) throws IOException {
  26. BufferedReader in = null;
  27. try {
  28. in = new BufferedReader(new FileReader(name));
  29. String line;
  30. while((line = in.readLine())!=null){
  31. System.out.println(line);
  32. }
  33. } catch (FileNotFoundException ex) {
  34. System.out.println(ex.toString());
  35. } finally {
  36. if (in != null) {
  37. in.close();
  38. }
  39. }
  40. }
  41. public static void odczyt3(String name) throws IOException {
  42. Scanner in = null;
  43. try {
  44. in = new Scanner(new FileReader(name));
  45. int suma = 0;
  46. while(in.hasNext()){
  47. if(in.hasNextInt()){
  48. suma += in.nextInt();
  49. } else{
  50. in.next();
  51. }
  52. }
  53. System.out.println(suma);
  54. } finally {
  55. if (in != null) {
  56. in.close();
  57. }
  58. }
  59. }
  60. public static void zapis(String nazwa) throws IOException {
  61. FileWriter out = null;
  62. try{
  63. out = new FileWriter(nazwa, true);
  64. out.write("Ala ma kota i psa\r\n");
  65. } finally {
  66. if (out != null) {
  67. out.close();
  68. }
  69. }
  70. }
  71. public static void zapis2(String nazwa) throws IOException {
  72. PrintWriter out = null;
  73. try{
  74. out = new PrintWriter(new FileWriter(nazwa, true));
  75. out.printf("abc %.2f def", Math.PI);
  76. } finally {
  77. if (out != null) {
  78. out.close();
  79. }
  80. }
  81. }
  82. public static void main(String[] args) throws IOException {
  83. //odczyt2("src/javaapplication5/JavaApplication5.java");
  84. //odczyt3("abc.txt");
  85. zapis2("nowy.txt");
  86. }
  87. }