为什么在文件输入/输出的java程序中会出现此错误?

为什么在文件输入/输出的java程序中会出现此错误?,java,file,input,output,Java,File,Input,Output,我无法理解为什么会出现这种错误 我不太明白这个话题。我之所以这么做,是因为这是我在大学里提交的实践材料 输出中显示的错误: >PS E:\Vedang\Java_Package> cd "e:\Vedang\Java_Package\" ; if ($?) { javac FileRandomIntegers.java } ; if ($?) { java FileRandomIntegers } >FileRandomIntegers.java:9: e

我无法理解为什么会出现这种错误

我不太明白这个话题。我之所以这么做,是因为这是我在大学里提交的实践材料

输出中显示的错误:

>PS E:\Vedang\Java_Package> cd "e:\Vedang\Java_Package\" ; if ($?) { javac FileRandomIntegers.java } ; if ($?) { java FileRandomIntegers }

>FileRandomIntegers.java:9: error: constructor File in class File cannot be applied to given types;
          File inFile = new File("rand.dat");  
                        ^
 > required: no arguments
 > found:    String
 > reason: actual and formal argument lists differ in length

>FileRandomIntegers.java:13: error: no suitable constructor found for FileWriter(File)
               FW = new FileWriter(inFile);
                    ^
 >    constructor FileWriter.FileWriter(String) is not applicable
      (argument mismatch; File cannot be converted to String)
 >    constructor FileWriter.FileWriter(java.io.File) is not applicable
      (argument mismatch; File cannot be converted to java.io.File)
 >    constructor FileWriter.FileWriter(FileDescriptor) is not applicable
      (argument mismatch; File cannot be converted to FileDescriptor)

>FileRandomIntegers.java:39: error: no suitable constructor found for Scanner(File)
               Scanner scanner = new Scanner(inFile);
                                 ^
>     constructor Scanner.Scanner(Readable) is not applicable
      (argument mismatch; File cannot be converted to Readable)
>     constructor Scanner.Scanner(InputStream) is not applicable
      (argument mismatch; File cannot be converted to InputStream)
>     constructor Scanner.Scanner(java.io.File) is not applicable
      (argument mismatch; File cannot be converted to java.io.File)
>     constructor Scanner.Scanner(Path) is not applicable
      (argument mismatch; File cannot be converted to Path)
> >    constructor Scanner.Scanner(String) is not applicable
      (argument mismatch; File cannot be converted to String)
> >    constructor Scanner.Scanner(ReadableByteChannel) is not applicable
      (argument mismatch; File cannot be converted to ReadableByteChannel)

>Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors

代码:

import java.io.*;
导入java.util.*;
类FileRandomInteger
{  
公共静态void main(字符串参数[])
{  
FileWriter FW=null;
BufferedWriter bfwr=null;
文件填充=新文件(“rand.dat”);
尝试
{  
国际和国际;
FW=新文件编写器(infle);
bfwr=新的缓冲写入程序(FW);
随机生成随机=新随机();
System.out.println(“\n生成并存储0到99之间的随机整数..\n”);

对于(int i=0;i,以下是我的一项测试的结果

Generating & Storing 10 Random integers from 0 to 99.
86 33 63 1 36 97 20 82 25 9 

Retrieving Random integers stored in file random.dat
86 33 63 1 36 97 20 82 25 9 
我把你的代码分解成更简单的方法,我可以单独测试

我还使用了
BufferedReader
来读取文件,因为您使用了
BufferedWriter
来写入文件

这是我使用的完整的可运行代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class FileRandomIntegers {

    public static void main(String[] args) {
        new FileRandomIntegers().createAndProcessFile();
    }
    
    private Random random;
    
    public void createAndProcessFile() {
        this.random = new Random();
        File file = new File("random.dat");
        try {
            writeFile(file);
            readFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void writeFile(File file) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        System.out.println("Generating & Storing 10 Random integers "
                + "from 0 to 99.");
        for (int index = 0; index < 10; index++) {
            int number = random.nextInt(100);
            System.out.print(number + " ");
            writer.write(Integer.toString(number));
            writer.write(System.lineSeparator());
        }
        System.out.println();
        System.out.println();
        writer.flush();
        writer.close();
    }
    
    public void readFile(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
         System.out.println("Retrieving Random integers stored in file " + 
                 file.getName());  
        String line = reader.readLine();
        while (line != null) {
            System.out.print(line + " ");
            line = reader.readLine();
        }
        System.out.println();
        System.out.println();
        reader.close();
    }

}
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.Random;
公共类FileRandomInteger{
公共静态void main(字符串[]args){
新的FileRandomIntegers().createAndProcessFile();
}
私有随机;
public void createAndProcessFile(){
this.random=新的random();
File File=新文件(“random.dat”);
试一试{
可写文件(文件);
读取文件(文件);
}捕获(IOE异常){
e、 printStackTrace();
}
}
public void writeFile(文件)引发IOException{
BufferedWriter writer=新的BufferedWriter(新文件编写器(文件));
System.out.println(“生成并存储10个随机整数”
+“从0到99。”);
对于(int-index=0;index<10;index++){
整数=random.nextInt(100);
系统输出打印(数字+“”);
write.write(Integer.toString(number));
writer.write(System.lineSeparator());
}
System.out.println();
System.out.println();
writer.flush();
writer.close();
}
公共void readFile(文件文件)引发IOException{
BufferedReader reader=新的BufferedReader(新文件读取器(文件));
System.out.println(“检索存储在文件中的随机整数”+
getName());
字符串行=reader.readLine();
while(行!=null){
系统输出打印(行+“”);
line=reader.readLine();
}
System.out.println();
System.out.println();
reader.close();
}
}

是否有名为
File
的自定义类?
(参数不匹配;文件无法转换为java.io.File)
表示存在不相关的,但这很有趣:
FW=new FileWriter(infle)我也会考虑使用标准的java命名约定。这里是我从哪里得到解决方案的链接。但是这个代码给了我错误。我不理解这个文件输入/输出的主题。谢谢你的回应。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class FileRandomIntegers {

    public static void main(String[] args) {
        new FileRandomIntegers().createAndProcessFile();
    }
    
    private Random random;
    
    public void createAndProcessFile() {
        this.random = new Random();
        File file = new File("random.dat");
        try {
            writeFile(file);
            readFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void writeFile(File file) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        System.out.println("Generating & Storing 10 Random integers "
                + "from 0 to 99.");
        for (int index = 0; index < 10; index++) {
            int number = random.nextInt(100);
            System.out.print(number + " ");
            writer.write(Integer.toString(number));
            writer.write(System.lineSeparator());
        }
        System.out.println();
        System.out.println();
        writer.flush();
        writer.close();
    }
    
    public void readFile(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
         System.out.println("Retrieving Random integers stored in file " + 
                 file.getName());  
        String line = reader.readLine();
        while (line != null) {
            System.out.print(line + " ");
            line = reader.readLine();
        }
        System.out.println();
        System.out.println();
        reader.close();
    }

}