【问题标题】:File Handling (binary File with .dat extension) in Java returning unknown compile time errrorJava 中的文件处理(扩展名为 .dat 的二进制文件)返回未知编译时错误
【发布时间】:2020-02-27 13:34:56
【问题描述】:
void abIn()throws IOException
{

    FileOutputStream fos=new FileInputStream("abc.dat");
    DataOutputStream dos = new DataInputStream(fos);
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    for(int i=0;i<5;i++)
    {
        System.out.println("Enter PC, Price Quantity  "+(i+1));
        pc=Integer.parseInt(br.readLine());
        up=Double.parseDouble(br.readLine());
        q=Integer.parseInt(br.readLine());
        dos.writeInt(pc);
        dos.writeDouble(up);
        dos.writeInt(q);
    }
    dos.close();
    fos.close();
}
void abOut()throws IOException
{
    FileInputStream fin=new FileInputStream("ABC.DAT");
    DataInputStream din=new DataInputStream(fin);
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Product Code");
    int x=Integer.parseInt(br.readLine());
    boolean EOF =false;
    while(!EOF)
    {
        try
        {
            int pcx =din.readInt(pc);
            int upx=din.readDouble(up);
            int qx=din.readInt(q);
            if(pcx==x)
            {
                System.out.println(pcx+" "+upx+" "+q);
            }
        }
        catch(EOFException e)
        {}
    }
    din.close();
    fin.close();
}
public static void main()throws IOException
{
    f1 ob=new f1();
    ob.abIn();
    ob.abOut();
}

返回未知的编译时间错误。 我尝试使用 Java 中的产品代码、产品价格和数量(单位)创建一个 dat 文件,然后使用产品代码搜索并获取上述输入的其他详细信息。

【问题讨论】:

  • 请将堆栈跟踪添加到您的问题中。
  • 究竟是什么错误(请复制并粘贴)。
  • 拥有完整的代码会有所帮助。我尝试编译,由于缺少outer class关键字而失败。
  • 实际上我知道我必须使用 FileWriter 而不是 FileOutputStream 但接下来的问题是如何使用 File reader 命令显示。
  • 而事实是外部类是类 abc 一个在它之上的头文件,即导入 java.io.*;

标签: java bluej


【解决方案1】:

有五个编译错误,它们根本不是“未知”的。

f1.java:11: error: incompatible types: FileInputStream cannot be converted to FileOutputStream
    FileOutputStream fos=new FileInputStream("abc.dat");
                         ^
f1.java:12: error: incompatible types: FileOutputStream cannot be converted to InputStream
    DataOutputStream dos = new DataInputStream(fos);
                                               ^
f1.java:39: error: method readInt in class DataInputStream cannot be applied to given types;
            int pcx =din.readInt(pc);
                        ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
f1.java:40: error: method readDouble in class DataInputStream cannot be applied to given types;
            int upx=din.readDouble(up);
                       ^
  required: no arguments
  found: double
  reason: actual and formal argument lists differ in length
f1.java:41: error: method readInt in class DataInputStream cannot be applied to given types;
            int qx=din.readInt(q);
                      ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length

从前两个开始:

如果要创建 FileOutputStream,请创建 FileOutputStream。 DataOutputStream 也是如此。您不能创建一个旨在读取文件的对象,然后告诉它写入文件。这不是它的作用。

所以,你需要改变这些:

FileOutputStream fos=new FileInputStream("abc.dat");
DataOutputStream dos = new DataInputStream(fos);

对这些:

FileOutputStream fos=new FileOutputStream("abc.dat");
DataOutputStream dos = new DataOutputStream(fos);

剩下的错误很清楚。 DataInputStream 方法readIntreadDouble 不接受任何参数。

如果您想从水龙头取水,您不会期望自己提供水只是为了取水。同样,当您要读取整数值时,不应该提供整数值;调用readInt 的全部意义在于您没有整数值,而您希望 DataInputStream 为您提供一个。

所以你不应该将你自己的值传递给一个读取方法。您应该传递零个参数(即,使用空括号调用方法)。

此外,在 Java 中,方法由其名称和参数唯一标识,因此您必须传递与该方法声明的参数完全相同的参数。 readIntreadDouble 被声明为采用零参数,因此您必须传递零参数。

因此,你应该改变这三行:

int pcx =din.readInt(pc);
int upx=din.readDouble(up);
int qx=din.readInt(q);

到这里:

int pcx =din.readInt();
int upx=din.readDouble();
int qx=din.readInt();

最后,你不应该忽略EOFException。该异常意味着没有更多数据要读取,那么为什么还要继续尝试读取数据呢?

正确的做法是停止读取数据:

catch (EOFException e)
{
    break;
}

由于您的 EOF 变量从未使用过,您实际上可以将循环重写为:

try
{
    while (true)
    {
        int pcx = din.readInt();
        int upx = din.readDouble();
        int qx = din.readInt();
        if (pcx == x)
        {
            System.out.println(pcx+" "+upx+" "+q);
        }
    }
}
catch(EOFException e)
{
    System.out.println("No more data.");
}

遇到异常会退出循环。

【讨论】:

    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 2011-09-24
    • 2020-03-29
    • 1970-01-01
    • 2022-08-18
    • 2011-09-18
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多