Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Main {
    /**
     * Read a file and return as a String
     *
     * @param file the file to read
     * @return the string representing the contents of the file
     * @throws IOException when reading the file fails
     * @since 0.11.0
     */
    public static String readFile(File file) throws IOException {
        Reader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        try {
            StringBuilder sb = new StringBuilder();
            for (;;) {
                String line = br.readLine();
                if (line == null) {
                    return sb.toString();
                } else {
                    sb.append(line);
                }
                sb.append(System.getProperty("line.separator"));
            }
        } finally {
            br.close();
        }
    }
}