Get Rid Of Unreadable Characters

Line separators are dependent on the operating system. This may cause some annoyance when opening files created on another operating system. For example, if you are using Microsoft Windows, and you open a text file created on UNIX, you may see some hardly readable text that goes on for a few lines, and is cluttered with black squares as line separators.

The platform-independent nature of Java has given us the means to write code to straighten out the inconsistent End-Of-Line characters from one platform to another. If you look at the source code for java.io.BufferedWriter , you’ll see the private instance variable “private String lineSeparator”, the constructor of BufferedWriter instantiates this variable to a system-defined line separator. For example, you’ll have
on Unix and
on Microsoft Windows.

Method “public void newLine() throws IOException{…}” of BufferedWriter uses this lineSeparator to write an appropriate End-Of-Line character to output stream. Now, look at the source code for java.io.BufferedReader, and search for method “public String readLine() throws IOException{…}”. You’ll see that this method treats several cases, namely,
,
,
, to determine if a line is terminated or not.

What does this have to do with us? Well, you may convert the line separator used in a file by reading its lines with the method readLine of the class BufferedReader, and write its lines back using the methods write and newLine of the class BufferedWriter. And, as easy as that, you’ll get rid of black square eyesores. Here is some code for illustration:
Note that the code deals with .txt and .java files, but you can easily modify it to deal with other file extensions.

 import java.io.*; //Instances of classes that implement java.io.FilenameFilter // are used to filter filenames. //These instances are used to filter directory listings in the list method of class File, //and by the Abstract Window Toolkit's file dialog component. public class EOLConverter implements FilenameFilter { public static void main(String[] arguments) { convertDirectory("."); } /************************/ public boolean accept(File file, String string) { String path = new String(file.toString() + "?" + string).replace('?', File.separatorChar); boolean isDir = new File(path).isDirectory(); boolean javaFile = string.endsWith(".java"); boolean txtFile = string.endsWith(".txt"); return (isDir || javaFile || txtFile); } /************************/ public static void convertDirectory(String string) { File directory = new File(string); String[] list = directory.list(new EOLConverter()); for (int i = 0; i < list.length; i++) { System.out.println(list[i]); String path = new String(string + "?" + list[i]).replace('?', File.separatorChar); if (new File(path).isDirectory()) { convertDirectory(path); } else { convertFile(path); } } } /************************/ public static void convertFile(String string) { //here, the conversion takes place automatically, //thanks to Java File file_txt = new File(string); File file_tmp = new File(string + ".tmp"); try { BufferedReader bufferedreader = new BufferedReader(new FileReader(file_txt)); BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter(file_tmp)); String line; while ((line = bufferedreader.readLine()) != null) { bufferedwriter.write(line); bufferedwriter.newLine(); } bufferedreader.close(); bufferedwriter.close(); if (file_txt.delete()) { file_tmp.renameTo(file_txt); } } catch (FileNotFoundException fe) {/*HANDLE EXCEPTION*/} catch (IOException e) {/*HANDLE EXCEPTION*/} } } 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: