0

**私はJavaの初心者であり、プランナーを作成しています。イベントの日付、時刻、名前、説明の文字列を記述し、後でそれを見つけて、イベントに関する情報を表示する必要があります。テキストファイルに書き込んで後で見つけるにはどうすればよいですか?ありがとう!**

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Test {

    /**
     * @throws IOException 
     * @param args
     * @throws 
     */
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner ( System.in );
        String choice;
        String password = null;
        String time = null, name = null, dateview = null, dateedit, description = null, dateadd = null;
        FileWriter ostream;


        JOptionPane.showMessageDialog(null, "Welcome To The Pocket Planner!");
        System.out.println("");
        password = JOptionPane.showInputDialog("Please Enter Your Password");
        while (!password.toUpperCase().equals("PASSWORD"))
        {
            JOptionPane.showMessageDialog(null, "Try Again", "Incorrect Password", JOptionPane.ERROR_MESSAGE);
            password = JOptionPane.showInputDialog("Please Enter Your Password");
        }

        {
            do{
                do{
                    do{

                        choice = JOptionPane.showInputDialog("Type \"View\" To View Your Schedule" + "\n" + "Type \"Edit\" To Edit Your Schedule" + "\n" + "Type \"Add\" To Add To Your Schedule");

                        if (choice.equals("View")){
                            dateview = JOptionPane.showInputDialog("Enter the date you want to view in this format: MM/DD/YY");
                            while(dateadd != null){
                                FileInputStream istream = new FileInputStream("filename.txt");
                                DataInputStream in = new DataInputStream(istream);
                                BufferedReader br = new BufferedReader(new InputStreamReader(in));String strLine;
                                dateadd = br.readLine();
                            if(dateadd.equalsIgnoreCase(dateview)){
                                JOptionPane.showMessageDialog(null, (time + ":" + "\n" + name + " -" + " " + description));
                            }
                            else 
                            {
                                JOptionPane.showMessageDialog(null, "Try Again", "Event Not Found", JOptionPane.WARNING_MESSAGE);
                            }
                            }
                        }
                        else if (choice.equals("Edit")){
                            dateedit = JOptionPane.showInputDialog("Enter the date you want to edit in this format: MM/DD/YY");
                        }
                        else if (choice.equals("Add")) {
                            name = JOptionPane.showInputDialog("Enter the name of the event you want to add.");
                            dateadd = JOptionPane.showInputDialog("Enter the date you want to add in this format: MM/DD/YY");
                            time = JOptionPane.showInputDialog("Enter the time of your event");
                            description = JOptionPane.showInputDialog("Enter the description of your event");

                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Try Again", "Invalid Entry", JOptionPane.ERROR_MESSAGE);   
                        }
                    }while(!choice.toUpperCase().equals("Edit"));
                }while(!choice.toUpperCase().equals("View"));
            }while(!choice.toUpperCase().equals("Add"));

        }





    }

}
4

2 に答える 2

1

長期的には、JavaのXML機能を確認することをお勧めします

を見てみましょう

それまでの間、 http://docs.oracle.com/javase/tutorial/essential/io/を確認することをお勧めします。

于 2012-07-26T19:50:30.167 に答える
1

日付、時刻、名前などの文字列を、$$$などの区切り文字で連結できます(この区切り文字は名前に表示されないため、それに応じて使用してください)。

それで

File file = new File("textfile.txt");
// This is the file in which you want to write all planner entries
output = new BufferedWriter(new FileWriter(file));
output.write(text);

読むには、次のようにファイルを開きます

FileInputStream fstream = new FileInputStream("textfile.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;

while ((strLine = br.readLine()) != null)   { // Until file has content, keep reading
String[] temp;
temp = strLine.split("$$$");

//using string.split() with the delimiter to get back all the sub-strings.

}
于 2012-07-26T21:03:31.250 に答える