gpt4 book ai didi

java - '错误: cannot find symbol' Compiler Error

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:17 25 4
gpt4 key购买 nike

我基本上是从教科书中复制了以下代码。我以前遇到过这种错误并设法修复它,但因为我不熟悉所使用的类和方法,所以我在这个问题上遇到了一些麻烦。

下面是编译器抛出的错误。

TextReader.java:27: error: cannot find symbol output = new BufferedOutputStream(filePath.newOutputStream());
symbol: method newOutputStream()
location: variable filePath of type Path

下面是代码。它基本上应该从用户那里获取输入并将其写入文本文件,然后读取文本文件并向用户显示信息。

import java.nio.file.*;
import static java.nio.file.StandardOpenOption.*;
import java.io.*;
import javax.swing.JOptionPane;
public class TextReader
{
public static void main (String[]args)
{
Path filePath = Paths.get("Message.txt");
String s = JOptionPane.showInputDialog(null,"Enter text to save as a file","Text File Creator",JOptionPane.INFORMATION_MESSAGE);
byte[] data = s.getBytes();
OutputStream output = null;
InputStream input = null;
try
{
output = new BufferedOutputStream(filePath.newOutputStream());
output.write(data);
output.flush();
output.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Message: " + e,"Error!!",JOptionPane.WARNING_MESSAGE);
}
try
{
input = filePath.newInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String ss = null;
ss = reader.readLine();
JOptionPane.showMessageDialog(null,"Below is the information from the saved file:\n" + ss,"Reader Output",JOptionPane.INFORMATION_MESSAGE);
input.close();
}
catch (IOException ee)
{
JOptionPane.showMessageDialog(null,"Message: " + ee,"Error!!",JOptionPane.WARNING_MESSAGE);
}
}
}

最佳答案

Path 没有方法 newOutputStream()。我通常使用 new FileOutputStream(File) 来写入文件,尽管 Java 7 中可能有更新的 API。

你真的应该使用 IDE,例如Ecplise 或 Netbeans,因为它会立即告诉您该方法不存在,并且通常会使编写代码变得容易一千倍。例如,您可以在 Eclipse 中按 ctrl+space,它将显示与您输入的最后一个单词相匹配的类/方法/变量的列表(当输入句点时,该列表也会自动弹出)。

关于java - '错误: cannot find symbol' Compiler Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20895090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com