Results 1 to 8 of 8

Thread: [RESOLVED] how to read the data from .dat file?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    6

    Resolved [RESOLVED] how to read the data from .dat file?

    May i know how to read data from .dat file by using binary?Maybe my question is not clear......i paste out the code that i am using......

    import java.io.*;

    class BinaryFile
    {
    public static final short BIG_ENDIAN = 1;

    public static final short LITTLE_ENDIAN = 2;

    protected RandomAccessFile _file;

    protected short _endian;

    protected boolean _signed;

    public BinaryFile (RandomAccessFile f)
    {
    _file = f;
    _endian = LITTLE_ENDIAN;
    _signed = false;
    }

    public void setEndian(short i) throws Exception
    {
    if ((i == BIG_ENDIAN) || (i == LITTLE_ENDIAN))
    _endian = i;
    else
    throw (new Exception(
    "Must be BinaryFile.LITTLE_ENDIAN or BinaryFile.BIG_ENDIAN"));
    }

    public int getEndian()
    {
    return _endian;
    }

    public void setSigned(boolean b)
    {
    _signed = b;
    }

    public boolean getSigned()
    {
    return _signed;
    }

    public String readFixedString(int length) throws java.io.IOException
    {
    String rtn = "";

    for (int i = 0; i < length; i++)
    rtn += (char) _file.readByte();
    return rtn;
    }

    public void writeFixedString (String str, int length) throws
    java.io.IOException
    {
    int i;

    if (str.length() > length)
    str = str.substring (0, length);

    for (i = 0; i < str.length(); i++)
    _file.write(str.charAt(i));

    i = length - str.length();
    while ((i--) > 0)
    _file.write(0);
    }

    public String readLengthPrefixString() throws java.io.IOException
    {
    short len = readUnsignedByte();
    return readFixedString(len);
    }

    public void writeLengthPrefixString(String str) throws
    java.io.IOException
    {
    writeByte((byte) str.length());
    for (int i = 0; i < str.length(); i++)
    _file.write(str.charAt(i));
    }

    public String readFixedZeroString(int length) throws
    java.io.IOException
    {
    String rtn = readFixedString(length);
    int i = rtn.indexOf(0);
    if ( i != -1)
    rtn = rtn.substring(0, i );
    return rtn;
    }

    public void writeFixedZeroString(String str, int length) throws
    java.io.IOException
    {
    writeFixedString(str, length);
    }

    public String readZeroString() throws java.io.IOException
    {
    String rtn = "";
    char ch;

    do
    {
    ch = (char) _file.read();
    if(ch != 0)
    rtn += ch;
    }while (ch != 0);
    return rtn;
    }

    public void writeZeroString(String str) throws java.io.IOException
    {
    for (int i = 0; i < str.length(); i++)
    _file.write(str.charAt(i));
    writeByte((byte) 0 );
    }

    protected short readUnsignedByte () throws java.io.IOException
    {
    return (short) (_file.readByte() & 0xff);
    }

    public short readByte() throws java.io.IOException
    {
    if (_signed)
    return (short) _file.readByte();
    else
    return (short) _file.readUnsignedByte();
    }

    public void writeByte(short b) throws java.io.IOException
    {
    _file.write(b & 0xff);
    }

    public int readWord() throws java.io.IOException
    {
    short a, b;
    int result;

    a = readUnsignedByte();
    b = readUnsignedByte();

    if (_endian == BIG_ENDIAN)
    result = ((a << 8 ) | b);
    else
    result = (a | (b << 8));

    if (_signed)
    if (( result & 0x8000) == 0x8000)
    result = - (0x10000 - result);
    return result;
    }

    public void writeWord(int w) throws java.io.IOException
    {
    if (_endian == BIG_ENDIAN)
    {
    _file.write((w & 0xff00) >> 8);
    _file.write(w & 0xff);
    }
    else
    {
    _file.write(w & 0xff);
    _file.write((w & 0xff00) >> 8);
    }
    }

    public long readDWord() throws java.io.IOException
    {
    short a, b, c, d;
    long result;

    a = readUnsignedByte();
    b = readUnsignedByte();
    c = readUnsignedByte();
    d = readUnsignedByte();

    if (_endian == BIG_ENDIAN)
    result = ((a << 24) | (b << 16) | (c << 8) | d);
    else
    result = (a | (b << 8) | (c << 16) | (d << 24));

    if (_signed)
    if ((result & 0x80000000L) == 0x80000000L)
    result = -((0x100000000L) - result);
    return result;
    }

    public void writeDWord(long d) throws java.io.IOException
    {
    if (_endian == BIG_ENDIAN)
    {
    _file.write((int) (d & 0xff000000) >> 24);
    _file.write((int) (d & 0xff0000) >> 16);
    _file.write((int) (d & 0xff00) >> 8);
    _file.write((int) (d & 0xff));
    }
    else
    {
    _file.write((int) (d & 0xff));
    _file.write((int) (d & 0xff00) >> 8);
    _file.write((int) (d & 0xff0000) >> 16);
    _file.write((int) (d & 0xff000000) >> 24);
    }
    }

    public void align (int a ) throws java.io.IOException
    {
    if ((_file.getFilePointer() % a) > 0)
    {
    long pos = _file.getFilePointer() / a;
    _file.seek((pos + 1) * a);
    }
    }
    }
    this is the binary class file i am using.

    import java.io.*;

    class BinaryExample
    {
    public static void main(String args[]) throws FileNotFoundException
    {
    int i;
    String stra,strb;
    RandomAccessFile file;
    BinaryFile bin;
    final short endian = BinaryFile.BIG_ENDIAN;
    final boolean signed = true;

    try
    {
    file = new RandomAccessFile("011.dat","r");
    bin = new BinaryFile(file);
    bin.setEndian(endian);
    bin.setSigned(signed);
    stra = bin.readFixedString(25);
    strb = bin.readFixedString(2);
    file.close();

    System.out.println("Wafer ID\t:" + stra);
    System.out.println("Wafer No.\t:" + strb);
    }
    catch ( Exception e)
    {
    System.out.println("**Error:" + e.getMessage());
    }
    }
    }
    This is the main file(not complete yet).

    however i could not read out the exact output....any one can help me?
    Last edited by rita_ruan; Sep 16th, 2006 at 02:40 AM. Reason: question stated is not clear

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: how to read the data from .dat file?

    The following code will read and file using a buffer, 1KB everytime
    Code:
        FileInputStream fis=new FileInputStream("Path");
        DataInputStream dis = new DataInputStream( fis ) ;
        byte[] buffer = new byte[1024] ;
        try
        {
          while ( dis.available() > 0 )
          {
    	dis.read( buffer ) ;
    	// Your code here
          }
          fis.close() ;
          dis.close() ;
        }
        catch ( IOException ex )
        {
        }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    6

    Re: how to read the data from .dat file?

    thanks computerJy,
    for my problem is that,i created a binary class file using RandomAccessFile to access the .dat file....
    and need to creat a main file which contains several methods to read the data from .dat file...
    example like: readByte(); readFixedString(); etc.............

    however the output is wrong.how should i write to get the correct output?
    Last edited by rita_ruan; Sep 16th, 2006 at 02:00 AM.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: how to read the data from .dat file?

    Code:
      public void writeFixedString ( String str, int length ) throws
          java.io.IOException
      {
        int i ;
        if ( length <= 0 )
        {
          throw new IllegalArgumentException( "length must be a positive number" ) ;
        }
        if ( str.length() > length )
        {
          str = str.substring( 0, length ) ;
        }
    
        for ( i = 0 ; i < str.length() ; i++ )
        {
          _file.write( str.charAt( i ) ) ;
        }
    
        i = length - str.length() ;
        {
          while ( ( i-- ) > 0 )
          {
    	_file.write( 0 ) ;
          }
        }
      }
    I've added an extra line, but your code doesn't write bytes, it's writing int values why not use
    Code:
        _file.writeChars( str ) ;
    Or
    Code:
        for ( i = 0 ; i < str.length() ; i++ )
        {
          _file.writeByte( str.charAt( i ) ) ;
        }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    6

    Re: how to read the data from .dat file?

    actually now i have a .dat file.....i cannot write the information to it....my aim is to read data out of it....so do you know the code to read data from the file?

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: how to read the data from .dat file?

    You don't just read a data file, you must know the format of written data...
    Some data files might be videos, others make dababase backup files... you must find out the format of the file before you do this
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    6

    Re: how to read the data from .dat file?

    thanks computerJy,
    the file format is ASCII, length is in bytes. actually i have the output in my hand...but i dun know how to get it.

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [RESOLVED] how to read the data from .dat file?

    Try this for reading
    Code:
      public String readFixedString ( int length ) throws IOException
      {
        byte[] bytes = new byte[length] ;
        for ( int i = 0 ; i < length ; i++ )
        {
          bytes[i] = _file.readByte() ;
        }
        return new String( bytes ) ;
      }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width