Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JSP Interest »

Help me

Snehal Pandya

2003-07-17

Replies:

Friends,

I am in a problem.
I want to write message log .
For that I am using Random access file.
I want outout on the JSP.

My JSP file is

<%@(protected).*" %>
<%@(protected).*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>
<BODY>
<jsp:useBean id="r1" class="p1.RandomFileFinal" scope="session" />
<form name="dataView" method= "post" action = "fileView.jsp">
<%
   boolean eofsw=false;
   out.println("Reading...");
  String b="";
   try{
       if(!eofsw){
           out.println("Entered the if..");
           b=r1.readData(0,4);
           out.println(b);
           out.println("<br>");
       }
   }
   catch(Exception e){
       out.println("Error in the file");
   }


%>
</form>
</BODY>
</HTML>

And Java File is

package p1;
import java.io.*;

public class RandomFileFinal
{
   public void writeData1(String type, String origin, String msg)
   {
       byte b=0;
       long maxsize = 500;
       String hdrMsg="0|0|0|*";
       String str="";
       byte dtlMsg[] = new byte[20];
       byte hdrData[] = new byte[30];
       RandomAccessFile rs= null;
       String sub[] = new String[4];
       int l=0,k=0,i=0;
       long hdrSize =0;
       long remHdr =0,remDetail=0;
       int cur=0;
       try{
           rs= new RandomAccessFile("c:/FileLog.dat","rw");

           if ( rs.length()==0){
               rs.writeBytes(hdrMsg);
           }
           else{
               rs.seek(0);
               while(true){
                   b=rs.readByte();
                   char c = (char)b;
                   if (c=='*')
                   {
                       hdrSize = rs.getFilePointer();
                       remHdr=30-hdrSize;
                       break;
                   }
                   str+=c;
               }
               while(l<str.length()){
                   k = str.indexOf("|",l);
                   sub[i] = str.substring(l,k);
                   l=k+1;
                   i++;
               }
               long l1 = rs.length();
               sub[1]=String.valueOf(l1);
               cur = Integer.parseInt(sub[2]);
               if (cur==99){
                   cur= 0;
               }
               else{
                   cur++;
               }
               sub[2]=String.valueOf(cur);
               rs.setLength(maxsize);
               hdrMsg=String.valueOf(maxsize)+"|"+sub[1].trim()+"
|"+sub[2].trim()+"|*";
               hdrData= hdrMsg.getBytes();
               rs.seek(0);
               rs.write(hdrData);
               rs.seek(0);
               rs.skipBytes(30+cur*20);
               type+=origin.trim()+msg.trim();
              dtlMsg =type.getBytes();
               rs.write(dtlMsg);
               rs.close();
           }
       }
       catch(IOException e){
           System.out.println("Error in Write Data and in the file
cration");
           //rs.close();
       }


   }

   public String readData(int start, int end)
   {
       int cur=0;
       byte b=0;
       int i=0,j =0;
       String dataRet ="";
       boolean eofsw = true;
       String str="";
       System.out.println(start);
       byte[] data = new byte[20];
       try{
           RandomAccessFile rs= null;
           long hdrSize =0;
           rs= new RandomAccessFile("c:/FileLog.dat","r");
           rs.seek(0);
           char tests;

           rs.seek(0);
           rs.skipBytes(30+(start*20));
           while(eofsw){
               str="";
               if(start >= end ){
                   break;
               }
               try{
                   for(j = 0; j<=2000;j++){
                       tests= (char)rs.readByte();
                       System.out.print(tests);
                       str+=tests;
                   }

                   start++;
               System.out.println(str);

               }
               catch(EOFException eof){
                   eofsw = false;
                   System.out.println("This is Error ");

              }
               rs.close();
               return(str);

           }


       }catch(IOException ie){
               System.out.println("Can't open the File ");
       }



         return(null);
   }


}

My problem is I am not getting out put record by record it is comming in
the one string.

Please help me
Thanks in advance

Snehal
---------------------------------------------------------------------------------------------------------

No one in this world has been honored for what they received.
They were honored for what they gave.

===========================================================================
To unsubscribe: mailto listserv@(protected)".
For digest: mailto listserv@(protected)".

Some relevant archives, FAQs and Forums on JSPs can be found at:

http://java.sun.com/products/jsp
http://archives.java.sun.com/jsp-interest.html
http://forums.java.sun.com
http://www.jspinsider.com


©2008 junlu.com - Jax Systems, LLC, U.S.A.