Java Text File Read readFile(String path)

Here you can find the source of readFile(String path)

Description

read File

License

Open Source License

Declaration

public static String readFile(String path) 

Method Source Code


//package com.java2s;
/*//w w  w .ja v  a2 s  .  c o  m
 * Copyright (c) 2015, Varav Inc. All rights reserved.
 * License terms are included in LICESNSE.txt file.
 */

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class Main {
    public static String readFile(String path) {
        StringBuilder result = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(path));
            String buffer;
            while ((buffer = reader.readLine()) != null) {
                result.append(buffer + '\n');
            }
            reader.close();
        } catch (IOException e) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                JOptionPane.showMessageDialog(null, "<html><br>Could not read from file: " + path + "<br>"
                        + "Error: " + e.toString() + "</html>", "Error!", JOptionPane.ERROR_MESSAGE);
            } catch (Exception ee) {
            }
            e.printStackTrace();
        }
        return result.toString();
    }

    public static String[] readFile(String path1, String path2) {
        return new String[] { readFile(path1), readFile(path2) };
    }
}

Related

  1. getFileContents(String path)
  2. getFileContents(String pathToFile)
  3. getFileContents(String pFileName)
  4. loadAsString(String location)
  5. readFile(String filePath)
  6. readTextFile(File file, int max, String ellipsis)
  7. readTextFile(final DataInputStream is)
  8. readTextFile(final String path, final String enc)
  9. readTextFile(InputStream inStream)