Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    /**
     * Reads the first non-empty line from a file.
     *
     * @param file the file from which the line is to be read. This argument
     *   cannot be {@code null}.
     * @return the first non-empty line in the given file or {@code null} if
     *   there is no such line in the specified file
     *
     * @throws IOException thrown if there was an error while reading the
     *   specified file (e.g.: it does not exist)
     */
    private static String readFirstLine(Path file) throws IOException {
        try (InputStream input = Files.newInputStream(file)) {
            LineNumberReader reader = new LineNumberReader(new InputStreamReader(input, DEFAULT_CHARSET));
            String line = reader.readLine();
            while (line != null) {
                line = line.trim();
                if (!line.isEmpty()) {
                    return line;
                }
                line = reader.readLine();
            }
        }

        return null;
    }
}