英文:
"Cannot find symbol" in Java file operations
问题
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class Main {
    public static void main(String[] args) {
        try {
            FileInputStream in = null;
            FileOutputStream outp = null;
            in = new FileInputStream("file.txt");
            Random rand = new Random();
            int x = rand.nextInt(9);
            int guess;
            int count = 0;
            Scanner input = new Scanner(System.in);
            int temp;
            char znak;
            int i = 0;
            while (i < 1000) {
                temp = rand.nextInt(94) + 32;
                znak = (char) temp;
                in.write((int) znak);
                i++;
            }
            i = 0;
            in.close();
            outp = new FileOutputStream("file.txt");
            while (i < 1000) {
                znak = (char) outp.read();
                System.out.print(znak);
                i++;
            }
        } catch (Exception e) {
            System.out.print("Input error");
            return;
        }
    }
}
出现问题的可能原因是令人困惑的文件流使用。在代码的后半部分,你试图从 outp (FileOutputStream类型)中读取数据,但是 FileOutputStream 并没有提供 read() 方法。类似地,在之前的部分,你试图向 in (FileInputStream类型)中写入数据,但是 FileInputStream 也没有提供 write() 方法。
如果你想在文件中写入数据,你应该使用 FileOutputStream,如果你想从文件中读取数据,你应该使用 FileInputStream。此外,注意在读取数据之前,你需要先将数据写入文件。
希望这些解释对你有所帮助。如果你还有其他问题,请随时问我。
英文:
I have the following code:
import java.util.Random; 
import java.util.Scanner;  
import java.io.*;
public class Main
{
public static void main(String[] args)
{
	try
	{
  FileInputStream in = null;
  FileOutputStream outp = null;
  in = new FileInputStream("file.txt");
			
  Random rand = new Random(); 
  int x = rand.nextInt(9);
  int guess; 
  int count=0;
  Scanner input = new Scanner(System.in);
  int temp;
  char znak;
  int i=0;
  while(i<1000)
  {
    temp=rand.nextInt(94)+32;
    znak=(char)temp;
    in.write((int)znak);
    i++;
  }
  i=0;
  in.close();
  outp = new FileOutputStream("file.txt");
  while(i<1000)
  {
    znak = (char)outp.read();
    System.out.print(znak);
    i++;
  }
		
	}
	catch(Exception e)
	{
			System.out.print("Input error");
			return;
	}
}
}
I apologise for pasting it all in instead of only the relevant parts, but I'm still new to Java and can't pinpoint where exactly the problem lies. When I attempt to compile it, I get the following errors:
Main.java:31: error: cannot find symbol
        in.write((int)znak);
          ^
  symbol:   method write(int)
  location: variable in of type FileInputStream
  Main.java:40: error: cannot find symbol
        znak = (char)outp.read();
                         ^
  symbol:   method read()
  location: variable outp of type FileOutputStream
What could be causing the problem here? From what I've gathered, these are normally returned when I try to use an undefined variable, but I do define them before.
答案1
得分: 3
InputStream不可写,OutputStream不可读。您希望执行的操作顺序是相反的。
英文:
InputStreams aren't writable, and OutputStreams aren't readable.  You have the order of operations you want to do backwards.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论