MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class MainClass {

    public static void main(String[] args) {
        File aFile = new File("afile.txt");
        FileOutputStream outputFile = null;
        try {
            outputFile = new FileOutputStream(aFile, true);
            System.out.println("File stream created successfully.");
        } catch (FileNotFoundException e) {
            e.printStackTrace(System.err);
        }
        ByteBuffer buf = ByteBuffer.allocate(1024);
        System.out.println("\nByte buffer:");
        System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", buf.position(), buf.limit(),
                buf.capacity());

        // Create a view buffer
        CharBuffer charBuf = buf.asCharBuffer();
        System.out.println("Char view buffer:");
        System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", charBuf.position(), charBuf.limit(),
                charBuf.capacity());
        try {
            outputFile.close(); // Close the O/P stream & the channel
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
}