Skip to content

📋 Java code to access data in files using NIO File classes

Notifications You must be signed in to change notification settings

AndrewJBateman/java-nio-files

Repository files navigation

⚡Java NIO

  • Java coding project for a Java Programming Masterclass see 👏 Inspiration below

*** Note: to open web links in a new window use: ctrl+click on link**

📄 Table of contents

📚 General info

📷 Screenshots

  • Not applicable for this project

📶 Technologies

💾 Setup

  • Open folder in an IDE such as IntelliJ.

💻 Code Examples

  • class to create a data file and write data to it. flip() method used to flip the buffer so buffer trimmed to current position then position changed to zero.
public class Main {

  public static void main(String[] args) {
    try(FileOutputStream binFile = new FileOutputStream("data.dat");
        FileChannel binChannel = binFile.getChannel()) {

      byte[] outputBytes = "Hello World".getBytes();
      ByteBuffer buffer = ByteBuffer.wrap(outputBytes);
      int numBytes = binChannel.write(buffer);
      System.out.println("numBytes written was: " + numBytes);

      ByteBuffer intBuffer = ByteBuffer.allocate(Integer.BYTES);
      intBuffer.putInt(245);
      intBuffer.flip();
      numBytes = binChannel.write(intBuffer);
      System.out.println("numBytes written was: " + numBytes);

      intBuffer.flip();
      intBuffer.putInt(-98765);
      intBuffer.flip();
      numBytes = binChannel.write(intBuffer);
      System.out.println("numBytes written was: " + numBytes);
    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}

🆒 Features

  • tba

📋 Status & To-Do List

  • Status: Working tutorial code Section 14-247 completed.
  • To-Do: Complete tutorial

👏 Inspiration

✉️ Contact

About

📋 Java code to access data in files using NIO File classes

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published