Writing a file using I/O in Java

This piece of code writes a string of characters to a new text file in order to demonstrate how the Java IO operations work.
1.import java.io.fOutput;
2.import java.io.IOException;
3.import java.nio.ByteBuffer;
4.import java.nio.channels.fChannel;
5. 
6.public class MainClass
7.{
8.  public static void main(String args[])
9.  {
10.    fOutput fOutput;
11.    fChannel fChannel;
12.    ByteBuffer byteBuffer;
13. 
14.    try
15.    {
16.      fOutput = new fOutput("test.txt");
17.      fChannel = fOutput.getChannel();
18.      byteBuffer = ByteBuffer.allocateDirect(26);
19. 
20.      for (int i = 0; i < 26; i++)
21.      {
22.        byteBuffer.put((byte) ('A' + i));
23.      }
24.      byteBuffer.rewind();
25.      fChannel.write(byteBuffer);
26.      fChannel.close();
27.      fOutput.close();
28.    }
29.    catch (IOException exc)
30.    {
31.      System.out.println(exc);
32.    }
33.  }
34.}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top