Java Path File Name nio getFileContent(String fileNameOrPath)

Here you can find the source of getFileContent(String fileNameOrPath)

Description

get File Content

License

Open Source License

Declaration

public static String getFileContent(String fileNameOrPath) 

Method Source Code


//package com.java2s;
/*//from w w  w  .  j  a  v  a 2 s .  co m
 * gMix open source project - https://svs.informatik.uni-hamburg.de/gmix/
 * Copyright (C) 2012  Karl-Peter Fuchs
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.RandomAccessFile;

import java.nio.channels.Channels;

public class Main {
    public static String getFileContent(String fileNameOrPath) {
        String result = "";
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(new DataInputStream(new FileInputStream(fileNameOrPath))));
            String line;
            while ((line = br.readLine()) != null)
                result += line + "\n";
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("ERROR: could not read from file " + fileNameOrPath + "!");
        }
        return result;
    }

    public static String readLine(long offset, String filePointer) throws IOException {
        String result;
        RandomAccessFile raf = new RandomAccessFile(filePointer, "r");
        raf.seek(offset);
        // use BufferedReader to read line instead of raf.readLine() for performance reasons:
        BufferedReader reader = new BufferedReader(Channels.newReader(raf.getChannel(), "ISO-8859-1"));
        result = reader.readLine();
        raf.close();
        return result;
    }
}

Related

  1. getDatabaseDirectoryPath(String databaseDirectory, String name)
  2. getDataPath(String filename)
  3. getDeckNameFromFile(final Path deckFile)
  4. getDefaultPathName(final String address, final long lspId)
  5. getDirectoryNames(Path baseDirectory)
  6. getFileInFolder(Path p, String filename)
  7. getFilename (final String filepath)
  8. getFilename(final Path path)
  9. getFilename(final Path path)