Replacing non-ASCII characters using Java Regular Expressions

The following java code shows how we can replace non-ASCII characters from a file using regular expressions.

package com.kushal.utils;

/**
 * ReplaceNonASCIICharacters.java
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 *
 * This class reads a file with non ASCII Characters in it.
 * Replaces the non ASCII Characters using regular expression.
 * Saves the content with non-ASCII Characters removed to a new file.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class ReplaceNonAsciiCharacters {

	public static void main(String args[]) {

		/**
		 * This is the input file name with some non-ASCII characters in the
		 * content of the file.
		 */
		String fileName = "C:/Temp/WithNonASCIICharacters.txt";
		/**
		 * This is the location of the output file - the content of this file
		 * will be the input file content minus the non-ASCII characters.
		 */
		String outputFileName = "C:/Temp/WithNonASCIICharactersRemoved.txt";

		try {

			/**
			 * Create a reader to read the input file
			 */
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			String line = "";

			String formattedStr = "";
			int count = 0;
			/**
			 * Iterate through each line of content
			 * remove any non-ASCII characters with blank using
			 * regular expression.
			 *
			 * Append the new line character properly.
			 */
			while ((line = in.readLine()) != null) {
				if (count == 0)
					formattedStr += line.replaceAll("[^\p{ASCII}]", "");
				else
					formattedStr += "n" + line.replaceAll("[^\p{ASCII}]", "");

				count++;
			}

			/**
			 * Write the content to the output file using BufferedWriter object.
			 */
			BufferedWriter out = new BufferedWriter(new FileWriter(
					outputFileName));
			out.write(formattedStr);

			/**
			 * Once done, flush the writer and close it.
			 */
			out.flush();
			out.close();

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

	}

	/*
	 * SANJAAL CORPS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
	 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
	 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
	 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SANJAAL CORPS SHALL NOT BE
	 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
	 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
	 *
	 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
	 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
	 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
	 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
	 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
	 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
	 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SANJAAL CORPS
	 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
	 * HIGH RISK ACTIVITIES.
	 */
}

[My Video Promotion]
  1. How to extract plain Text from HTML Website easily in Java
  2. Regular Expression to validate the US zip codes
  3. Java Regular Expressions – Single World Replacement Example /Tutorial
  4. How To Find Wild Cards In A String Using Regular Expressions?
  5. How To Find What Java Version You Are Using?
  6. Computing the total, free and usable disk space easily using JDK 1.6
  7. Creating java.util.date and java.util.sql objects from Calendar object
  8. Bar Code Generation In Java Using iText API
  9. Simple Tag Generator In Java For Music Blogs
  10. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  11. Calling URL Browser From Java Application
  12. Java Reflection Tutorial – List Methods Of A Class
  13. Cleaning Punctuation And White Spaces From A String
  14. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  15. Prime Number Finder In Java
  16. Utility Class For Getting SQL Connection and Closing It
  17. How to find if a string is contained in another string using regex in Java?
  18. Programmatically Validating US and Canada Phone Numbers Using Regular Expressions In Java
  19. Java Design Pattern – Singleton Pattern
  20. Reading / Writing File in Java and String Manipulation
  21. How to capture a screenshot using Java and save it to a file?
  22. Calculating The Difference Between Two Dates In Terms of Months
  23. Getting The Name Of Currently Logged In User In The System
  24. Java Regular Expressions (Theory, Classes and Syntax)
  25. How To Set And Get System Clipboard Contents In Java
  26. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  27. Generating Nepali Unicode Sequence For Characters
  28. How to validate PO Box Address Variations With Java Regular Expressions?
  29. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  30. Replacing non-ASCII characters using Java Regular Expressions
  31. US 5 and 9 Digits Zip Code Validation In Java Using Regular Expressions
  32. Calculating Folder Size In Java
Tagged , , , , , , , , . Bookmark the permalink.

3 Responses to Calculating Folder Size In Java

  1. Pingback: Sanjaal.com » Latest Updates

  2. Jamie says:

    This approach uses less memory:

    public static class SizeCounter implements FileFilter
    {
    private long total = 0;
    public SizeCounter(){};
    public boolean accept(File pathname) {
    if ( pathname.isFile()) {
    total+=pathname.length();
    } else {
    pathname.listFiles(this);
    }
    return false;
    }
    public long getTotal()
    {
    return total;
    }
    }

    private static long getFileOrDirectorySize(File file) {
    SizeCounter counter = new SizeCounter();
    file.listFiles(counter);
    return counter.getTotal();
    }

  3. kushalzone says:

    Thank you Jamie for your optimized solution.

Leave a Reply to Jamie Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.