Advertisement
Sanady

Java

Mar 3rd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package zadania1;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStreamWriter;
  14. import java.io.PrintWriter;
  15.  
  16. public class Zadania1
  17. {
  18.     public static void main(String[] args)
  19.     {
  20.         Zadania1 zad = new Zadania1();
  21.         zad.NacitajObsahZoSubora("C:\\Users\\Ivan\\Desktop\\test.txt");
  22.         zad.ZapisObsahDoSubora("C:\\Users\\Ivan\\Desktop\\test.txt", "Fuck this shit I am out");
  23.     }
  24.    
  25.     public void NacitajObsahZoSubora(String fileName)
  26.     {
  27.         String text = "", tmp;
  28.         try
  29.         {
  30.             FileInputStream file = new FileInputStream(fileName);
  31.             InputStreamReader input = new InputStreamReader(file);
  32.             try (BufferedReader fromFile = new BufferedReader(input))
  33.             {
  34.                 while((tmp = fromFile.readLine()) != null)
  35.                 {
  36.                     text = text+ "\n" + tmp;
  37.                 }
  38.                 System.out.println("Obsah suboru:\n" + text);
  39.             }
  40.         }
  41.         catch(IOException e)
  42.         {            
  43.         }  
  44.     }
  45.    
  46.     public void ZapisObsahDoSubora(String fileName, String writeText)
  47.     {
  48.         try
  49.         {
  50.             FileOutputStream file = new FileOutputStream(fileName);
  51.             OutputStreamWriter output = new OutputStreamWriter(file);
  52.             try (PrintWriter toFile = new PrintWriter(output))
  53.             {
  54.                 toFile.println("" + writeText);
  55.             }
  56.         }
  57.         catch (IOException e)
  58.         {  
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement