Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static void main(String[] args) {
        File inputFile = new File("test1.txt");
        if (!inputFile.exists()) {
            System.out.println("The input file " + inputFile.getAbsolutePath() + "  does  not  exist.");
            System.out.println("Aborted the   file reading process.");
            return;
        }
        try (FileChannel fileChannel = new FileInputStream(inputFile).getChannel()) {
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (fileChannel.read(buffer) > 0) {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    byte b = buffer.get();
                    System.out.print((char) b);
                }
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}