0

ユーザーがすべてのフィールドに入力した後、情報をテキストファイルに書き込もうとしています。私のテキスト ファイルには、既にいくつかの情報が含まれています。

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;22334456

それらをテキストファイルに文字列として入力したいのですが、各行には で区切られた複数の項目があり;ます。

新しい情報を追加する前に、ファイルを開いて内容を読む必要がありますか?

値を入力するためのコンストラクターをセットアップしましたが、どのように使用すればよいですか? メインが呼び出す新しいメソッドを作成する必要がありますか?

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

    }


}
4

2 に答える 2

2

新しい情報を追加する前に、ファイルを開いて内容を読む必要がありますか?

ファイルに追加することはできますが、ファイルの途中に文字や行を挿入することはできません。

たとえば を使用して追加できますnew FileWriter(file, true)

これが本番コードであり、単なる演習ではない場合は、OpenCSVを調べることをお勧めします。

于 2012-07-29T15:07:51.607 に答える
0

ファイルに書き込むには、FileOutputStream.

上書きするのではなく、ファイルに追加してファイルを開くことができるコンストラクターまたはメソッドがあるかどうかを確認します

于 2012-07-29T15:09:00.773 に答える