Java file encoding


 

Java file encoding

In this section, you will learn how to get the file encoding.

In this section, you will learn how to get the file encoding.

Java file encoding

In this section, you will learn how to get the file encoding.

Description of code:

An encoding is a mapping between a byte value and its representation. It describes how characters are represented as bytes in files. In the given example, we have used InputStreamReader class that wraps the input stream and called its method getEncoding() which returns the desired encoding of the file.

Here is the code:

import java.io.*;

public class FileEncoding {
	public static void main(String[] args) throws Exception {
		File in = new File("C:/file.txt");
		InputStreamReader r = new InputStreamReader(new FileInputStream(in));
		System.out.println(r.getEncoding());
	}
}

Ads