Java File Content Read getFileContent(String filename)

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

Description

Helper function for reading file

License

Open Source License

Declaration

@Nullable
public static String getFileContent(String filename) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.annotation.Nullable;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**/* w  ww .  j  av  a 2s  .com*/
     * Helper function for reading file
     */
    @Nullable
    public static String getFileContent(String filename) {
        String content = null;
        File file = new File(filename);

        if (!file.exists()) {
            return null;
        }

        try {
            FileReader reader = new FileReader(file);
            char[] chars = new char[(int) file.length()];
            reader.read(chars);
            content = new String(chars);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}

Related

  1. getFileContent(File file)
  2. getFileContent(File file)
  3. getFileContent(File file)
  4. getFileContent(File file)
  5. getFileContent(String filename)
  6. getFileContent(String fileName)
  7. getFileContent(String filename)
  8. getFileContent(String fileName)
  9. getFileContent(String fileName)