Reputation: 61
I need to to write a Java program that will write entered information in the console to a .txt file. If the .txt file already exists it will have to open it and write the additional information on another line. If the .txt file doesn't exist for a new "Lifter" it will create the .txt and write it in. Basically I don't know how to create a new .txt for each name entered and how to edit it if the person's .txt already exists. How would I go about doing that?
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class MeetPrinter {
public static void getInfo() {
Scanner scanner = new Scanner(System.in);
int question;
String firstName;
String lastName;
int squat;
int bench;
int lift;
String meetName;
do {
System.out.println("Enter first name: ");
firstName = scanner.next();
System.out.println("Enter Last Name: ");
lastName = scanner.next();
System.out.println("Enter new Meet Name: ");
meetName = scanner.next();
System.out.println("Enter max squat weight in kg: ");
squat = scanner.nextInt();
System.out.println("Enter max bench press in kg: ");
bench = scanner.nextInt();
System.out.println("Enter max deadlift in kg: ");
lift = scanner.nextInt();
System.out
.println("Enter '1' to enter more lifters or '2' if you are done entering.");
question = scanner.nextInt();
} while (question == 1);
try{
PrintWriter out = new PrintWriter("output.txt");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100000);
out.println(lastName + ", " + firstName + " Record #: " + randomInt);
out.println("");
String meet = "Meet Name";
String sq = "SQ Max";
String bp = "BP Max";
String sub = "SUB Total";
String dl = "DL Max";
String tot = "Total";
out.printf("%20s %15s %18s %19s %18s %18s",meet ,sq,bp,sub,dl,tot);
out.println("");
out.printf("%20s", meetName);
out.printf("%10d (%6.2f)", squat, squat * 2.2);
out.printf("%10d (%6.2f)", bench, bench * 2.2);
float subPounds = (float) ((float)(squat + bench) * 2.2);
out.printf("%10d (%6.2f)", squat + bench, subPounds);
out.printf("%10d (%6.2f)", lift, lift * 2.2);
float tPounds = (float) ((float)(squat + bench + lift) * 2.2);
out.printf("%10d (%6.2f)", squat + bench + lift, tPounds);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 1769
Reputation: 41200
Java File operation
File file = new File("<file name>");
BufferedWriter writer = null;
if(file.exists()){ //Check existence check
writer = new BufferedWriter(new FileWriter(toFile,true));
//bytes will be written to the end of the file rather than the beginning
}else{
file.createNewFile() //creating new file
writer = new BufferedWriter(new FileWriter(toFile));
}
BufferedWriter writer = new BufferedWriter(writer);
writer.write(...);
---or----
PrintWriter out
= new PrintWriter(new BufferedWriter(writer));
out.println(...);
Upvotes: 1
Reputation: 13063
Update
change
PrintWriter out = new PrintWriter("output.txt");
to
FileWriter out = new FileWriter("output.txt", true);
The true
will tell the FileWriter to append to the file if it exists, which is exactly what you want to do.
here is the javadoc for FileWriter.
The FileWriter has the option to write Strings, which is what you do.
Also like twain249 suggested, do something about the path...
Upvotes: 2