Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import javax.swing.JOptionPane;
- /**
- *
- * @author IT Academy
- */
- class C_BezpecnaPracaSubor {
- public static String precitajObsahSuboru(String menoSuboru) {
- String obsahSouboru = "";
- BufferedReader bfr = null;
- try {
- bfr = new BufferedReader(
- new FileReader(
- new File(menoSuboru)));
- String riadok = null;
- while ((riadok = bfr.readLine()) != null) {
- obsahSouboru += riadok;
- }
- } catch (IOException e) {
- // Chybové okno
- JOptionPane.showMessageDialog(
- null,
- e.getLocalizedMessage(),
- "Chyba pri práci so suborom",
- JOptionPane.ERROR_MESSAGE);
- } finally {
- if (bfr != null) {
- // Zavretie súboru, pokial bol predtým otvorený
- try {
- bfr.close();
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- }
- if (obsahSouboru.length() == 0) {
- // Ukončenie programu pokial neboli načítané žiadne dáta
- System.exit(1);
- }
- return obsahSouboru;
- }
- }
- public static void main(String[] args) {
- String text;
- text = precitajObsahSuboru("data.txt");
- System.out.println(text);
- text = precitajObsahSuboru("data2.txt");
- System.out.println(text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement