0 0
WP Ad Inserter plugin
Read Time:5 Minute, 24 Second

Table of Contents

We’ll look at many methods for writing to a file in Java. These include BufferedWriter, PrintWriter, FileOutputStream, DataOutputStream, RandomAccessFile, FileChannel, and the Java 7 Files utility class used in this example. We’ll also discuss locking the file while writing and several other aspects of writing to file.

Write using PrintWriter

The example below looks at how to use PrintWriter to save formatted text to a file:

@Test
public void writeWithPrintWriter() throws IOException { FileWriter fWriter = new FileWriter(fileName); PrintWriter pWriter = new PrintWriter(fWriter); pWriter.print("Some String"); pWriter.printf("The grocery name is %s and its price is %d $", "Tomatoes", 250); pWriter.close();
}

With the printf method, we’re publishing a raw String to a file and some prepared text. We can use FileWriter, BufferedWriter, or even System.out to generate the writer.

How to Write with FileChannel

FileChannel can be faster than regular IO when dealing with huge files. Using FileChannel, the following code writes String to a file:

@Test
public void writeUsingFileChannel() throws IOException { RandomAccessFile raStream = new RandomAccessFile(fileName, "rw"); FileChannel fileChannel = raStream.getChannel(); String strVal = "Codeunderscored"; byte[] stringBytes = strVal.getBytes(); ByteBuffer byteBuffer = ByteBuffer.allocate(stringBytes .length); byteBuffer.put(stringBytes); byteBuffer.flip(); fileChannel.write(byteBuffer); raStream.close(); fileChannel.close(); // verification RandomAccessFile raFile = new RandomAccessFile(fileName, "r"); assertEquals(strVal, raFile.readLine()); raFile.close();
}

How to Write with FileOutputStream

Now let’s look at how we can send binary data to a file using FileOutputStream. The following code transforms a String to bytes and uses FileOutputStream to write the bytes to a file:

SUGGESTED READ

@Test
public void writeUsingFileOutputStream() throws IOException { String strVar = "Codeunderscored"; FileOutputStream outputStream = new FileOutputStream(fileName); byte[] strBytes = strVar.getBytes(); outputStream.write(strBytes); outputStream.close();
}

How to Write with DataOutputStream

Next, let’s look at how to write a String to a file using DataOutputStream.

@Test
public void writeUsingDataOutputStream() throws IOException { String value = "Codeundersocred"; FileOutputStream foStream = new FileOutputStream(fileName); DataOutputStream doStream = new DataOutputStream(new BufferedOutputStream(foStream)); doStream.writeUTF(value); doStream.close(); // results verification String result; FileInputStream fiStream = new FileInputStream(fileName); DataInputStream diStream = new DataInputStream(fiStream); result = diStream.readUTF(); diStream.close(); assertEquals(value, result);
}

How to Write with BufferedWriter

Let’s start easy by writing a String to a new file with BufferedWriter:

public void WriteUsingBufferedWritter() throws IOException { String strVar = "Codeunderscored"; BufferedWriter bfWriter = new BufferedWriter(new FileWriter(fileName)); bfWriter.write(strVar); bfWriter.close();
}

Subsequently, we can append a String to the existing file as follows:

@Test
public void appendStringUsingBufferedWritter() throws IOException { String strVar = "underscored"; BufferedWriter brWriter = new BufferedWriter(new FileWriter(fileName, true)); brWriter.append(' '); brWriter.append(strVar ); writer.close();
}

How to Write to a Temporary File

Let’s try writing to a temporary file now. The code below makes a temporary file and writes a String to it:

@Test
public void writeToTemporaryFile() throws IOException { String strWrite = "Codeunderscored"; File temporaryFile = File.createTempFile("trial", ".tmp"); FileWriter fWriter = new FileWriter(temporaryFile); fWriter.write(strWrite); fWriter.close(); BufferedReader bReader = new BufferedReader(new FileReader(temporaryFile)); assertEquals(strWrite, bReader.readLine()); bReader.close();
}

As we can see, the intriguing and unique part is the generation of the temporary file. After that, we write to the file the same as before.

SUGGESTED READ

How to Write with RandomAccessFile

Let’s look at writing and editing within an existing file rather than creating a new one or adding to one that already exists. , we require unrestricted access. RandomAccessFile allows us to write to a specific location in a file based on bytes offset from the file’s beginning. This code creates an integer value with an offset from the file’s start:

private void writeToPosition(String fname, int data, long pos) throws IOException { RandomAccessFile raFile = new RandomAccessFile(fname, "rw"); raFile.seek(pos); raFile.writeInt(data); raFile.close();
}

This method is used to read the int stored at a given location:

private int readFromPosition(String fname, long pos) throws IOException { int result = 0; RandomAccessFile raFile = new RandomAccessFile(fname, "r"); raFile.seek(pos); resultVals = reader.readInt(); raFile.close(); return resultVals;
}

Let’s write an integer, change it, and then read it back to see how well our functions work:

@Test
public void writingToSpecificFilePosition() throws IOException { int yearData = 2022; int countData = 4000; writeToPosition(fileName, yearData, 10); assertEquals(yearData, readFromPosition(fileName, 10)); writeToPosition(fileName2, countData, 10); assertEquals(countData, readFromPosition(fileName, 10));
}

How to Write with Files Class

A new utility class in Java 7, Files, gives interfacing with the filesystem. We may create, move, copy, and remove files and directories using the Files class. It can also read and write data to a file:

@Test
public void writeWithFileClass() throws IOException { String strVal = "Codeunderscored"; Path fetchPath = Paths.get(fileName); byte[] strBytes = strVal.getBytes(); Files.write(fetchPath, strBytes); String readString = Files.readAllLines(fetchPath).get(0); assertEquals(strVal, readString);
}

How to Lock File Before Writing

Finally, when writing to a file, we must occasionally double-check that no one else is doing so simultaneously. Essentially, we need to lock the file while it is being written. Let’s utilize FileChannel to see if we can lock the file before writing to it:

SUGGESTED READ

@Test
public void LockFileBeforeWriting() throws IOException { RandomAccessFile raFile = new RandomAccessFile(fileName, "rw"); FileChannel fChannel = raFile.getChannel(); FileLock fileLock = null; try { lock = fChannel.tryLock(); } catch (final OverlappingFileLockException e) { raFile.close(); fChannel.close(); } stream.writeChars("Test drive lock"); fileLock.release(); raFile.close(); fChannel.close();
}

An OverlappingFileLockException will be triggered if the file is already locked when we try to acquire the lock.

Conclusion

This article has demonstrated how to write data to a file in Java using various methods. However, the following are the key takeaways from us.

A FileNotFoundException will be issued if we try to read from a file that does not exist. In addition, if we try to write to an empty file, the file is created first, and no exception is issued. Because the stream is not closed implicitly, it is critical to end it after each use to relinquish associated resources. Before abandoning the resources in the output stream, the close() function calls flush(), which forces any buffered bytes to be sent to the stream.

Although several of these classes’ APIs allow for more, this is an excellent place to start. PrintWriter is used to write formatted text, FileOutputStream is used to write binary data, while the DataOutputStream is used to write basic data types. In addition, the RandomAccessFile is used to write to a specific point, and FileChannel is used to write quicker in larger files.

Source: https://www.codeunderscored.com/8-ways-to-write-to-file-in-java/

Model Formsets in Django Previous post Model Formsets in Django
Next post ListView in Django

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.