Hi
I am writing a simple java program in which i am facing some difficulty while writing the text in the file.
Aim of the program is to add some debug message in the empty catch block.

I am able to add the message but the text in the file are overwriting. I used RandomAccessFile and when i find the position i used writeBytes. But it overwrites the text in files (below the catch block..3-4 lines).

Can someone suggest how can i add text without overwriting using RandomAccessFile .

Thanks & Regards
Ronak

If its possible to do so then the Javadoc for RandomAccessFile will tell you how.

edit: It looks like it should do it by default from what I just read. Post your code in code tags.

Is it really necessary to use a RandomAccessFile? Using a FileWriter in append mode should do the trick.

Thanks for your reply..here is my code..
I am writing that part which is writing the text in the file.becuase the whole code is big.

Note:lineNum denotes the position where it should write.

if (flag==0)
{
RandomAccessFile writeObj = new RandomAccessFile(args[0]+"/"+arr[i],"rw"); //Object to write in the file
for(int j=0;j<lineNum;j++)
{
writeObj.readLine();				
}
writeObj.writeBytes("Log.errorMessage(\"Exception in "+ arr[i]+"\");}");	  //writing in the file
}

Thanks again

Since you are writing out textual content, you should use a Writer instead of dealing with raw bytes and the like. What exactly is your requirement? If you don't need to edit the file at a random location, RandomAccessFile isn't even required. If the text is overwriting the original text, it means you are not properly aligning your file pointer.

Look into the methods getFilePointer() for saving the previous write location and seek() to move to that location. Without a working compilable code, helping you out will be difficult.

Thanks,
Actually I need to add debug message in the empty catch block from the list of the files present in the particular directory.
so i have to add the message at any random location according to the condition.
Can i use FileWriter obj ?How can i add message in the middle of the file using FileWriter obj?
Here is my whole code.I am passing directory name as a comman line argument.As of now i have hard coded for the Log message in the condition.Please copy this code in a new file to get the good visibility.Program is running.You can run and place some java files with empty catch blocks in the directory.Thanks...
Thanks

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;


public class FinalCode {

	public static void main(String[] args) throws IOException 							
	//********************************************************************************************
	//Pass the directory name as the command line argument which contains list of the java files 
	//********************************************************************************************
	{					
				RandomAccessFile raf = null;											 //To open file in read write mode
				
				int i=0;				
				String[] arr=null;														 //List of files	
				File file= new File(args[0]);											 //Passing directory name
				if (file.isDirectory())
				{
					 arr=file.list(); 													 //Copying list of files from the directory					
				}				
				for(i=0;i<arr.length;i++)
				{					
					System.out.println("File Name:"+arr[i]);
					FileReader fileReader = new FileReader(args[0]+"/"+arr[i]);			 //Take the full name of the file from the input directory
					raf = new RandomAccessFile(args[0]+"/"+arr[i],"rwd");				 //open the file in the read write mode								
					String str=raf.readLine();											 //String containing each line		
					int flag=0;				
					int lineNum=0;
					while (str!=null)
					{
						if(str.indexOf("catch(")!=-1&& str.indexOf("{")!= -1 && str.indexOf("}")!= -1 && str.indexOf("Log")<0){ //for catch(Exception e){} condition									
								System.out.println("Ronak");
								FileWriter fobj =new FileWriter(args[0]+"/"+arr[i],true);
							RandomAccessFile writeObj = new RandomAccessFile(args[0]+"/"+arr[i],"rwd");
							
								for(int j=0;j<lineNum;j++)
								{
									writeObj.readLine();										
								}
								//writeObj.setLength(writeObj.length()+400);
								flag=1;
								//Writing debug message in the java file
								//writeObj.write(1234);
								writeObj.writeBytes("catch(Exception e){Log.errorMessage(\"Exception in "+ arr[i]+"\");}");	
								str=raf.readLine();
								lineNum++;
								continue;							
						}
						
						if (str.indexOf("catch")!=-1){															//for catch(Exception e){
														         												//} condition
							System.out.println(str);							
							while(str.indexOf("{")<-1)  //Check for the opening brace
							{
								str=raf.readLine();
							}									
								while (str.indexOf("}")<0) //check for the closing brace
								{								
									str=raf.readLine();
									System.out.println(str);
									lineNum++;
																								
									if (str!=null){
										if (str.indexOf("Log(")!=-1)
										{
//											Writing debug message in the java file
											System.out.println("Having non empty catch block in java file"+arr[i]);
											flag=1;
											break;
										}
									}
								}
								if (flag==0)
								{
									System.out.println("Writing error message in the java file");
									RandomAccessFile writeObj = new RandomAccessFile(args[0]+"/"+arr[i],"rwd"); //Object to write in the file
									for(int j=0;j<lineNum;j++)
									{
										writeObj.readLine();										
									}
//									Writing debug message in the java file
									//writeObj.writeChars("Log.errorMessage(\"Exception in "+ arr[i]+"\");}");
									writeObj.writeBytes("Log.errorMessage(\"Exception in "+ arr[i]+"\");}");									
								}
				
							}
							str=raf.readLine();						
							lineNum++;		//Calculating the line number				
						}																					
											
					}						
					
					
				}

	}

