Reputation: 13
I've basically copied the below code from a text book. I've had this kind of error before and managed to fix it but because I'm not familiar with the Classes & Methods used I am having a bit a trouble with this one.
Below is the error thrown by the compiler.
TextReader.java:27: error: cannot find symbol output = new BufferedOutputStream(filePath.newOutputStream());
symbol: method newOutputStream()
location: variable filePath of type Path
Below is the code. It is basically supposed to get input from a user write it to a text file then read the text file and display the info to the user.
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);
}
}
}
Upvotes: 1
Views: 1596
Reputation: 3279
Path
doesn't have a method newOutputStream()
. I usually use new FileOutputStream(File)
for writing to files, though there might be newer API in Java 7.
You should really use an IDE, e.g. Ecplise or Netbeans, as it will instantly tell you that the method doesn't exist, and will make writing code a thousand times easier in general. For example you can press ctrl+space in Eclipse and it will bring up a list of classes/methods/variables that match the last word you typed (the list will also automatically pop up when typing a period).
Upvotes: 1