Print string in file java

Java 11 – Files writeString() API

Learn to write a string into file in Java using Files.writeString(path, string, options) method. This API has been introduced in Java 11.

1. Files writeString() methods

java.nio.file.Files class has two overloaded static methods to write content to file.

public static Path writeString​(Path path, CharSequence csq, OpenOption. options) throws IOException public static Path writeString​(Path path, CharSequence csq, Charset cs, OpenOption. options) throws IOException
  • First method writes all content to a file, using the UTF-8 charset.
  • First method is equivalent to writeString(path, string, StandardCharsets.UTF_8, options) .
  • Second method does the same with with only using the specified charset.
  • options specifies how the file is opened.

2. Files writeString() example

Java program to write String into a file using Files.writeString() method.

import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.Files; import java.io.IOException; import java.nio.file.StandardOpenOption; public class Main < public static void main(String[] args) < Path filePath = Paths.get("C:/", "temp", "test.txt"); try < //Write content to file Files.writeString(filePath, "Hello World !!", StandardOpenOption.APPEND); //Verify file content String content = Files.readString(filePath); System.out.println(content); >catch (IOException e) < e.printStackTrace(); >> >

Where the file c:/temp/test.txt is empty initially.

Drop me your questions in comments section.

Источник

Class PrintStream

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException ; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method of the underlying output stream is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ( ‘\n’ ) is written.

All characters printed by a PrintStream are converted into bytes using the given encoding or charset, or the platform’s default character encoding if not specified. The PrintWriter class should be used in situations that require writing characters rather than bytes.

This class always replaces malformed and unmappable character sequences with the charset’s default replacement string. The CharsetEncoder class should be used when more control over the encoding process is required.

Field Summary

Fields declared in class java.io.FilterOutputStream

Constructor Summary

Creates a new print stream, without automatic line flushing, with the specified file name and charset.

Creates a new print stream, without automatic line flushing, with the specified file name and charset.

Источник

Java: Save/Write String Into a File

Saving a String into files can be done in a few ways using Java. In this article, we’ll show some common methods for writing a String into a file.

Here’s a list of all the classes and methods we’ll go over:

Files.writeString()

Since Java 11, the Files class contains a useful utility method Files.writeString() . This method comes in two variants. The most basic form requires a Path of the file to write to and the textual contents. The other variant also accepts an optional CharSet :

Path path = Paths.get("output.txt"); String contents = "Hello"; try < Files.writeString(path, contents, StandardCharsets.UTF_8); >catch (IOException ex) < // Handle exception > 

There is little room for flexibility here, but it works great if you need to write something down into a file quickly.

Files.write()

A String, like other objects, can be converted into a byte[] . The Files.write() method deals with bytes:

Path path = Paths.get("output.txt"); String someString = "Hello World"; byte[] bytes = someString.getBytes(); try < Files.write(path, bytes); >catch (IOException ex) < // Handle exception > 

There is no need to close any resources since we haven’t opened any resources ourselves.

FileWriter

FileWriter is one of the simplest ways to write some textual contents into a file. We’ll create a File instance and pass it into the FileWriter constructor to «bridge» them.

Then, we simply use the FileWriter instance to write to it:

File output = new File("output.txt"); FileWriter writer = new FileWriter(output); writer.write("This text was written with a FileWriter"); writer.flush(); writer.close(); 

After using the writer, it’s important to flush and close the resources. Alternatively, you can do this with the try-with-resources syntax:

try(FileWriter writer = new FileWriter("output.txt")) < writer.write("This text was written with a FileWriter"); > catch(IOException e)< // Handle the exception > 

BufferedWriter

BufferedWriter is a wrapper object that is used around objects of type Writer . If we have an existing Writer such as FileWriter , we can wrap it inside a BuffereWriter .

BufferedWriter is best used when there are multiple write() operations for a file. In this case, those multiple writes are temporarily stored into an internal buffer and written into a file only when there is enough content. This avoids having to store each new chunk of text into a file and instead provides an appropriate buffer for temporary storage.

Using a BufferedWriter is much more efficient than FileWriter for multiple writes, but it doesn’t help with a single write.

In fact, using BufferedWriter for a single write would cause unnecessary overhead. For such simple cases, FileWriter is a much better option.

Let’s create a BufferedWriter :

BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt")); 

BufferedWriter and FileWriter both extend Writer so they have the same methods:

try(BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) < writer.write("Written with BufferedWriter); > catch(IOException e) < // Handle the exception >

PrintWriter

PrintWriter lets us format the text before writing it down. It contains methods we’re used to, such as printf() , println() , etc. Let’s create a PrintWriter :

File output = new File("output.txt"); PrintWriter writer = new PrintWriter(output); 

A better way to work with a PrintWriter is with the try-with-resources syntax:

try(PrintWriter writer = new PrintWriter(new FileWriter("output.txt"))) < // Write using the PrintWriter instance > catch < // Handle Exception > 

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Once we have a PrintWriter instance, let’s explore some of the methods it provides.

PrintWriter with append()

PrintWriter , like the StringBuilder provides the append() method which allows us to append contents to the end of an existing file.

Let’s appent() some text to an empty writer :

writer.append("Welcome to my fruit store! From me, you can buy:\n"); writer.append("Apples"); writer.append("\n"); writer.append("Oranges"); writer.append("\n"); writer.append("Bananas"); 

The append() method returns the PrintWriter object it was called upon. This makes it possible to chain append() methods and organize them more neatly:

writer.append("Welcome to my fruit store! From me, you can buy:\n"); writer.append("Apples\n").append("Oranges\n").append("Bananas\n"); 

PrintWriter with print()

PrintWriter contains methods for formatted printing. These include print() , printf() , and println() :

writer.print("Welcome to my fruit store %f", 2.0); writer.printf("From me, you can buy %s and %s.", "apples", "oranges"); 

PrintWriter with write()

With write() , we can write many different types of textual contents into the stream. Examples include char arrays, Strings, and integers:

char[] hello = 'H', 'e', 'l', 'l', 'o', '!', '\n'>; writer.write(hello); writer.write("Welcome to my fruit store\n"); writer.write("From me, you can buy apples and oranges"); 

The write() method only accepts content without formatting options so it’s similar to print() , but can’t format Strings.

To finalize each PrintWriter «session» of either appending, printing, or writing, it’s important to flush and close the stream:

The flush() method «flushes» the contents into the file and close() permanently closes the stream.

Note: If you use the try-with-resources syntax, it’ll flush and close the stream automatically.

Conclusion

In this article, we’ve shown some common methods for writing strings into files. There are many options because there are many possible use-cases.

We’ve covered the Files.writeString() , Files.write() methods, as well as the FileWriter , BufferedWriter and PrintWriter classes.

Источник

Читайте также:  Отличие атрибута от свойства html
Оцените статью