Appending Characters In An Entire Column Of A Text File-Java

I just fell in a situation where I was desired to put tabs in an entire column of a text file. MS-Word, MS-Excel were useless for this purpose. So finally I built a small java program that can handle around 1 GB of text file if your RAM allows. Although I didn’t tested it much yet, but basically it works.

In this program I am appending tabs in an entire column, my program asks user for the file name and the column number at which he wants to insert tabs. It takes some time depending on the file size. It works from the command line only, no GUI is available. If you want to insert any other character instead of tabs then you can replace ‘\t’ in file.writeByte(‘\t’) method with any character of your choice.

CAUTION: As this program is not tested for all possible conditions so it can result in unexpected behavior or can corrupt your data if anything goes wrong. To preserve your data, Do make a backup copy before using this program on your text file.

I was bound to use RandomAccessFile Class for accessing the file, as this class provides some specialized methods with which I can jump to specific positions like seek() . I was facing problem with detecting the end of lines so I used RandomAccessFile.readLine() method to move the file pointer to the end of the line. I am trying to do the same with RandomAccessFile.readByte() to detect end of line, but yet not got success, as soon as I get it working in that manner, I’ll upload that program as well.

The program is below, any questions and suggestions are invited in comments 🙂

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
/**
*
* @author Abhishek
*/
public class GetMeTabs {
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter File Name Along With Its Extension(like abc.txt) : ");
String fName=br.readLine();
System.out.print("Enter Column Number At Which You Want To Insert Tabs : ");
int col=Integer.parseInt(br.readLine());
br.close();
System.out.println("\nInsertion of tabs is in progress. Please wait...\n\n");
RandomAccessFile file=new RandomAccessFile(fName,"rwd");
int fileLength=(int) file.length();
long lineBeg=0;
boolean finish=false;
String s;
while(finish!=true){
file.seek(col+lineBeg); //Position pointer where you have to start copying to make room for new char
int rem=(int) (fileLength-(col+lineBeg));
byte[] takeData=new byte[rem]; //make byte array of size required into which data is to be copied
file.readFully(takeData); //read rest of file from position where we fixed pointer above
file.seek(lineBeg+col); //Place pointer back again to position where new char is to be inserted
file.writeByte('\t'); //Insert specified char
file.write(takeData); //write the copied data back again after inserting char
file.seek(lineBeg); //Place pointer back on line begining for function below to skip that complete line
fileLength=(int) file.length(); //Calcualte again the file size after adding char
if((file.readLine())!=null){ //Move the pointer to the end of line in which we inserted char just now
long ofFirstLine=file.getFilePointer();
if(fileLength==ofFirstLine) //Break the loop when EOF is encountered
break;
if((s=file.readLine())!=null){ //Moves pointer to the end of present line to check the contents of next line
if(s.length()<1)
lineBeg=ofFirstLine+2; //If next line contains nothing, it is used to skip that line
else
lineBeg=ofFirstLine;
}
else
lineBeg=ofFirstLine;
}
}
System.out.println("WE ARE DONE...");
file.close();
}
}