Answer the question
In order to leave comments, you need to log in
Adding entry to .txt in Java?
The program writes to the 1.txt file, but does not save.
During a new launch, everything in the 1.txt file is erased, and during execution everything is OK. Tell me how to implement a mini database in a text file? To record and ask for a new one, and when closing, all records were saved.
p.s. I'm new to this. I've already covered everything! (I hope I'm asking clearly).
public class Main {
static Formatter x;
static Scanner scn;
public static void main(String[] args) {
try{
x = new Formatter("src//1.txt");
scn = new Scanner(System.in);
System.out.println("Число?");
int a = (int)Double.parseDouble(scn.next());
System.out.println("Название?");
String b = scn.next();
System.out.println("Комментарий?");
String c = scn.next();
x.format("Ваше название %s, Ваше %d число, Ваш комментарий %s", b, a, c);
x.close();
}catch (Exception e){}
}
}
Answer the question
In order to leave comments, you need to log in
x.format("Ваше название %s, Ваше %d число, Ваш комментарий %s", b, a, c);
x.flush()
x.close();
package tk.lslayer.temp;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Formatter;
import java.util.Scanner;
public class Main {
static Formatter x;
static Scanner scn;
public static void main(String[] args) {
try{
x = new Formatter(new BufferedWriter(new FileWriter("src//1.txt", true)));
scn = new Scanner(System.in);
System.out.println("Число?");
int a = (int)Double.parseDouble(scn.next());
System.out.println("Название?");
String b = scn.next();
System.out.println("Комментарий?");
String c = scn.next();
x.format("Ваше название %s, Ваше %d число, Ваш комментарий %s", b, a, c);
x.flush();
x.close();
} catch (Exception e) { }
}
}
If you are using Java 7+, then I advise you to use the try-with-resource construct
Automatically correctly and safely closes a file or another Closeble object.
try(Formatter x = new Formatter(new BufferedWriter(new FileWriter("src//1.txt", true)))){
Scanner scn = new Scanner(System.in);
System.out.println("Число?");
int a = (int)Double.parseDouble(scn.next());
System.out.println("Название?");
String b = scn.next();
System.out.println("Комментарий?");
String c = scn.next();
x.format("Ваше название %s, Ваше %d число, Ваш комментарий %s", b, a, c);
} catch (Exception e) { }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question