Write A String As Bytes : New IO « File Input Output « Java

Write A String As Bytes

 * Output:

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("www.java2s.com API \n");
    File aFile = new File("test.txt");

    FileOutputStream file = null;
    try {
      file = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
    FileChannel outChannel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());
    byte[] bytes = phrase.getBytes();

    try {
    } catch (IOException e) {


Related examples in the same category

1.Use a mapped file to read a text file
2.FileChannel: map(FileChannel.MapMode mode,long position,long size)
3.Write to a file using the new I/O
4.Write to a mapped file
5.Copy a file using NIO
6.Use New Java IO to write string into a file