Share on Google+Share on Google+

Java file encoding

Advertisement
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());
	}
}

Advertisements

Posted on: June 17, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.