0

Other students in my class have gotten this code to work, but I get an error. I am trying to use the readAllBytes() method for an assignment and I can't get it to work. My teacher there is another way he intended it to work using That method is not defined for DataInputStream. Take a look at the read(byte[]) method and the first integer in the datastream shows how large the byte Array is. Either way will work, I just am getting an error on line 39, and if I delete "import java.io.DataInputStream;" it fixes that error but I get an error on line 31 instead. Any help would be much appreciated enter image description here

package edu.ics211.h03;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.io.InputStream;

/**
 * Represents a ReadFile.
 *
 * @author Constantine Peros
 */
public class ReadFile implements IReadFile {

  /**
   * Creates a New ReadFile.
   */
  public ReadFile() {
    // TODO Auto-generated constructor stub
  }

  @Override
  public String readFile(String fileName) throws IOException {
    // Create a FileInputStream from the file name
    // Create a DataInputStream from the FileInputStream
    DataInputStream data = new DataInputStream(new FileInputStream(fileName));
    // Read the number of bytes using readInt
    int size = data.readInt();
    // Read the encoding using readByte
    byte encoding = data.readByte();
    // Create a byte array number of bytes long
    byte[] byteArray = new byte[size];
    // Fill the byte array
    byteArray = data.readAllBytes();
    // Close my inputs
    data.close();
    // Switch the encoding or use if statements
    switch (encoding) {
      case 1:
        return new String(byteArray, StandardCharsets.US_ASCII);
        break;
      case 2:
        return new String(byteArray, StandardCharsets.UTF_16LE);     
        break;
      case 3:
        return new String(byteArray, StandardCharsets.UTF_8);     
        break;
      case 4:
        return new String(byteArray, StandardCharsets.UTF_16);     
        break;
      default:
        return null;
    }
  }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
Constantine
  • 1
  • 1
  • 4
  • 1
    [`readAllBytes()`](https://docs.oracle.com/javase/9/docs/api/java/io/InputStream.html#readAllBytes--) is defined in Java9. Make sure you use the correct java version (compiler and JRE). – Progman Sep 11 '20 at 23:50
  • 1
    You should try to post a [mcve]. What kind of file are you trying to read? Is it a text file? Just like you copied your java code from your IDE and pasted it as text in your question, you should do the same with the error message. You shouldn't post an image of your error message. – Abra Sep 11 '20 at 23:52
  • Maybe you can post a sample file that you are trying to read? – Abra Sep 11 '20 at 23:58
  • Please mark line 39, as it is not visible to us. – NomadMaker Sep 12 '20 at 06:34

1 Answers1

0

Seems to me that all you need to do is replace this line of the code

byteArray = data.readAllBytes();

with this line

data.read(byteArray);

Here is the entire method.

public String readFile(String fileName) throws IOException {
    // Create a FileInputStream from the file name
    // Create a DataInputStream from the FileInputStream
    DataInputStream data = new DataInputStream(new FileInputStream(fileName));
    // Read the number of bytes using readInt
    int size = data.readInt();
    // Read the encoding using readByte
    byte encoding = data.readByte();
    // Create a byte array number of bytes long
    byte[] byteArray = new byte[size];
    // Fill the byte array
    data.read(byteArray);
//  byteArray = data.readAllBytes();
    // Close my inputs
    data.close();
    // Switch the encoding or use if statements
    switch (encoding) {
        case 1:
            return new String(byteArray, StandardCharsets.US_ASCII);
        case 2:
            return new String(byteArray, StandardCharsets.UTF_16LE);     
        case 3:
            return new String(byteArray, StandardCharsets.UTF_8);     
        case 4:
            return new String(byteArray, StandardCharsets.UTF_16);     
        default:
            return null;
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    `DataInputStream.read(byte[])` isn't guaranteed to read the entire length of the buffer; see the javadoc. On most systems `FileInputStream` only works on disk files and _usually_ input from disk files does read fully, so this may appear to work when you test it and only fail when delivered to an important customer. – dave_thompson_085 Sep 12 '20 at 01:51