Binary input in java 5

Hi all, I'm working on a project for class and have come up against a problem and unfortunately my text is not with me... if anyone could help me out I'd be really grateful! My constructor for the MyStudents class looks like this:
public MyStudents(String inF, String outF) throws EOFException, IOException,
          ClassNotFoundException {
          ObjectInputStream fileInput = new ObjectInputStream
               (new FileInputStream (inF));
          outFilename = outF;
          nStudents = 0;
          int i = 0;
          while (true) {
               s[i] = new Student(fileInput.readUTF(), (Date) fileInput.readObject(), fileInput.readInt());
               i++;
               nStudents++;
     }In my main method, after asking the user to input the binary input file, I have this code:
          MyStudents myClass = null;
          try {
               myClass = new MyStudents(in, out);
          } catch (EOFException e) {
               System.out.println("EOFException");
          } catch (IOException e) {
               System.out.println("IOException");
          } catch (ClassNotFoundException e) {
               System.out.println("ClassNotFoundException");
          } catch (Exception e) {
               System.out.println(e.getMessage());
          System.out.println("Initial list of students...");
          myClass.toString();however then I am getting a null pointer exception when i do myClass.toString()... any thoughts as to why? Thanks!

Hmm .. where to start?
First of all it seems that you are using exceptions as a flow control mechanism, not really what they are intended for.
Secondly, when an exception is thrown in a constructor, the constructor does not finish and thus the object does not get constructed. It is never good to have a constructor throw exceptions, just handle the exception internally.
And lastly you're not closing the input stream.
Try replacing
while (true)with
while (fileInput.available() > 0)That said, your reading of objects is assuming that the file is of the correct form. If the file is not a series of UTF string, date object and integer you'll still have the same problem.

Similar Messages

  • Read binary file in java

    To read a text file i used
    FileReader reader=new FileReader("d:/a.txt");
    please guide me on how to read a binary file in java
    Any input would be greatly appreciated!
    Thanks in advance!
    Edited by: nikkj on Aug 7, 2010 7:36 PM

    I'll give you the quick answer: you use Readers for character streams (e.g., text files) and InputStreams for binary streams (e.g., binary files).
    Still, read the tutorial. You will find out lots of useful info.

  • Store a uploaded file of type binary into a java.sql.Blob

    Hi all,
    I try a File-Upload and store the file in a  java.sql.Blob of a MaxDB.
    My Problem is, that I'm not able to import a Model-Attribute of data type byte[]. Further I don't no how to convert the uploaded value attribute of data type binary, in a java.sql.Blob.
    Regards,
    Silvia Hofmann

    http://www.excelsior-usa.com/jet.html
    http://www.ej-technologies.com/products/exe4j/overview.html
    http://jsmooth.sourceforge.net/
    Distributing your Application as an executable JAR file
    Google is your friend.

  • How to store binary data in Java

    I need to show news in my web aplication.So I am retrieving news from a Feed Server in which the field GEN_UNICODE_MESSAGE_1 gives the news data.News consists of both English/Arabic data.So how will I store the binary content in JAVA and how can I display the news in JSP?
    I tried storing the binary content in ByteArrayInputStream and appended it using StringBuilder.But I didnt get the desired output.Output is seen as ÇáÎáíÌíÉ ááÇÓÊËãÇÑÇáãÌãæÚÉÇáÔÑßÉÇáÇãÓÈÊãÈÑÓÈÊãÈÑÅÌãÇá.Please help me.
    Here GEN_UNICODE_MESSAGE_1 is of type BINARY.
    try{
    BufferedInputStream in=new BufferedInputStream(new ByteArrayInputStream((byte[])msg.getField("GEN_UNICODE_MESSAGE_1").value.get(h)));
    //ByteArrayInputStream in=new ByteArrayInputStream(((byte[])msg.getField("GEN_UNICODE_MESSAGE_1").value.get(h)));
    StringBuilder builder = new StringBuilder();
    byte[] buff=new byte[1024];
    int len;
    while ((len=in.read(buff))!=-1){
    builder.append(new String(buff,0,len));
    String incomingmsg=builder.toString();
    }catch(IOException e){}
    Edited by: Alance on May 7, 2010 10:12 PM

    BufferedInputStream bis=new BufferedInputStream(new ByteArrayInputStream((byte[])msg.getField("GEN_UNICODE_MESSAGE_1").value().toString().getBytes()));
    BufferedReader br = new BufferedReader(new InputStreamReader(bis));What on earth is all this?
    1. What is the type of msg.getField("GEN_UNICODE_MESSAGE_1").value()?
    2. Whatever that type is, you are then converting it to a String.
    3. You are then converting that to bytes.
    4. You are then casting that to byte[], which it already is,
    5. You are then wrapping a ByteArrayInputStream around that.
    6. ... and an InputStreamReader round that ...
    7. ... and a BufferedReader around that ...
    8. ... and reading lines
    9. ... and appending them to a StringBuilder
    10. ... and converting that to a String.
    Which you already at at step 2.
    String incoming = msg.getField("GEN_UNICODE_MESSAGE_1").value().toString();Not sure whether that's correct given the charset issues, but if not the problem starts at step 2. In any case steps 3-10 add precisely nothing.
    This is all pointless. The key question is (1). Answer that first.
    2. The type of String.getButy
    >
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = br.readLine()) != null) {
    sb.append(line + "\n");
    br.close();
    String incomingmsg=sb.toString();

  • Binary operations in Java

    Hi,
    are there any binary operations in Java like '&' in C? I didn't found any information in the API description.
    Thank u.

    You didn't find them from the API since they are a feature of the language. Try the langspec instead: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5228

  • How to convert numeric data to binary decimal in java

    How to convert numeric data to binary decimal in java Pleas egive me code example

    There is no numeric data. It's all binary. If you're talking about Strings, look at the Integer class.

  • Creating non binary trees in Java

    Hi everybody actually i am used in creating binary trees in java,can u please tell me how do we create non binary -trees in java.

    Hi,
    Can u let us know the application area for Non binary tree.
    S Rudra

  • Non binary trees in java

    Hi guys i am used in creating binary trees ,tell me how do we create non binary trees in java.

    public class Node {
      private final Object payload;
      private final Set<Node> children;
      public Node(Object payload) {
        this.payload = payload;
        children = new HashSet<Node>();
      // now add methods to add/remove children, a method to do something with the payload (say,
      // a protected method that passes the payload to a method specified by some kind of interface),
      // and methods to recurse over children.
    }Actually rather than using Object probably should have generic-ized the class, but whatever.

  • Creating Binary Files in Java

    Does anyone know how to create a binary file in Java.
    in C i can do an fopen("filename" , "rb") i cannot find a equivalent java binary file stream.
    Thanks

    The following code is part of my FileIO applet. This method allows the user to download any file from my server and save it on his/her disk, the download is accomplished by doing a byte-read and byte-write (byte mover as I'd call it). This example illustrates how you can read and write binary files in Java:
       public void aok_DownLoad(String inputFile, String outputFile) {
          try {
             URL url=new URL(inputFile);
             InputStream in;
             in=url.openStream();
             BufferedInputStream reader=new BufferedInputStream(in,4096);
             FileOutputStream out=new FileOutputStream(outputFile);
             BufferedOutputStream writer=new BufferedOutputStream(out,4096);
             byte[] buf=new byte[4096];
             int byteRead;
             while ((byteRead=reader.read(buf,0,4096))>=0) {writer.write(buf,0,byteRead);}
             reader.close();
             writer.flush();
             writer.close();
          catch (Throwable exception) {
             exception.printStackTrace();
       }V.V.
    PS: in this posting the code is posted in a different manner so that the > sign is not converted to & gt ; by the forum's software

  • Binary Tree in Java - ******URGENT********

    HI,
    i want to represent a binary tree in java. is there any way of doing that.
    thanx
    sraphson

    HI,
    i want to represent a binary tree in java. is there
    e any way of doing that.
    thanx
    sraphsonFirst, what is a binary tree? Do you know how the binary tree looks like on puesdo code? How about a representation in terms of numbers? What is a tree? What is binary? The reason I ask is, what do you know about programming?
    Asking to represent a binary tree in java seems like a question who doesn't know how it looks like in the first please. Believe me, I am one of them. I am not at this level yet. If you are taking a class that is teaching binary trees and you don't know how it looks like, go back to your notes.
    Sounds harsh, but it is better to hear it from a person that doesn't know either then a boss that hired you because Computer Science was what you degree said. Yet, you don't know how to program?
    Telling you will not help you learn. I can show you tutorials of trees would be start on where to learn.
    HOW TO USE TREES (oops this is too simple, but it is a good example)
    http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
    CS312 Data Structures and Analysis of Algorithms
    (Here is a course about trees. Search and learn)
    http://www.calstatela.edu/faculty/jmiller6/cs312-winter2003/index.htm

  • Binary trees in Java

    Hi there
    Do you have any suggestions about how to implement trees or binary trees in Java?
    As far as I know there are already some libraries for that, but I don't know how to use them. Was trying to get some implementation examples but I'm still very confused.
    How can I use for example:
    removeChild(Tree t)
    addChild(Tree t)
    isLeaf()
    Thanks in advance

    Lulu wrote:
    Hi there
    I have several questions about binary trees
    Let's see, I use TreeMap to create them with the following code:
    TreeMap treeMap = new TreeMap<Integer, String>();
    treeMap.put("first", "Fruit");
    treeMap.put("second","Orange");
    treeMap.put("third", "Banana");
    treeMap.put("fourth", "Apple");You've defined the map to hold integer keys and strings as values, yet you're trying to add string keys and string values to it: that won't work.
    If this is a map how do I define if the data should go to the left or to the right of certain node?That is all done for you. In a TreeMap (using the no-args constructor), you can only store objects that are comparable to each other (so, they must implement the Comparable interface!). So the dirty work of deciding if the entry should be stored, or traversed, into the left or right subtree of a node, is all done behind the scenes.
    Also note that TreeMap is not backed up by a binary tree, or a binary search tree, but by a red-black tree. A red-black tree is a self balancing binary tree structure.
    Should I have dynamical keys so that they increase automatically when adding new data?
    According to a webpage I should use Comparator(), is that to find the data in the tree and retrieve the key?
    ThanksI am not sure what it is you want. I am under the impression that you have to write a binary tree (or a binary search tree) for a course for school/university. Is that correct? If so, then I don't think you're permitted to use a TreeMap, but you'll have to write your own classes.

  • Converting binary input into a constellat​ion diagram

    Hi
    I am using the MT Format constellation vi for generating a constellation diagram. The input signal is a multitone signal with 3 tones. I am not able to give this input to the vi. Is there any other vi that I could use? Also can I convert the multitone signal into a binary input for the vi.
    Thank you.

    You need to be a bit more specific.
    What signal are you trying to convert?  Can you post an example with data?
    Jeff

  • Sensor output as input to java program

    hi , pls tell me how to use output from infrared senors as input to java program..................???????
    mail me the code at [email protected]

    Hi,
    Check the following syntax:
    SUBMIT <rep> TO SAP-SPOOL
    [<params>|SPOOL PARAMETERS <pripar>]
    [ARCHIVE PARAMETERS <arcpar>]
    [WITHOUT SPOOL DYNPRO].
    Ex:
    The following executable program is connected to the logical database F1S:
    REPORT SAPMZTS1.
    TABLES SPFLI.
    GET SPFLI.
    NEW-LINE.
    WRITE: SPFLI-MANDT, SPFLI-CARRID, SPFLI-CONNID,
    SPFLI-CITYFROM, SPFLI-AIRPFROM, SPFLI-CITYTO,
    SPFLI-AIRPTO, SPFLI-FLTIME, SPFLI-DEPTIME, SPFLI-ARRTIME,
    SPFLI-DISTANCE, SPFLI-DISTID, SPFLI-FLTYPE.
    The following program calls SAPMZTS1 and sends the output to the spool system:
    REPORT SAPMZTST NO STANDARD PAGE HEADING.
    DATA: VAL,
    PRIPAR LIKE PRI_PARAMS,
    ARCPAR LIKE ARC_PARAMS.
    CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
    LAYOUT = 'X_65_132'
    LINE_COUNT = 65
    LINE_SIZE = 132
    IMPORTING
    OUT_PARAMETERS = PRIPAR
    OUT_ARCHIVE_PARAMETERS = ARCPAR
    VALID = VAL
    EXCEPTIONS
    ARCHIVE_INFO_NOT_FOUND = 1
    INVALID_PRINT_PARAMS = 2
    INVALID_ARCHIVE_PARAMS = 3
    OTHERS = 4.
    IF VAL <> SPACE AND SY-SUBRC = 0.
    SUBMIT SAPMZTS1 TO SAP-SPOOL
    SPOOL PARAMETERS PRIPAR
    ARCHIVE PARAMETERS ARCPAR
    WITHOUT SPOOL DYNPRO.
    ENDIF.
    Regards,
    Bhaskar
    Edited by: Bhaskar Chikine on Sep 9, 2008 4:33 PM

  • Newbie, having a problem with Input in Java

    Hello, Thank you for reading this!
    I am having a hard time understanding this.
    I am having an awful time with Input into variables. The following code I have got where what I type in anything from the keyboard it stores it in the variable String "name", but when I do an If statement with the variable it does not equal when I want it too.
    Example,
    If I type in "yes" and it should display in the output
    You said yes
    science
    It will do the first part, what I typed in but when I say
    if answer == "yes" then display science
    it always goes with the else statement.
    It is little things like this that blows my mind. How am I going to tell it with an conditional operator if it does not look at them the same?
    It says I typed in yes I know this by thr System.outprintln("You typed, answer);
    but when I do an if answer == "yes" and inside the variable "answer" is "yes"
    it will not match.
    Why????????
    Is it because I am also pressing the enter key and it see's this?
    I have read the book over and over, I don't know what I am missing.
    When you look at an Input example in the book they are almost always numbers like:
    If (temp <40)
    System.out.println("Not freezing");
    Please help me with the input and conditions.
    Below is just simple code, look at the If area?
    import java.io.*;
    public class test
    public static void main(String args[])
    String name;
    BufferedReader reader;
    reader= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Do you want to take the test? answer yes or no");
    try {
    answer =reader.readLine();
    System.out.println("You said " + answer);
    if (answer == "yes")
    System.out.println("Science");
    else
    System.out.println("Arts");
    catch (IOException ioe) {
    System.out.println("I/O Exception Occurred");

    You could tidy it up a bit on other ways too;-import java.io.*;
    public class test{
       public static void main(String args[]){
          String answer = "";
          BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Do you want to take the test? Please answer \"yes\" or \"no\": ");
             try {
                answer =reader.readLine();
                System.out.println("You said " + answer);
                answer = answer.toUpperCase().trim();
                if (answer.equals("YES") ) System.out.println("Science");
                else System.out.println("Arts");
             catch (IOException ioe) { System.out.println("Exception "+ioe);

  • DE PDP-1 binary file from Java

    Can someone here help me, please!?
    Anyone know how to convert a Java class file into a binary file that will run natively on my Digital PDP-1 computer? I just spent over $120,000 for it! Thanks.
    This resurrected thread was first posted November 25, 1960 at 8:25AM
    -------------------------------------------------------------------------

    This resurrected thread was first posted November 25,
    1960 at 8:25AMIn what time zone?

Maybe you are looking for

  • Need Suggestion to Stage Process Order related data

    Hi All, Could anybody help me by providing the solution or suggestion for the problem which I am describing here. The problem is like to stage the process order related data (which are downloaded from SAP ECC for sending to machine data base or confi

  • Can't charge Apple BT headset through iPhone 3G Dock?

    Is this correct? I plugged the BT charging cable into the Apple iPhone 3G dock. I put the iPhone in the dock to charge, and the headset in its charging port, and left it overnight. This morning I noticed the headset had lost charging! Then I just plu

  • Problem with the selection screen

    Hi All,        In my program i have given the input in selection screen , Its process something and a msg was displayed , and user confirm it to continue , the program take us back to the selection screen - in this case i have to clear all the values

  • Create partition taking too long

    hi, In our 11g database we have 15 tables which are range-list partitioned. Range partitioning on some of these tables is taking nearly 10mins/table and the whole process is taking nearly 1hour. actually we are splitting the existing partitions based

  • ERROR: package.opf is over 300KB

    Hi All, For one of the EPUB package I have; EpubPreflight Version 0.1.0 shows an "Error: package.opf is over 300KB". Whereas EPUB Best Practice 1.0.3 does not say that OPF file should or must be below 300KB uncompressed. The best practice guide only