Dear All,
I have a java application which is a listener receiving data from different gps devices. The programme have been working fine. Each time I receive a string it will read process and store into a local database(mysql) and also write into a text file. Off late I notice the text file is having a lot of corrupted data. What could be the cause can it be the programme itself or the network connection which is receiving it? Any idea please ?

Recommended Answers

All 9 Replies

is the data in the database corrupted as well? If not, perhaps your file writer is not being flushed, over-running a buffer, etc. What do the log files say, if anything? Are there any IOExceptions being thrown?

Dear Nelson,
Out of the many cases I went through all those with the corrupted data did not manage to get into the database but there was only one single one managed to make into the database. This is the sample of corrupted data in my text file "$80SLM,02,F,E000332,250411134157,022650216,000037,0000,000������������������’r�0164130E001000*71BD# 25/04/2011 21:50:12
$PA 25/04/2011 21:50:". Any idea of what could gone wrong?

Here is part of java code. If notice once I send I do flush.

w =  new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
         
w.write("$PA\n");
w.flush();

I'll go back to my question of your exception handling. What does your try/catch block look like? The reason I ask is I've seen many applications where the catch block is empty for whatever exception is being caught. Since this is a socket based communication, IO exceptions could either be coming from the network, or from writing the file.
On a final note, if this is being run on a Unix system such as Solaris, HP/UX, maybe a Linux system, have you checked your file system to make sure your inodes are not corrupted?

there are two areas

1/ set Charset and Collations in Database or DbTable on correct value
2/ for non-ASCII chars you have to use an Encode for file (2nd. parameters for File I/O)

Dear Nelson,
Attached below is part of my codes. This is one with the database part removed ready.

public class commServer {
  
  
   public static void main(String[] args) {

      try {
			    final ServerSocket serverSocketConn = new ServerSocket(9000);
				
				while (true) 
					{
						try 
						{
					            Socket socketConn1 = serverSocketConn.accept();
                                new Thread(new ConnectionHandler(socketConn1)).start();			            
						}
						catch(Exception e)
						{
							System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
						    e.printStackTrace(System.out);
						}
					}
			
      } 
      catch (Exception e) 
      {
         System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
         e.printStackTrace(System.out);
         //System.exit(0); 
      }
   }
   
}
  class ConnectionHandler implements Runnable {
    private Socket receivedSocketConn1;
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy"); 
    DateFormat inDf=new SimpleDateFormat("ddMMyyHHmmss");  
    DateFormat outDf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  

    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }

 
   //@Override
   public void run() { 
      
      Connection dbconn = null;
      BufferedWriter w = null;
      BufferedReader r = null;
      

      try {
      
         PrintStream out = System.out; 
      	 BufferedWriter fout = null;
         w =  new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
         r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
         
        
         
         
         int m = 0, count=0;
         String line="";
         String n="";
         w.write("$PA\n");
         w.flush();
         while ((m=r.read()) != -1) 
         {
           	  
           	  
              Date dateIn = new Date();
           	  n = n + (char) m;
           	   //n = n + (char) m;
           	  //  n = new StringBuffer().append((char)m).toString();
         	  int i = n.indexOf("GET");
						if(i != -1) { 
							break;
						}
			  
         	  // System.out.println("joinig the N : "+n);
         	  // System.out.println("\nM : "+m);
         	  if (m==35)
         	  {
	         	  
	             String ori = n;
	             String noCheckSum =  n.substring(0,(n.length()-4));
     			 int addExist = n.indexOf("@");
     			 
                 String[] slave = null;
                 if(addExist!=-1)
                 {
     	            slave = noCheckSum.split("@");
     	          	n = slave[0];
     	      	    //System.out.println(" slave : "+slave.length);
     	         }
			     else
			     {
			     	n = noCheckSum;
			     }
	                     
	             String[] result = n.split(",");
	                            
	             Statement stmt = null;
	             
	             
	             w.write("$PA\n");
                 w.flush();
	             
      
			      int count1 = 0;
			      
			      
	              	    
                      Date date = Calendar.getInstance().getTime(); 
                      String today = formatter.format(date); 
				      String filename= "MyDataFile"+today+".txt"; 
                      boolean append = true; 
                      
                      FileWriter fw = null;
                      try
                      {
                      
                      fw = new FileWriter(filename,append); 
	                  fw.write(ori+"  "+dateFormat.format(dateIn)+"\n");//appends the string to the file 
	                  Date dateOut = new Date();
	                  fw.write("$PA"+"  "+dateFormat.format(dateOut)+"\n");//appends the string to the file 
                      }
                      catch (IOException ex)  
				      { 
				          //ex.printStackTrace(new PrintWriter(sWriter));
				          System.out.println("MyError:IOException has been caught in in the file operation"+ex.toString());
				          ex.printStackTrace(System.out);
				      }      

 
      				  finally
      				  {
        
        			   try 
       				   {
        
	                   if ( fw != null ) 
	                   {
	          	       fw.close();
	                   }
	                   else 
	                   {
	        	       System.out.println("MyError:fw is null in finally close");
	                   //logger.log(Level.SEVERE, "MyError:fw is null in finally close", "");
	                   }
       
                       }
				        catch(IOException ex){
				          System.out.println("MyError:IOException has been caught in fw is null in finally close");
				          ex.printStackTrace(System.out);
				
				        }
      				  }
	                  try
			          {
	     	            //database part removed.
	                      

			          }
			          catch (SQLException ex)  
			          { 
                         System.out.println("MyError:Error SQL Exception : "+ex.toString());
                         ex.printStackTrace(System.out);
			          }      
				      finally
				      {
				        try 
				       	{
					        if ( stmt != null ) 
					        {
					          stmt.close();
					        }
					        else 
					        {
					        	System.out.println("MyError:stmt is null in finally close");
					        }
				        }
				        catch(SQLException ex){
				            System.out.println("MyError:SQLException has been caught for stmt close");
                            ex.printStackTrace(System.out);
				        }
				        try 
				       	{
					        if ( dbconn != null ) 
					        {
					          dbconn.close();
					        }
					        else 
					        {
					          //logger.log(Level.SEVERE, "MyError:dbconn is null in finally close", "");
					          System.out.println("MyError:dbconn is null in finally close");
					        }
				        }
				        catch(SQLException ex){
				             System.out.println("MyError:SQLException has been caught for dbconn close");
                             ex.printStackTrace(System.out);
				        }
								        
				      }
	             
	             n="";
	            
         	  }
         }
      } 
      catch (IOException ex)  
      { 
           System.out.println("MyError:IOException has been caught in in the main first try");
           ex.printStackTrace(System.out);
      }      
      finally
      {
        try 
       	{
        
	        if ( w != null ) 
	        {
	          	w.close();
	        }
	        else 
	        {
	        	System.out.println("MyError:w is null in finally close");
	        }
        }
        catch(IOException ex){
           System.out.println("MyError:IOException has been caught in w in finally close");
           ex.printStackTrace(System.out);
        }
        
      }
   }
}

Dear Nelson,
Yes this is one is running on centos. So how to check the inodes. The thing is this behaviour is not occuring for all the devices only certain devices.

To check inode usage - df -F ufs -o i

I only mention this because I've seen weird behavior with corrupt inodes, even when the disk isn't full.

Dear Nelson,
Where to run this in the folder where my java file is it ? I have run like this - df -F ufs -o i it says command not found.

Dear Nelson,
Do you see any errors in my coding? Thank you.

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.