4

以下代码给出了存储charbyte. 如何将fin.read()函数读取的字节存储到字节数组中并打印出来?

import java.io.FileInputStream;

class IO {
    public static void main(String args[]) {
        int i;
        int j = 0;
        FileInputStream fin = new FileInputStream("file.txt");
        byte[] b = new byte[100];
        do {
            i = fin.read();
            j++;
            b[j] = (char) i;
        } while (i != -1);
        System.out.println(b);
    }
}

输出:

possible loss of precision
found   : char
required: byte
            b[j] =(char) i;
                  ^
1 error

我如何使它工作?将文件读入字节数组然后显示?

4

5 回答 5

2

您可以使用InputStream.read(byte [])直接读入字节数组

于 2012-11-10T21:07:56.533 回答
1

将以下行更改b[j] = (char) i;b[j] = (byte) i;.

它会给你另一个编译器错误。但第一个问题将得到解决。

第一个不起作用,因为 Java 使用 Unicode 作为char类型,所以它有两个字节。您将int值转换为,char但随后尝试将其分配给byte变量,为此您必须显式转换为byte,这既不是 C 也不是 C++。

您还可以考虑使用更直接的方法fin.read(b),在您的情况下最多读取100字节并在达到 EOF 时返回 -1。这样您就不必i显式转换为byte.

例如像这样:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

class IO {
    public static void main(String args[]) {
        byte[] b = new byte[100];
        try (FileInputStream fin = new FileInputStream("file.txt");) {
            while (true) {
                int i = fin.read(b);
                if (i < 0) {
                    break;
                }
                if (i < b.length) {
                    b = Arrays.copyOf(b, i);
                }
                System.out.println(Arrays.toString(b));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2012-11-10T21:05:14.773 回答
1

读取文件并打印:

    File file = new File("C:\\MyFile.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

      // this statement reads the line from the file and print it to
        // the console.
        System.out.println(dis.readLine());
      }

      // dispose all the resources after using them, you need to move this to the finally block and check for null!!
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
于 2012-11-10T21:06:02.013 回答
1

尝试使用Apache Commons IOFileUtils.readFileToByteArray(file)中的方法。这比手动编写代码更简单。

于 2012-11-10T21:07:14.350 回答
1

对于您的问题,答案很简单。您正在将 int 转换为 char 但您想将其存储为字节。因此,您必须在对话期间将其转换为字节。IE

import java.io.*;
class IO{
public static void main(String args[]){
    int i;
    int j=0;
    FileInputStream fin = new FileInputStream("file.txt");
    byte[] b = new byte[100];
    do{
        i= fin.read();
        j++;
        b[j] =(byte) i;
    }while( i != -1);
    System.out.println(b);
}

}

此外,您不需要将其读取为 int。直接读取为字符或字节。

于 2012-11-10T21:18:01.497 回答