遇到了使用readAllBytes()方法无法正常工作的问题。

huangapple go评论76阅读模式
英文:

Having trouble getting readAllBytes() method to work

问题

其他班上的学生已经让这段代码正常工作了,但我却遇到了一个错误。我正在尝试使用readAllBytes()方法来完成一个作业,但无法使其工作。我的老师说有另一种方法可以使用,但他打算使用的方法在DataInputStream中没有定义。

请查看read(byte[])方法以及数据流中的第一个整数,该整数显示了字节数组的大小。无论哪种方法都可以,但我在第39行遇到了一个错误,如果我删除import java.io.DataInputStream;,可以修复该错误,但是我会在第31行遇到错误。非常感谢任何帮助。

package edu.ics211.h03;

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

/**
 * 代表一个ReadFile(读取文件)。
 * 作者:Constantine Peros
 */
public class ReadFile implements IReadFile {

  /**
   * 创建一个新的ReadFile。
   */
  public ReadFile() {
    // TODO 自动生成的构造函数存根
  }

  @Override
  public String readFile(String fileName) throws IOException {
    // 从文件名创建FileInputStream
    // 从FileInputStream创建DataInputStream
    DataInputStream data = new DataInputStream(new FileInputStream(fileName));
    // 使用readInt读取字节数
    int size = data.readInt();
    // 使用readByte读取编码
    byte encoding = data.readByte();
    // 创建一个长度为字节数的字节数组
    byte[] byteArray = new byte[size];
    // 填充字节数组
    byteArray = data.readAllBytes();
    // 关闭输入流
    data.close();
    // 切换编码或使用if语句
    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;
    }
  }
}
英文:

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;
}
}
}

答案1

得分: 0

似乎你只需要将代码中的这行替换掉:

byteArray = data.readAllBytes();

使用这行:

data.read(byteArray);

以下是整个方法:

public String readFile(String fileName) throws IOException {
    // 从文件名创建一个FileInputStream
    // 从FileInputStream创建一个DataInputStream
    DataInputStream data = new DataInputStream(new FileInputStream(fileName));
    // 使用readInt读取字节数
    int size = data.readInt();
    // 使用readByte读取编码
    byte encoding = data.readByte();
    // 创建一个字节数组,长度为字节数
    byte[] byteArray = new byte[size];
    // 填充字节数组
    data.read(byteArray);
    // 替换掉原来的这行
    // byteArray = data.readAllBytes();
    // 关闭输入流
    data.close();
    // 切换编码或使用if语句
    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;
    }
}
英文:

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;
    }
}

huangapple
  • 本文由 发表于 2020年9月12日 07:46:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63855541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定