Results 1 to 8 of 8

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

Threaded View

  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

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