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.channels.FileChannel;

public class MainClass {
    public static void main(String[] args) {
        String phrase = new String("text\n");
        String dirname = "C:/test"; // Directory name
        String filename = "byteData.txt";
        File aFile = new File(dirname, filename);
        // Create the file output stream
        FileOutputStream file = null;
        try {
            file = new FileOutputStream(aFile, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace(System.err);
        }
        FileChannel outChannel = file.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(phrase.length());
        byte[] bytes = phrase.getBytes();
        buf.put(bytes);
        buf.flip();
        try {
            outChannel.write(buf);
            file.close();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
}