“Cannot find symbol” 在 Java 文件操作中。

huangapple go评论188阅读模式
英文:

"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(&quot;file.txt&quot;);

			
  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&lt;1000)
  {
    temp=rand.nextInt(94)+32;
    znak=(char)temp;
    in.write((int)znak);
    i++;
  }
  i=0;
  in.close();

  outp = new FileOutputStream(&quot;file.txt&quot;);
  while(i&lt;1000)
  {
    znak = (char)outp.read();
    System.out.print(znak);
    i++;
  }
		
	}
	catch(Exception e)
	{
			System.out.print(&quot;Input error&quot;);
			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.

huangapple
  • 本文由 发表于 2020年4月11日 04:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定