java - 将字符串写入 Planner 中的文本文件

标签 java string file text

** 我是 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"));

        }





    }

}

最佳答案

您可以使用分隔符(例如 $$$)连接日期、时间、名称等字符串(此分隔符不应出现在名称中,因此请相应地使用分隔符)。

然后

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.

}

关于java - 将字符串写入 Planner 中的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11676093/

相关文章:

java - 编辑文件而不遍历整个字节缓冲区?

java - 按指定值排序对象的优先级队列

python - 如何检查 pandas 中的列是否以 a 或 b 结尾

string - Go 中的模板字符串?

c - Hangman 程序字符串问题

Java上传的文件不一样

c - 如何计算 C 文件中的整数个数?

java - 使用 JPA 条件 API 将条件表达式与 "AND"和 "OR"谓词组合

java - 不兼容 :org. codehaus.plexus。 .exception.ComponentLookupException

java - 与 SINGLE_TABLE 继承的子类的关系