eww, still can't read it w/ the code tags. code = JAVA perhaps?

Thanks,
Actually I need to add debug message in the empty catch block from the list of the files present in the particular directory.
so i have to add the message at any random location according to the condition.
Can i use FileWriter obj ?How can i add message in the middle of the file using FileWriter obj?
Here is my whole code.I am passing directory name as a comman line argument.As of now i have hard coded for the Log message in the condition.Please copy this code in a new file to get the good visibility.Program is running.You can run and place some java files with empty catch blocks in the directory.Thanks...
Thanks

Help with Code Tags (Toggle Plain Text)

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;


public class FinalCode {

	public static void main(String[] args) throws IOException 							
	//********************************************************************************************
	//Pass the directory name as the command line argument which contains list of the java files 
	//********************************************************************************************
	{					
				RandomAccessFile raf = null;											 //To open file in read write mode
				
				int i=0;				
				String[] arr=null;														 //List of files	
				File file= new File(args[0]);											 //Passing directory name
				if (file.isDirectory())
				{
					 arr=file.list(); 													 //Copying list of files from the directory					
				}				
				for(i=0;i<arr.length;i++)
				{					
					System.out.println("File Name:"+arr[i]);
					FileReader fileReader = new FileReader(args[0]+"/"+arr[i]);			 //Take the full name of the file from the input directory
					raf = new RandomAccessFile(args[0]+"/"+arr[i],"rwd");				 //open the file in the read write mode								
					String str=raf.readLine();											 //String containing each line		
					int flag=0;				
					int lineNum=0;
					while (str!=null)
					{
						if(str.indexOf("catch(")!=-1&& str.indexOf("{")!= -1 && str.indexOf("}")!= -1 && str.indexOf("Log")<0){ //for catch(Exception e){} condition									
								System.out.println("Ronak");
								FileWriter fobj =new FileWriter(args[0]+"/"+arr[i],true);
							RandomAccessFile writeObj = new RandomAccessFile(args[0]+"/"+arr[i],"rwd");
							
								for(int j=0;j<lineNum;j++)
								{
									writeObj.readLine();										
								}
								//writeObj.setLength(writeObj.length()+400);
								flag=1;
								//Writing debug message in the java file
								//writeObj.write(1234);
								writeObj.writeBytes("catch(Exception e){Log.errorMessage(\"Exception in "+ arr[i]+"\");}");	
								str=raf.readLine();
								lineNum++;
								continue;							
						}
						
						if (str.indexOf("catch")!=-1){															//for catch(Exception e){
														         												//} condition
							System.out.println(str);							
							while(str.indexOf("{")<-1)  //Check for the opening brace
							{
								str=raf.readLine();
							}									
								while (str.indexOf("}")<0) //check for the closing brace
								{								
									str=raf.readLine();
									System.out.println(str);
									lineNum++;
																								
									if (str!=null){
										if (str.indexOf("Log(")!=-1)
										{
//											Writing debug message in the java file
											System.out.println("Having non empty catch block in java file"+arr[i]);
											flag=1;
											break;
										}
									}
								}
								if (flag==0)
								{
									System.out.println("Writing error message in the java file");
									RandomAccessFile writeObj = new RandomAccessFile(args[0]+"/"+arr[i],"rwd"); //Object to write in the file
									for(int j=0;j<lineNum;j++)
									{
										writeObj.readLine();										
									}
//									Writing debug message in the java file
									//writeObj.writeChars("Log.errorMessage(\"Exception in "+ arr[i]+"\");}");
									writeObj.writeBytes("Log.errorMessage(\"Exception in "+ arr[i]+"\");}");									
								}
				
							}
							str=raf.readLine();						
							lineNum++;		//Calculating the line number				
						}																					
											
					}						
					
					
				}

	}import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;


