How to read / write UTF8 and Non-UTF8 files in Java

In the following java code example, you will learn 4 things about file operations

  1. How to read a regular file, such as plain text from file system, as bytes array
  2. How to write byte array content to the file system
  3. How to read UTF-8 encoded files
  4. How to write file as UTF-8 encoded

package com.icodejava.blog;
/**
 * @author Kushal Paudyal
 * Created on 08/15/2015
 * 
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class FileUtilities {

	private FileUtilities() {

	}

	public static void main(String args[]) throws FileNotFoundException, IOException {

		/**
		 * Non UTF8 format file read/write example
		 * Reads content from a file and writes to another file.
		 */
		
		byte[] fileContent = loadFile("/Users/kushal/Desktop/someFile.txt");
		writeFile("/Users/kushal/Desktop/someOutputFile", fileContent);
		
		/**
		 * UTF8 format file read/write example
		 * Reads content from a file and writes to another file.
		 */
		String content = readUTF8File("/Users/paudyak/Desktop/np_name.txt");
		writeUTF8File("/Users/paudyak/Desktop/np_name_out.txt", content);

	}

	/**
	 * This method shows how to read a file as bytes [] 
	 */
	public static byte[] loadFile(String fileName) throws FileNotFoundException, IOException {

		File file = new File(fileName);
		int length = (int) file.length();

		BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
		byte[] bytes = new byte[length];
		reader.read(bytes, 0, length);
		reader.close();

		return bytes;

	}

	/**
	 * This method shows how to write byte [] into a file.
	 */
	public static void writeFile(String fileName, byte[] content) throws IOException {

		File file = new File(fileName);

		BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));

		writer.write(content);

		writer.flush();

		writer.close();

	}

	/**
	 * This method shows how you can read a UTF8 file in Java. 
	 */
	public static String readUTF8File(String fileName) {

		String content = "";

		try {

			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF8"));

			String line;
			while ((line = in.readLine()) != null) {

				System.out.println(line);
				content += line;
				

			}

			in.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}

	/**
	 * This method show how to write UTF8 content to the file system.
	 */
	public static void writeUTF8File(String fileName, String content) {

		try {

			File fileDir = new File(fileName);

			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir),"UTF8"));

			out.append(content);
			out.flush();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

  1. How to read / write UTF8 and Non-UTF8 files in Java
  2. How To Print A Text File In Java
  3. How to read a tab separated or tab delimited file in Java program and print the content to console
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  6. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  7. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  8. Reading / Writing File in Java and String Manipulation
  9. How To Read DOC file Using Java and Apache POI
  10. Writing To Excel File Using Apache POI
  11. Reading Excel File Using Java And Apache POI
  12. Calculating Folder Size In Java
  13. How To Rename A File In Java
  14. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

One Response to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

Leave a Reply

Your email address will not be published. Required fields are marked *