Advertisement
IT-Academy

Java Secure File

May 30th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import javax.swing.JOptionPane;
  7.  
  8. /**
  9.  *
  10.  * @author IT Academy
  11.  */
  12. class C_BezpecnaPracaSubor {
  13.  
  14.     public static String precitajObsahSuboru(String menoSuboru) {
  15.         String obsahSouboru = "";
  16.         BufferedReader bfr = null;
  17.  
  18.         try {
  19.             bfr = new BufferedReader(
  20.                     new FileReader(
  21.                             new File(menoSuboru)));
  22.             String riadok = null;
  23.             while ((riadok = bfr.readLine()) != null) {
  24.                 obsahSouboru += riadok;
  25.             }
  26.         } catch (IOException e) {
  27.             // Chybové okno
  28.             JOptionPane.showMessageDialog(
  29.                     null,
  30.                     e.getLocalizedMessage(),
  31.                     "Chyba pri práci so suborom",
  32.                     JOptionPane.ERROR_MESSAGE);
  33.         } finally {
  34.             if (bfr != null) {
  35.                 // Zavretie súboru, pokial bol predtým otvorený
  36.                 try {
  37.                     bfr.close();
  38.                 } catch (IOException ex) {
  39.                     throw new RuntimeException(ex);
  40.                 }
  41.             }
  42.             if (obsahSouboru.length() == 0) {
  43.                 // Ukončenie programu pokial neboli načítané žiadne dáta
  44.                 System.exit(1);
  45.             }
  46.             return obsahSouboru;
  47.         }
  48.  
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         String text;
  53.         text = precitajObsahSuboru("data.txt");
  54.         System.out.println(text);
  55.         text = precitajObsahSuboru("data2.txt");
  56.         System.out.println(text);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement