- Java/File Input Output/ByteBuffer
- A ByteBuffer is a fixed-capacity buffer that holds byte values.
- Applying Regular Expressions on the Contents of a File
- Converting Between a ByteBuffer an a Byte Array
- Convert interchangeably between a ByteBuffer and a byte array
- Create a ByteBuffer
- Create a ByteBuffer using a byte array
- Create a character ByteBuffer
- Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity.
- Create a double ByteBuffer
- Create a float ByteBuffer
- Create a long ByteBuffer
- Create an integer ByteBuffer
- Create a non-direct ByteBuffer with a 10 byte capacity
- Create a short ByteBuffer
- Determining If a ByteBuffer Is Direct
- Fast Copy File
- Get and Set char type data in a ByteBuffer
- Get and Set double type data in a ByteBuffer
- Get and Set float type data in a ByteBuffer
- Get and Set int type data in a ByteBuffer
- Get and Set long type data in a ByteBuffer
- Get and Set short type data in a ByteBuffer
- Get a substring
- Get default byte ordering
- Get remaining byte count in a ByteBuffer
- Get the ByteBuffer»s capacity
- How to get bytes from a ByteBuffer
- Put a multibyte value
- Put bytes into a ByteBuffer
- Putting Bytes into a ByteBuffer
- Reading from a Channel with a ByteBuffer
- Retrieve all bytes in the buffer
- Retrieve bytes between the position and limit
- Set the limit for ByteBuffer
- Set the position
- Set to little endian
- This convenience method sets the position to 0
- use FileChannel and ByteBuffer
- Use NIO to read a text file.
- Use the absolute get().
- Use the relative get()
- Using a ByteBuffer to Store Strings
- View buffers
- Write with ByteBuffer
- Writing and Appending a ByteBuffer to a File
- Writing to a File in Java
Java/File Input Output/ByteBuffer
A ByteBuffer is a fixed-capacity buffer that holds byte values.
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Applying Regular Expressions on the Contents of a File
import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main
public static CharSequence fromFile(String filename) throws IOException < FileInputStream fis = new FileInputStream(filename); FileChannel fc = fis.getChannel(); // Create a read-only CharBuffer on the file ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size()); CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf); return cbuf; >public static void main(String[] argv) throws Exception < // Create matcher on file Pattern pattern = Pattern.rupile("pattern"); Matcher matcher = pattern.matcher(fromFile("infile.txt")); // Find all matches while (matcher.find()) < // Get the matching string String match = matcher.group(); >>
Converting Between a ByteBuffer an a Byte Array
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < // Create a ByteBuffer from a byte array byte[] bytes = new byte[10]; ByteBuffer buf = ByteBuffer.wrap(bytes); >
Convert interchangeably between a ByteBuffer and a byte array
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < // Create a ByteBuffer from a byte array byte[] bytes = new byte[10]; ByteBuffer buffer = ByteBuffer.wrap(bytes); // Retrieve bytes between the position and limit bytes = new byte[buffer.remaining()]; buffer.get(bytes, 0, bytes.length); // Retrieve all bytes in the buffer buffer.clear(); bytes = new byte[buffer.capacity()]; buffer.get(bytes, 0, bytes.length); >
Create a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < // Create a ByteBuffer using a byte array byte[] bytes = new byte[10]; ByteBuffer buffer = ByteBuffer.wrap(bytes); // Create a non-direct ByteBuffer with a 10 byte capacity // The underlying storage is a byte array. buffer = ByteBuffer.allocate(10); // Create a memory-mapped ByteBuffer with a 10 byte capacity. buffer = ByteBuffer.allocateDirect(10); >
Create a ByteBuffer using a byte array
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Create a character ByteBuffer
import java.nio.ByteBuffer; import java.nio.CharBuffer; public class Main
public static void main(String[] argv) throws Exception
Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity.
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Create a double ByteBuffer
import java.nio.ByteBuffer; import java.nio.DoubleBuffer; public class Main
public static void main(String[] argv) throws Exception
Create a float ByteBuffer
import java.nio.ByteBuffer; import java.nio.FloatBuffer; public class Main
public static void main(String[] argv) throws Exception
Create a long ByteBuffer
import java.nio.ByteBuffer; import java.nio.LongBuffer; public class Main
public static void main(String[] argv) throws Exception
Create an integer ByteBuffer
import java.nio.ByteBuffer; import java.nio.IntBuffer; public class Main
public static void main(String[] argv) throws Exception
Create a non-direct ByteBuffer with a 10 byte capacity
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Create a short ByteBuffer
import java.nio.ByteBuffer; import java.nio.ShortBuffer; public class Main
public static void main(String[] argv) throws Exception
Determining If a ByteBuffer Is Direct
//Contents in a non-direct ByteBuffer are stored in the normal memory. //Contents in a direct ByteBuffer are stored in some I/O device. import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer bbuf = ByteBuffer.wrap(new byte[10]); boolean isDirect = bbuf.isDirect(); // false bbuf = ByteBuffer.allocate(10); isDirect = bbuf.isDirect(); // false bbuf = ByteBuffer.allocateDirect(10); isDirect = bbuf.isDirect(); // true >
Fast Copy File
import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main
static public void main(String args[]) throws Exception < FileInputStream fin = new FileInputStream("infile.txt"); FileOutputStream fout = new FileOutputStream("outfile.txt"); FileChannel inc = fin.getChannel(); FileChannel outc = fout.getChannel(); ByteBuffer bb = ByteBuffer.allocateDirect(1024); while (true) < int ret = inc.read(bb); if (ret == -1) break; bb.flip(); outc.write(bb); bb.clear(); >>
Get and Set char type data in a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); // Put values of different types buf.putChar((char) 123); // Reset position for reading buf.flip(); // Retrieve the values char c = buf.getChar(); >
Get and Set double type data in a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); // Put values of different types buf.putDouble(12.3D); // Reset position for reading buf.flip(); // Retrieve the values double d = buf.getDouble(); >
Get and Set float type data in a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); // Put values of different types buf.putFloat(12.3F); // Reset position for reading buf.flip(); // Retrieve the values float f = buf.getFloat(); >
Get and Set int type data in a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); // Put values of different types buf.putInt(123); // Reset position for reading buf.flip(); // Retrieve the values int i = buf.getInt(); >
Get and Set long type data in a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); // Put values of different types buf.putLong(123L); // Reset position for reading buf.flip(); // Retrieve the values long l = buf.getLong(); >
Get and Set short type data in a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); buf.putShort((short) 123); // Reset position for reading buf.flip(); // Retrieve the values short s = buf.getShort(); >
Get a substring
import java.nio.ByteBuffer; import java.nio.CharBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); CharBuffer cbuf = buf.asCharBuffer(); cbuf.put("a string"); int start = 2; // start is relative to cbuf"s current position int end = 5; CharSequence sub = cbuf.subSequence(start, end); // str >
Get default byte ordering
import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(10); ByteOrder order = buf.order(); // ByteOrder.BIG_ENDIAN >
Get remaining byte count in a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Get the ByteBuffer»s capacity
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocateDirect(10); int capacity = buf.capacity(); // 10 >
How to get bytes from a ByteBuffer
// Create an empty ByteBuffer with a 10 byte capacity ByteBuffer bbuf = ByteBuffer.allocate(10); // Retrieve the capacity of the ByteBuffer int capacity = bbuf.capacity(); // 10 // The position is not affected by the absolute get() method. byte b = bbuf.get(5); // position=0 // Set the position bbuf.position(5); // Use the relative get() b = bbuf.get(); // Get the new position int pos = bbuf.position(); // Get remaining byte count int rem = bbuf.remaining(); // Set the limit bbuf.limit(7); // remaining=1 // This convenience method sets the position to 0 bbuf.rewind(); // remaining=7
Put a multibyte value
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(10); buf.putShort(0, (short) 123); buf.get(0); // 0 buf.get(1); // 123 >
Put bytes into a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < // Create an empty ByteBuffer with a 10 byte capacity ByteBuffer bbuf = ByteBuffer.allocate(10); // Retrieve the capacity of the ByteBuffer int capacity = bbuf.capacity(); // 10 // The position is not affected by the absolute get() method. byte b = bbuf.get(5); // position=0 // Set the position bbuf.position(5); // Use the relative get() b = bbuf.get(); // Get the new position int pos = bbuf.position(); // 6 // Get remaining byte count int rem = bbuf.remaining(); // 4 // Set the limit bbuf.limit(7); // remaining=1 // This convenience method sets the position to 0 bbuf.rewind(); // remaining=7 >
Putting Bytes into a ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer bbuf = ByteBuffer.allocate(10); int capacity = bbuf.capacity(); // 10 System.out.println(capacity); bbuf.put((byte) 0xFF); bbuf.position(5); bbuf.put((byte) 0xFF); int pos = bbuf.position(); int rem = bbuf.remaining(); bbuf.limit(7); bbuf.rewind(); >
Reading from a Channel with a ByteBuffer
import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; public class Main
public static void main(String[] argv) throws Exception < ReadableByteChannel channel = new FileInputStream("infile").getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(10); int numRead = 0; while (numRead >= 0) < buf.rewind(); numRead = channel.read(buf); buf.rewind(); // Read bytes from ByteBuffer; see also for (int i = 0; i < numRead; i++) < byte b = buf.get(); >> >
Retrieve all bytes in the buffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Retrieve bytes between the position and limit
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Set the limit for ByteBuffer
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocateDirect(10); buf.limit(7); // remaining=1 >
Set the position
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Set to little endian
import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main
public static void main(String[] argv) throws Exception
This convenience method sets the position to 0
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocateDirect(10); buf.rewind(); // remaining=7 >
use FileChannel and ByteBuffer
import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main
static public void main(String args[]) throws Exception < FileInputStream fin = new FileInputStream("infile.txt"); FileOutputStream fout = new FileOutputStream("outfile.txt"); FileChannel inc = fin.getChannel(); FileChannel outc = fout.getChannel(); ByteBuffer bb = ByteBuffer.allocate(1024); while (true) < int ret = inc.read(bb); if (ret == -1) break; bb.flip(); outc.write(bb); bb.clear(); >>
Use NIO to read a text file.
import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ExplicitChannelRead
public static void main(String args[]) < FileInputStream fIn; FileChannel fChan; long fSize; ByteBuffer mBuf; try < fIn = new FileInputStream("test.txt"); fChan = fIn.getChannel(); fSize = fChan.size(); mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf); mBuf.rewind(); for (int i = 0; i < fSize; i++) System.out.print((char) mBuf.get()); fChan.close(); fIn.close(); >catch (IOException exc) < System.out.println(exc); System.exit(1); >>
Use the absolute get().
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocateDirect(10); byte b = buf.get(5); // position=0 >
Use the relative get()
import java.nio.ByteBuffer; public class Main
public static void main(String[] argv) throws Exception
Using a ByteBuffer to Store Strings
import java.nio.ByteBuffer; import java.nio.CharBuffer; public class Main
public static void main(String[] argv) throws Exception < ByteBuffer buf = ByteBuffer.allocate(100); CharBuffer cbuf = buf.asCharBuffer(); cbuf.put("a string"); cbuf.flip(); String s = cbuf.toString(); // a string >
View buffers
import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.FloatBuffer; public class Buffers
public static void main(String[] args) < try < float[] floats = < 6.61E-39F, 9.918385E-39F>; ByteBuffer bb = ByteBuffer.allocate(floats.length * 4); FloatBuffer fb = bb.asFloatBuffer(); fb.put(floats); CharBuffer cb = bb.asCharBuffer(); System.out.println(cb.toString()); > catch (Exception e) < System.out.println(e.getMessage()); e.printStackTrace(); >>
Write with ByteBuffer
import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main
public static void main(String[] args) throws Exception < String fromFileName = "from.txt"; String toFileName = "to.txt"; FileChannel in = new FileInputStream(fromFileName).getChannel(); FileChannel out = new FileOutputStream(toFileName).getChannel(); ByteBuffer buff = ByteBuffer.allocateDirect(32 * 1024); while (in.read(buff) >0) < buff.flip(); out.write(buff); buff.clear(); >in.close(); out.close(); >
Writing and Appending a ByteBuffer to a File
import java.io.File; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main
public static void main(String[] argv) throws Exception
Writing to a File in Java
When working on an enterprise application, sometimes it is needed to write the text or binary data into files in Java e.g. writing user-generated reports into the filesystem.
Though there are multiple ways of writing the files in Java, let’s quickly go through a few of them for quick reference when it is needed.
1. Using Files.writeString() and Files.write()
With the method writeString() introduced in Java 11, we can write a String into a file using a single-line statement.
- As the name suggests, writeString() method is used to write the character data into files.
- All characters are written as they are, including the line separators. No extra characters are added.
- By default, UTF-8 character encoding is used.
- It throws IOException if an I/O error occurs writing to or creating the file, or the text cannot be encoded using the specified charset.
Path filePath = Path.of("demo.txt"); String content = "hello world !!"; Files.writeString(filePath, content);
Files class another method write() since Java 7 and it works similar to writeString(). The write() method can be used to write the raw data in bytes or to write the strings in lines.
Path filePath = Path.of("demo.txt"); String content = "hello world !!"; //Write bytes Files.write(filePath, content.getBytes()); //Write lines List lines = Arrays.asList("a", "b", "c"); Files.write(filePath, lines, StandardCharsets.UTF_8);
2. Fast Writing FileChannel and ByteBuffer
FileChannel can be used for reading, writing, mapping, and manipulating a file. If we are writing the large files, FileChannel can be faster than standard IO.
File channels are safe for use by multiple concurrent threads.
Path fileName = Path.of("demo.txt"); String content = "hello world !!"; try ( RandomAccessFile stream = new RandomAccessFile(filePath.toFile(),"rw"); FileChannel channel = stream.getChannel();)
BufferedWriter the simplest way to write the content to a file. It writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriter and OutputStreamWriter .
As it buffers before writing, so it results in fewer IO operations, so it improves the performance.
Path filePath = Path.of("demo.txt"); String content = "hello world !!"; try (BufferedWriter writer = new BufferedWriter( new FileWriter(filePath.toFile())))
4. Using FileWriter or PrintWriter
FileWriter the most clean way to write files. The syntax is self-explanatory and easy to read and understand. FileWriter writes directly into the file (less performance) and should be used only when the number of writes is less.
Path filePath = Path.of("demo.txt"); String content = "hello world !!"; try(FileWriter fileWriter = new FileWriter(filePath.toFile()))
Use PrintWriter to write formatted text to a file. This class implements all of the print methods found in PrintStream , so you can use all formats which you use with System.out.println() statements.
Path filePath = Path.of("demo.txt"); String content = "hello world !!"; try(FileWriter fileWriter = new FileWriter(filePath.toFile()); PrintWriter printWriter = new PrintWriter(fileWriter);)
Use FileOutputStream to write binary data to a file. FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter .
Path filePath = Path.of("demo.txt"); String content = "hello world !!"; try(FileOutputStream outputStream = new FileOutputStream(filePath.toFile()))
DataOutputStream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.
Path filePath = Path.of("demo.txt"); String content = "hello world !!"; try ( FileOutputStream outputStream = new FileOutputStream(filePath.toFile()); DataOutputStream dataOutStream = new DataOutputStream(new BufferedOutputStream(outputStream));)
- If we try to write to a file that doesn’t exist, the file will be created first and no exception will be thrown (except using Path method).
- Always close the output stream after writing the file content to release all resources. It will also help in not corrupting the file.
- Use PrintWriter is used to write formatted text.
- Use FileOutputStream to write binary data.
- Use DataOutputStream to write primitive data types.
- Use FileChannel to write larger files. It is the preferred way of writing filesin Java 8 as well.