java error with ".write" - java

I'm having a problem here
I am using netbeans!
I am unable to use the .write
Here are my codes:
ModFile=new File(NameText.getText() + ".mod");
if(!ModFile.exists()){
try {
ModFile.createNewFile();
System.out.println("Mod file has been created to the current directory");
ModFile.*write*(CodesBox.getText());
} catch (IOException ex) {
Logger.getLogger(ModMakerGui.class.getName()).log(Level.SEVERE, null, ex);
}
}
This line:
ModFile.*write*(CodesBox.getText());
is giving me problems!
Please help me here

File does not have write method. Please use FileWriter or Print Writer
ModFile=new File(NameText.getText() + ".mod");
FileWriter writer=null;
if(!ModFile.exists()){
try {
ModFile.createNewFile();
writer=new FileWriter(ModFile);
System.out.println("Mod file has been created to the current directory");
writer.write(CodesBox.getText());
} catch (IOException ex) {
Logger.getLogger(ModMakerGui.class.getName()).log(Level.SEVERE, null, ex);
}
}

Related

output stream doesn't make a file

The code below is supposed to create and write to a file, but it doesn't create a file in my directory. Everything with the Scanner is working, it scans everything from jTextField perfectly.
OutputStream os;
try {
os = new FileOutputStream("kurinys.dat");
try (DataOutputStream dos = new DataOutputStream(os)) {
Scanner skanuoklisSaugojimui = new Scanner(jTextField1.getText());
while(skanuoklisSaugojimui.hasNextInt()){
int natosAukstis = skanuoklisSaugojimui.nextInt();
dos.writeInt(natosAukstis);
}
}
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
}
Can someone please help me find the problem?
Don't use a nested try, it doesnt have any sense and could case a lot of problems in terms of exception handling.
OutputStream os;
try {
os = new FileOutputStream("kurinys.dat");
}
catch (FileNotFoundException e) {
}
try (DataOutputStream dos = new DataOutputStream(os)) {
Scanner skanuoklisSaugojimui = new Scanner(jTextField1.getText());
while(skanuoklisSaugojimui.hasNextInt()){
int natosAukstis = skanuoklisSaugojimui.nextInt();
dos.writeInt(natosAukstis);
}
}
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Grafika.class.getName()).log(Level.SEVERE, null, ex);
}

Java Serialization not working, appears broken because of IDE

