Mohsin Ali
Mohsin Ali

Reputation: 123

FileInputStream returns null

I have two methods, both using FileInputStream Objects. The First one returns expected value. This method works fine. But the Second method returns nothing. The value passed to the second method is not null. I need to get the hexadecimal format of the files passed to methods. Why is it so? Kindly Explain. Here is my code

public String binaryFile1(File file1){
        try{
        stringBuilder1=new StringBuilder();
        is1=new FileInputStream(file1);
        while(b!=-1){
            counter++;
            b=is1.read();
            String s = Integer.toHexString(b).toUpperCase();
        if (s.length() == 1) {
            stringBuilder1.append('0');
        }
            if(counter%5==0){
               stringBuilder1.append(s).append("\n");
               counter=0;
            }else
                stringBuilder1.append(s).append(' ');

        }
        is1.close();
        }catch(Exception e){
            e.printStackTrace();
        }

        return stringBuilder1.toString();
}


public String binaryFile2(File file2){
        try{
        stringBuilder2=new StringBuilder();
        is2=new FileInputStream(file2);
        while(b!=-1){
            counter++;
            b=is2.read(); //Here b does not get any content assigned.
            String s = Integer.toHexString(b).toUpperCase();
        if (s.length() == 1) {
            stringBuilder2.append('0');
        }
            if(counter%5==0){
               stringBuilder2.append(s).append("\n");
               counter=0;
            }else
                stringBuilder2.append(s).append(' ');
        }
        is2.close();
        }catch(Exception e){
            e.printStackTrace();
        }

        return stringBuilder2.toString(); //Here stringBuilder2 is null
}

Upvotes: 0

Views: 2483

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

Since b is shared and you don't reset it after binaryFile1 it's still -1 at the start of binaryFile2. I suggest you use,

int b;
while ((b = is2.read()) != -1) {
  // ...
}

Edit

It is important to close your resources when you're done. I also suggest you try and limit variable scope as much as possible. Using try-with-resources you could write binaryFile2 like

public String binaryFile2(File file) {
    StringBuilder sb = new StringBuilder();
    int counter = 0;
    try (InputStream is = new FileInputStream(file)) {
        int b;
        while ((b = is.read()) != -1) {
            counter++;
            String s = Integer.toHexString(b).toUpperCase();
            if (s.length() == 1) {
                sb.append('0');
            }
            sb.append(s);
            if (counter % 5 == 0) {
                sb.append(System.lineSeparator());
                counter = 0;
            } else {
                sb.append(' ');
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

Upvotes: 3

Related Questions