- 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**
- Java.nio used to define buffers - containers for data.
- Java.nio FileSystem class provides an interface to a file system.
- Java.nio IntBuffer class extends Buffer, an int buffer
- Java.nio flip() to flip buffer. Use after read and before write operations
- Java Pipe class 2 channels that implement a unidirectional pipe, a writable sink channel and a readable source channel.
- Java lambda expression a single method class - to take in parameters and return a value.
- Not applicable for this project
- Open folder in an IDE such as IntelliJ.
- 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();
}
}
}
- tba
- Status: Working tutorial code Section 14-247 completed.
- To-Do: Complete tutorial
- Repo created by ABateman, email: gomezbateman@yahoo.com