EDIT: The error was caused by Netbeans not the code. Post has been edited to show all the code since the Git files are being removed.
I have a group project at school to design a tower defense game and I have been attempting to add Serialization.
Here is the code:
public class Serialization implements Serializable {
private static FileOutputStream file;
private static ObjectOutputStream write;
public static boolean checkFile(String name){
boolean check = false;
check = new File("log",name+".ser").isFile();
return check;
}
public static void createFile(String name) {
try {
file = new FileOutputStream("log/"+name+".ser");
} catch (FileNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static ObjectOutputStream openFile(String name) {
try {
file = new FileOutputStream("log/"+name+".ser");
write = new ObjectOutputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
return write;
}
public static void addLine(ObjectOutputStream con,Object data) {
try {
con.writeObject(data);
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void readLine(String name){
FileInputStream fileIn = null;
try {
fileIn = new FileInputStream("log/"+name+".ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
System.out.println(in.readObject());
in.close();
fileIn.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fileIn.close();
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void closeFile(ObjectOutputStream con){
try {
con.close();
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I eventually abandoned it for two reasons. One it was overkill (trying to have a central class that handles multiple serialization files) and two I could not get it to work with what I am about to talk about.
What I really wanted was to serialize the enemyArray. This holds every living enemy in the game and all their information (stats). If you scroll down to line 192 that is what is left of my serialization attempts:
public void serialize(){
if (Serialization.checkFile("test")==false){
Serialization.createFile("test");
}
ObjectOutputStream connection = Serialization.openFile("test");
for (int x=0;x<enemyArray.length;x++){
if (enemyArray[x]!=null){
Serialization.addLine(connection,enemyArray[x]);
}
}
//Serialization.addLine(connection,enemyArray);
Serialization.closeFile(connection);
enemyArray = null;
Serialization.readLine("test");
//System.out.println(enemyArray[0].id);
// try {
// FileOutputStream fileOut = new FileOutputStream("log/test.ser");
// ObjectOutputStream out = new ObjectOutputStream(fileOut);
// out.writeObject(enemyArray);
// out.writeUTF(t);
// // Do work here
// for (int x=0;x<enemyArray.length;x++){
// if (enemyArray[x]!=null){
// Enemy tmp = enemyArray[x];
// System.out.println(tmp+" >>> "+enemyArray[x]);
// out.writeObject(tmp);
// }
// }
// out.close();
// fileOut.close();
// } catch (FileNotFoundException ex) {
// Logger.getLogger(EnemyController.class.getName()).log(Level.SEVERE, null, ex);
// } catch (IOException ex) {
// Logger.getLogger(EnemyController.class.getName()).log(Level.SEVERE, null, ex);
// }
}
}
It will create the file but will not save anything to it. What is wrong with the code? from what I studied enemyArray shouldn't be static so I removed that and still no change.
Try doing a out.flush(); brefore you close thre output stream.
I found the error after some troubleshooting in the computer lab with my group. Hopefully this can help anyone who finds themselves in my situation or in a similar Java file read/ write issue.
My IDE, Netbeans, shows the file in the files and project list once it is dynamically created, but because it was filled with serialized data and was created after the project was opened and run it would only display an empty file when in reality there is actually data inside the file. String data is fine so it may only be certain data types (like serialized objects) that trigger this error.
Several different versions of my code were correct but they all seemed broken because an empty file was returned to the IDE. The solution is two fold:
Manually check it through your file explorer. It will show a size and can be opened in another editor but may still be buggy in the IDE you ran the program in.
Close down your IDE and on reopening, at least in Netbeans case, the file should be updated correctly.
On a similar note Netbeans may just all together keep showing the empty file instead of offering you the option to select the files encoding. Just view the file in your explorer in this case and if the size is greater than 0 you know your code is ok. This is purely an IDE error.

PrintWriter: File not found exception?

String path=this.getClass().getResource("info.txt").toString();
void writeinfo()
{
PrintWriter writer = null;
try {
writer = new PrintWriter(path);
writer.println(highscore);
for(int i=1;i<=30;i++)
{
for(int j=1;j<=30;j++)
{
writer.println(v[i][j]);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
writer.close();
}
}
i just pasted the relevant part of the code. It says "java.io.FileNotFoundException", but the file does exist! I searched online but i find no solution to this.

Java open file error: can't find the specified path

I try to open a .txt file (agenda.txt) from the src/resources folder, read objects from it and add them to an ArrayList, but I get this error: "The system can not find the specified path.". This is the code I use:
public void open(File f) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
Object o;
try {
while ((o = ois.readObject()) != null) {
model.adauga((Abonat) o);
}
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(
this,
ex.getMessage(),
"Clasa...!",
JOptionPane.ERROR_MESSAGE);
return;
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(
this,
ex.getMessage(),
"Eroare deschidere fisier!",
JOptionPane.ERROR_MESSAGE);
return;
} finally {
try {
ois.close();
} catch (IOException ex) {
Logger.getLogger(CarteDeTelefonGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And in the class constructor:
private String path ="resources/agenda.txt";
File f=new File(path);
open(f);
What is wrong in the code?
the file should be located outside src, something like baseproject/resources. Thats because your path is the project base not your sources directory. Or you can change the code to
private String path ="src/resources/agenda.txt";

Printing to multiple output files in java

I have two arrays that I want to print to separate files. Here's my code:
try {
PrintStream out = new PrintStream(new FileOutputStream(
"Edges.txt"));
for (i = 0; i < bcount; i++) {
out.println(b[i][0] + " " + b[i][1]);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
PrintStream out = new PrintStream(new FileOutputStream(
"Nodes.txt"));
for (i = 0; i < bigbIter; i++) {
out.println(bigb[i]);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
If I only use the first set of try / catch / catch, it works perfectly. But when I use both it doesn't work, giving me the errors "illegal start of type ... } catch" and "error: class, interface, or enum expected". What am I doing wrong?
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
You have an extra }, which throws off the parser and gives you lots of errors.
You should write a method to write to the file. Just pass the file name and data. You should see that you have too many closing brackets, get your IDE to highlight brackets.
Lesson is just don't copy/paste and then edit the catch block when you want it again!
Edit: Also in java 7 you can have multiple catches in one block, it is better to do this:
catch (FileNotFoundException | IOException e)
{
}

Categories