Advertisement
Guest User

Odczyt / zapis plików tekstowych

a guest
Dec 12th, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5. import java.util.StringJoiner;
  6.  
  7. public class Main {
  8.  
  9.  
  10.     public static void odczytPlikuTekstowego(String sciezka) throws IOException {
  11.         FileReader plikWej = null;
  12.  
  13.         try {
  14.             plikWej = new FileReader(sciezka);
  15.  
  16.             int kod  = plikWej.read();
  17.             while (kod != -1) {
  18.                 char znak = (char) kod;
  19.                 System.out.println("Znak: " + znak);
  20.                 kod  = plikWej.read();
  21.             }
  22.  
  23.         } finally {
  24.             if (plikWej != null) {
  25.                 plikWej.close();
  26.             }
  27.         }
  28.  
  29.         BufferedReader plikBuforowany = null;
  30.         try {
  31.             plikBuforowany = new BufferedReader( new FileReader(sciezka) );
  32.  
  33.             String wiersz = plikBuforowany.readLine();
  34.             int ile = 0;
  35.             while (wiersz != null) {
  36.                 ++ile;
  37.                 System.out.println(ile + " : " + wiersz);
  38.                 wiersz = plikBuforowany.readLine();
  39.             }
  40.         } finally {
  41.             if (plikBuforowany != null) {
  42.                 plikBuforowany.close();
  43.             }
  44.         }
  45.  
  46.         System.out.println("Odczyt za pomocą Scanner");
  47.         Scanner wej = null;
  48.         try {
  49.             wej = new Scanner(new BufferedReader( new FileReader(sciezka) ));
  50.  
  51.             while (wej.hasNext()) {
  52.                 if (wej.hasNextInt()) {
  53.                     int l = wej.nextInt();
  54.                     System.out.println("Liczba: " + l);
  55.                 } else {
  56.                     wej.next();
  57.                 }
  58.             }
  59.  
  60.         } finally {
  61.             if (wej != null) {
  62.                 wej.close();
  63.             }
  64.  
  65.         }
  66.     }
  67.  
  68.  
  69.     public static void zapisPlikuTekstowego(String sciezka) throws IOException {
  70.         FileWriter plikWyj = null;
  71.  
  72.         try {
  73.  
  74.             plikWyj= new FileWriter(sciezka, true);
  75.  
  76.             plikWyj.write("Dziś jest sobota.");
  77.  
  78.         } finally {
  79.             if (plikWyj != null) {
  80.                 plikWyj.close();
  81.             }
  82.         }
  83.  
  84.         PrintWriter wyjscie = null;
  85.         try {
  86.             wyjscie = new PrintWriter( new FileWriter(sciezka, true));
  87.  
  88.             for (int i = 0; i < 10; i++) {
  89.                 int p = 1 << i;
  90.                 wyjscie.println(p);
  91.  
  92.             }
  93.         } finally {
  94.  
  95.             if (wyjscie != null) {
  96.                 wyjscie.close();
  97.             }
  98.         }
  99.     }
  100.  
  101.     public static void main(String[] args)  {
  102.  
  103.         try {
  104.             // odczytPlikuTekstowego("dane.txt");
  105.  
  106.             String nazwa = "wyniki//p.txt";
  107.             zapisPlikuTekstowego(nazwa);
  108.         } catch (IOException e) {
  109.             System.out.println("Błąd odczytu: " + e.getLocalizedMessage());
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement