[java] Beginner Question

Started by
1 comment, last by sketchasketch 12 years, 9 months ago
This isn't really game related, it's a really easy question for more experienced programmers I'm sure, but, here's my problem, I'm trying to have a text file that updates with an age, the users first name, the users last name, and then a new line. right now, it makes a Formatter to make the file chinese.txt, then it adds the scanner variables plus two records I used to make sure I knew this stuff.

Every time I run it, I want the record to be added to the txt file, and NOT overwritten. Is there a common error I'm making that everyone makes, or is this a specific thing?

I can upload the the src if I need to so I can get help, but I wanted to ask you guys first.


boolean isLearning= true;
boolean isOpenToAdvice= true;


Advertisement
Asuming you use a FileWriter object to write to a file.

instead of using new FileWriter(filename) use new FileWriter(filename, true). This will open the file in append mode. Now just write to it normaly and it will append to the end of the file isntead of overwriting it.

The boolean parameter lets the object know whether to append or overwrite. thus new FileWriter(filename, false) would do exactly the same as new FileWriter(filename). Since overwrite is the default mode.

Asuming you use a FileWriter object to write to a file.

instead of using new FileWriter(filename) use new FileWriter(filename, true). This will open the file in append mode. Now just write to it normaly and it will append to the end of the file isntead of overwriting it.

The boolean parameter lets the object know whether to append or overwrite. thus new FileWriter(filename, false) would do exactly the same as new FileWriter(filename). Since overwrite is the default mode.



[EDIT}
wait, so if I do new File= filename, I'm making a new file every time, and overwriting the old on?
I'm using a formatter.
Here's the code that actually writes to the file.


import java.util.*;
public class coolthing
{
private Formatter x;

int age;
String firstname;
String lastname;
public void everything()
{
Scanner input=new Scanner(System.in);
System.out.println(" Type your age, in years.");
age= input.nextInt();
System.out.println("Type your first name.");
firstname= input.next();
System.out.println("Type your last name.");
lastname=input.next();



}
public void openFile()
{
try
{

x=new Formatter("C:\\Java\\chinese.txt");
}
catch(Exception e)
{
System.out.println("You have an error bro");
}
}
public void addRecords()
{
x.format(age + " " + firstname + " " + lastname );
x.format("\n");
x.format("%s%s%s", "20 ", "herp ", "derp ");
x.format("%s%s%s", "16 ", "derpy ", "herp ");

}
public void closeFile()
{
x.close();
}


public void complete()
{
System.out.println("Process Complete");
}
}

when I read the file, I'm also using

x=new Scanner(new File("C:\\Java\\chinese.txt"));

to find the file, is that wrong?
boolean isLearning= true;
boolean isOpenToAdvice= true;



Asuming you use a FileWriter object to write to a file.

instead of using new FileWriter(filename) use new FileWriter(filename, true). This will open the file in append mode. Now just write to it normaly and it will append to the end of the file isntead of overwriting it.

The boolean parameter lets the object know whether to append or overwrite. thus new FileWriter(filename, false) would do exactly the same as new FileWriter(filename). Since overwrite is the default mode.


I was hardly paying attention when I tried that, but I just re-read it, and it works like a charm, thanks a ton!
boolean isLearning= true;
boolean isOpenToAdvice= true;


This topic is closed to new replies.

Advertisement