K
K
KreMbI42015-02-09 17:46:20
Java
KreMbI4, 2015-02-09 17:46:20

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

3 answer(s)
V
Victor Gogilchin, 2015-02-09
@KreMbI4

x.format("Ваше название %s, Ваше %d число, Ваш комментарий %s", b, a, c);
    x.flush()
    x.close();

The flush() method writes data.
PS. The comments came up with this:
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) { }
}
}

K
Kirill Toklachev, 2015-02-09
@tolkkv

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) { }

T
Takheer, 2015-02-09
@Takheer

FileOutputStream or FileWriter to the rescue

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question