How to Create a File with a Specific charset in Java ?6 Jan 2025 | 3 min read In Java, in order to create a file with a given charset, we must supply the character encoding when writing text to a file. Classes to produce Java files with a certain character set: The OutputStreamWriter class and a FileOutputStream allow us to create Java files with a certain charset. Data can be stored in a file using the FileOutputStream OutputSream class. Choosing the charset to use when writing to a file is made possible by the OutputStreamWriter class. The article will teach us how to use a specified charset when creating a file in Java. Example 1:There are three parameters required for the writeToFileWithCharset function. Next, the content is written into the file with the provided charset using a FileOutputStream and an OutputStreamWriter inside the writeToFileWithCharset method. There are two categories in the primary method. The first one uses the UTF-8 character set, whereas the second one uses the ISO-8859-1 character set. The software controls file operations inside a try-with-resources block to guarantee appropriate resource allocation, and it outputs a message after a file is successfully created. Implementation:FileName: CreateCharsetFile.java Output: The file is created with the charset: UTF-8 The file is created with the charset: ISO-8859-1 Example2:Using the Windows-1252 character set, the provided Java program creates a file called "HelloWorld.txt" and inserts the given string into it. Using the file name, content, and character set as inputs, the FileWithCharset method manages the file creation and writing operation. To write the content to the file with the given charset, it makes use of an OutputStreamWriter and a FileOutputStream. An IOException is caught and displayed if it happens during this process. The main function creates a file, sets its name and contents, and then uses the Windows-1252 charset to write a file by calling FileWithCharset. Implementation:FileName: UsingWindows1252.java Output: The file is created with the charset: windows-1252 |
Fail-fast and Fail-safe are the iterators or collections in Java. Java SE specification doesn't use the Fail-safe term. We use the Fail-safe to segregate between the Non-Fail-fast and Fail-fast iterators. The Fail-Fast system terminates the operation as-fast-as-possible that are exposing failures and stop the entire operation....
6 min read
Java provides various useful built-in collection libraries. But sometimes we required special type of collections that are not in-built in Java's standard library. One of this collection is the Multimap. In this section, we will learn what is multimap and how to implement multimap in Java,...
5 min read
The java.nio.DoubleBuffer contains hasArray() function. The DoubleBuffer class is utilized to verify if the provided buffer is supported by a float array that may be accessed. If a backing array for this buffer is reachable, it returns true; otherwise, it returns false. The array() and arrayOffset()...
3 min read
In this section, we will create Java programs to find the sum of all the prime numbers in a given range. Before moving ahead in this section, let's see the important facts about prime numbers. A prime number is a number that is greater than 1 and...
4 min read
The java.time.format.DecimalStyle class contains the getAvailableLocales() method. The Java DecimalStyle class is used to retrieve a list of all the locales that are compatible with this DecimalStyle. A collection of the accessible locales is returned by this method. Syntax: public static Set<Locales> getAvailableLocales() Parameter: No parameter...
3 min read
Suppose we have a positive integer p, and we want the operations to be carried out on an array of integers nums, which contains the binary digits 1 through 2^p - 1. After performing any number of particular operations on the array elements, the objective is...
5 min read
Static objects play a critical function in the world of Java programming. They provide a method to share data and functionality throughout a couple of instances of a class. In this context, we are able to discover the idea of static objects in Java, talk their...
4 min read
One array containing non-negative numbers is given to us. Also, a number K is given. Our task is to count the number of pairs from the given array such that the OR operation between the elements in the pair gives a value greater than K. Example 1: Input int...
6 min read
Sudoku is a popular puzzle game that involves filling a 9x9 grid with numbers so that each row, each column, and each 3x3 sub-grid contain all the numbers from 1 to 9. Solving Sudoku programmatically can be challenging, but multithreading can significantly enhance the performance...
14 min read
OOPS MCQ 1) Which of the following language was developed as the first purely object programming language? SmallTalk C++ Kotlin Java Show Answer Workspace Answer: a. SmallTalk Explanation: This programming language was invented as the first pure OOPS (object-oriented) language. This language was designed by Alan Kay in the early 1970s. 2) Who developed object-oriented programming? Adele...
13 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India