public class FinalCode {

	public static void main(String[] args) throws IOException 							
	//********************************************************************************************
	//Pass the directory name as the command line argument which contains list of the java files 
	//********************************************************************************************
	{					
				RandomAccessFile raf = null;											 //To open file in read write mode
				
				int i=0;				
				String[] arr=null;														 //List of files	
				File file= new File(args[0]);											 //Passing directory name
				if (file.isDirectory())
				{
					 arr=file.list(); 													 //Copying list of files from the directory					
				}				
				for(i=0;i<arr.length;i++)
				{					
					System.out.println("File Name:"+arr[i]);
					FileReader fileReader = new FileReader(args[0]+"/"+arr[i]);			 //Take the full name of the file from the input directory
					raf = new RandomAccessFile(args[0]+"/"+arr[i],"rwd");				 //open the file in the read write mode								
					String str=raf.readLine();											 //String containing each line		
					int flag=0;				
					int lineNum=0;
					while (str!=null)
					{
						if(str.indexOf("catch(")!=-1&& str.indexOf("{")!= -1 && str.indexOf("}")!= -1 && str.indexOf("Log")<0){ //for catch(Exception e){} condition									
								System.out.println("Ronak");
								FileWriter fobj =new FileWriter(args[0]+"/"+arr[i],true);
							RandomAccessFile writeObj = new RandomAccessFile(args[0]+"/"+arr[i],"rwd");
							
								for(int j=0;j<lineNum;j++)
								{
									writeObj.readLine();										
								}
								//writeObj.setLength(writeObj.length()+400);
								flag=1;
								//Writing debug message in the java file
								//writeObj.write(1234);
								writeObj.writeBytes("catch(Exception e){Log.errorMessage(\"Exception in "+ arr[i]+"\");}");	
								str=raf.readLine();
								lineNum++;
								continue;							
						}
						
						if (str.indexOf("catch")!=-1){															//for catch(Exception e){
														         												//} condition
							System.out.println(str);							
							while(str.indexOf("{")<-1)  //Check for the opening brace
							{
								str=raf.readLine();
							}									
								while (str.indexOf("}")<0) //check for the closing brace
								{								
									str=raf.readLine();
									System.out.println(str);
									lineNum++;
																								
									if (str!=null){
										if (str.indexOf("Log(")!=-1)
										{
//											Writing debug message in the java file
											System.out.println("Having non empty catch block in java file"+arr[i]);
											flag=1;
											break;
										}
									}
								}
								if (flag==0)
								{
									System.out.println("Writing error message in the java file");
									RandomAccessFile writeObj = new RandomAccessFile(args[0]+"/"+arr[i],"rwd"); //Object to write in the file
									for(int j=0;j<lineNum;j++)
									{
										writeObj.readLine();										
									}
//									Writing debug message in the java file
									//writeObj.writeChars("Log.errorMessage(\"Exception in "+ arr[i]+"\");}");
									writeObj.writeBytes("Log.errorMessage(\"Exception in "+ arr[i]+"\");}");									
								}
				
							}
							str=raf.readLine();						
							lineNum++;		//Calculating the line number				
						}																					
											
					}						
					
					
				}

	}

The problem you are trying to solve here has already been addressed by AOP i.e. addressing cross cutting concerns. Read this.

Sifting through that big code would be quite a herculean task; I would advice you to use an IDE like Eclipse/Netbeans for debugging your application. Also I hope you realize that the logic you so painstakingly wrote would break as soon as someone decides to format the code in a different way?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.