Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Jan 7, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 \n on Unix and \r\n 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, \n, \r, \r\n, 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) { 
public boolean accept(File file, String string) 
String path = new String(file.toString() + "?" + string).replace('?', 
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++) 
String path = new String(string + "?" + list[i]).replace('?', 
if (new File(path).isDirectory()) { 
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"); 
BufferedReader bufferedreader = new BufferedReader(new 
BufferedWriter bufferedwriter = new BufferedWriter(new 
String line; 
while ((line = bufferedreader.readLine()) != null) 
if (file_txt.delete()) 
catch (FileNotFoundException fe) 
catch (IOException e) 
Behrouz Fallahi
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date