Hello,

I am trying to access a binary file with scanner functionality. I am trying to extract integer, long double value from the binary file.

The binary file formation is as follows

The binary file is in little Endian and is structured as follows:
*
* <ul>
* <li>1 integer: containing the number of signals in the file = <b>N</b>.
* <li><b>N</b> signal descriptions, each consisting of:
* <ul>
* <li>0 (zero) terminated ANSI ASCII byte string which is the signal name.
* <li>0 (zero) terminated ANSI ASCII byte string which is the signal unit.
* <li>1 integer designating the data type <b>T</b> according to the ordinal of the enums
* in DataType.
* </ul>
* <li><b>M</b> Data sets: One value for each signal according to its data type <b>T</b>,
* * i.e. <b>N</b> values per data set. Data sets repeat until the end of the file.
* </ul>

Now I am trying to do the following

public void open(File filelocation) throws FileNotFoundException, IOException {


            dis = new Scanner (filelocation).useDelimiter("\\s*0\\s*");



        }

        @Override
        public void close() throws IOException {
            // TODO Auto-generated method stub
            dis.close();

        }

        @Override
        public List<SignalExtract> getSignals() {

        public List<SignalExtract> getSignals() {

                while ((dis).hasNext()) {
                    System.out.println("access");
                    container.index=dis.nextInt();

                    container.signalname = dis.next();
                    container.signalunit = dis.next();
                    decision = dis.hasNextInt();
                    if(decision==true)
                    {
                     enumdecision = dis.nextInt();
                        if(enumdecision == 1)
                        {

                            container.timeinsecond =  dis.nextLong();


                        }
                        if(enumdecision == 2)
                        {

                            container.doublevalue = dis.nextDouble();

                        }
                        }


                    userList.add(container);

                }



            return userList;
            }

**

Any help will be appriciated.

Scanner is "A simple text scanner", you can't use it to parse a binary file. Have a look at DataInputStream which reads binary data - although you may need to write a bit of code to handle text that's encoded as zero delimited bytes

Is it possible to provide any code snippet for this purpose.

Search for Java DataInputStream on the web, you will find many examples.

I have implimented the process with DatainputStream. The open method.

public void open(InputStream filelocation) throws FileNotFoundException, IOException {


        dis =  new DataInputStream (filelocation);



    }

The parsing method for the work is as follows. The problem is I get an error on first access at dis.readchar.

while (true) {

            container.index=dis.readInt();

            while(dis.readChar()!='0'){

                tempchar= dis.readChar();
                container.signalname  = container.signalname +tempchar;
            }
            container.signalname  = container.signalname +'\0';     

            while(dis.readChar()!='0'){

                tempchar= dis.readChar();
                container.signalunit  = container.signalunit +tempchar;
            }
            container.signalunit  = container.signalunit +'\0';     

            decision = dis.readInt();

                if(decision == 1)
                {

                    container.timeinsecond =  dis.readLong();


                }
                if(decision == 2)
                {

                    container.doublevalue = dis.readDouble();

                }


            if(dis.read()!= -1)
            break;

            userList.add(container);






        }
        return  userList;

    }

What error are you getting? I assume that dis is either an instance variable or a class variable, and thus would be in scope for both methods, is this correct?

Bebcareful - Java chars are 16 bit unicode, so readChar reads 2 bytes. You may prefer to read 1 byte ata time.

dis is a memeber of the class DataExtract. I have created an static object of this class in my main. As a result I can access all the resources of this object.

Exception in thread "main" java.lang.NullPointerException
    at src.com.ifta.candidate.test.faisalamin.DataExtract.getSignals(DataExtract.java:71)
    at src.com.ifta.candidate.test.faisalamin.MainWindow.main(MainWindow.java:30)



Line 71 is statement   container.index=dis.readInt();

The specification explicitly talks about little endian data and NUL (\0) terminated strings (like C programming language). You are better off using FileInputStream which reads raw bytes and handle them explicitly in your application. Java uses big endian so default method calls (readInt etc.) won't get you anywhere.

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.