0

I am trying to write the information into a textfile after the user has input all their fields. My text file have a already some information in it.

Anthony Ducan;anthony;a123;55 Peter Street;3321444;VISA;3213504011223 Barry Blake;barry;a999;456 George Street;23239876;VISA;435677779876 Claire Rerg;clare;c678;925 Edward Lane;67893344;MASTERCARD;223344556677

I want to enter them into as a string into my textfile, and each line has multiple items seprated by ;.

Do I need to open my file , read what it contains before adding new information , or I can just simply add into it?

I have set up a constructor for inputting values, but how do I use it? Do i need to make a new method for my main to call it?

import java.io.*;
import java.util.*;

public class newCust{

    String name,username,password,address,contact,creditType,creditNum;
    Scanner input = new Scanner(System.in);

    public newCust(String name , String username, String password, String address, String contact, String creditType, String creditNum){

    this.name= name;
    this.username= username;
    this.password = password;
    this.address = address;
    this.contact = contact;
    this.creditType = creditType;
    this.creditNum = creditNum;
    System.out.println("Welcome to Kreg Hotel Booking System");
    System.out.println("==============================================");
    System.out.println("Please enter your name: ");
    name = input.nextLine();
    System.out.println("Please enter your login username: ");
    username = input.nextLine();
    System.out.println("Please enter your login password: ");
    password = input.nextLine();
    System.out.println("Please enter your address: ");
    address = input.nextLine();
    System.out.println("Please enter your contact: ");
    contact = input.nextLine();
    System.out.println("Please enter your credit card type: ");
    creditType = input.nextLine();
    System.out.println("Please enter your credit card number: ");
    creditNum = input.nextLine();


        try{
            PrintWriter fileout = new PrintWriter("customerinfo");
            newCust info = new newCust(name,username,password,address,contact,creditType,creditNum);
            fileout.print(info);

        }
        catch(IOException ex){

        }


  }

    public static void main(String[] args){

    }


}
Tony
  • 3,269
  • 1
  • 27
  • 48
Heng Aik Hwee
  • 159
  • 1
  • 4
  • 13

2 Answers2

2

Do i need to open my file , read what it contains before adding new information , or i can just simply add into it?

You can append to a file, but you can't insert characters or lines in the middle of it.

You can append to it by using for instance new FileWriter(file, true).

If this is production code and not simply an exercise for you, I recommend you to look into OpenCSV.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
0

To write to a file, you use FileOutputStream.

Check if there is any constructor or methods that allows you to open the file appending to it instead of overwritting

assylias
  • 321,522
  • 82
  • 660
  • 783
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • By the way, that kind of files are called CSV `Comma Separated Values`. There already libraries to deal with them. – SJuan76 Jul 29 '12 at 15:10