Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中返回未知编译时错误的文件处理(扩展名为.dat的二进制文件) void abIn()引发IOException { FileOutputStream fos=新的FileInputStream(“abc.dat”); DataOutputStream dos=新的DataInputStream(fos); BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in)); 对于(inti=0;i_Java_Bluej - Fatal编程技术网

Java中返回未知编译时错误的文件处理(扩展名为.dat的二进制文件) void abIn()引发IOException { FileOutputStream fos=新的FileInputStream(“abc.dat”); DataOutputStream dos=新的DataInputStream(fos); BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in)); 对于(inti=0;i

Java中返回未知编译时错误的文件处理(扩展名为.dat的二进制文件) void abIn()引发IOException { FileOutputStream fos=新的FileInputStream(“abc.dat”); DataOutputStream dos=新的DataInputStream(fos); BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in)); 对于(inti=0;i,java,bluej,Java,Bluej,有五个编译错误,它们根本不是“未知的” 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

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

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();
}
从前两个开始:

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

因此,您需要更改这些:

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 fos=new FileInputStream("abc.dat");
DataOutputStream dos = new DataInputStream(fos);
剩下的错误很明显。DataInputStream方法和不接受任何参数

如果你想从水龙头里取水,你不会期望仅仅为了取水而提供你自己的水。同样,当你想读取一个整数值时,你不应该提供一个整数值;调用
readInt
的关键是你没有整数值,你想让DataInputStream提供给你有一个

因此,您不应该将自己的值传递给read方法,而应该传递零个参数(即,使用空括号调用该方法)

此外,在Java中,方法的名称和参数都是唯一标识的,因此必须传递与该方法声明的参数完全相同的参数。
readInt
readDouble
声明为接受零参数,因此必须传递零参数

因此,您应该更改这三行:

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

int pcx =din.readInt(pc);
int upx=din.readDouble(up);
int qx=din.readInt(q);
最后,您不应该忽略
EOFEException
。该异常意味着没有更多的数据可读取,那么您为什么要继续尝试读取数据呢

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

int pcx =din.readInt();
int upx=din.readDouble();
int qx=din.readInt();
由于从未使用
EOF
变量,因此实际上可以将循环重写为:

catch (EOFException e)
{
    break;
}

遇到异常时,循环将退出。

请将stacktrace添加到您的问题中。具体错误是什么(请复制并粘贴)。这将有助于获得完整的代码。我尝试编译,但由于缺少外部类关键字而失败。事实上,我得到了一个事实,即我必须使用FileWriter而不是FileOutputStream,但接下来的问题是,我将如何使用文件读取器命令显示。事实上,外部类是abc类a头文件on顶部,即导入java.io.*;
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.");
}