为文件输入和输出创建适当的数组到JTextArea

时间:2015-06-13 21:55:52

标签: java arrays jtextarea objectinputstream objectoutputstream

所以我不仅对java而且对一般的编程都很新。说到这一点,我正在尝试编写一个程序,创建一个基于日历的笔记,这取决于所选的日期,你可以做一个笔记,并根据月份和年份将其放入一个文件,然后检索该和其他任何注释一天一天。这必须以某种方式使用数组,我不知道如何完全实现完全混淆如何使用数组(教程没有帮助)。所以这就是我到目前为止所做的。

首先是UserInterface.java文件:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class UserInterface extends JFrame implements ActionListener {

private String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
private String[] days = {"1", "2", "3", "4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};         
private JButton save, retrieve;
private JTextField year;
private JTextArea entry;
private JComboBox month = new JComboBox(months);
private JComboBox day = new JComboBox(days);

public UserInterface() {


    JPanel mPanel = new JPanel(new GridLayout(2,1));
    mPanel.add(new JLabel("Month"));
    mPanel.add(month);
    month.addActionListener(this);

    JPanel dPanel = new JPanel(new GridLayout(2,1));
    dPanel.add(new JLabel("Day"));
    dPanel.add(day);
    month.addActionListener(this);

    JPanel yPanel = new JPanel(new GridLayout(2,1));
    yPanel.add(new JLabel("Year"));
    yPanel.add(year = new JTextField(4));
    month.addActionListener(this);

    JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 10));
    p1.add(new JLabel("Set Date for Entry:"));
    p1.add(mPanel);
    p1.add(dPanel);
    p1.add(yPanel);

    JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 60, 10));
    p2.add(save = new JButton("Save"));
    save.addActionListener(this);
    p2.add(retrieve = new JButton("Retrieve"));
    retrieve.addActionListener(this);


    JPanel full = new JPanel(new BorderLayout());
    full.add(p1, BorderLayout.NORTH);
    full.add(p2, BorderLayout.SOUTH);
    full.add(entry = new JTextArea(10, 10), BorderLayout.CENTER);


    JFrame frame = new JFrame();
    frame.setTitle("Calendar Manager");
    frame.setSize(500, 400);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable( false );
    frame.setVisible(true);
    frame.add(full);
}

public void actionPerformed(ActionEvent e) {
     if(e.getActionCommand().equals("Save")) {

         String m = (String)month.getSelectedItem();

         String d = (String)day.getSelectedItem();
         String y = year.getText();
         String data = entry.getText();

           CalendarManager.save(m, d, y, data);
           entry.setText("Data written successfully to file with name "+ month+" "+year+".txt");

       } else if(e.getActionCommand().equals("Retrieve")){



           String m = (String)month.getSelectedItem();

           String d = (String)day.getSelectedItem();
           String y = year.getText();
           String data = "";

           String result = CalendarManager.retrieve(m, d, y, data);
           entry.setText(result);

   }
   }


}

然后是CalendarManager.java文件:

import java.io.*;


public class CalendarManager {


private String[] calObject = new String[31];
   public static boolean save(String month, String day, String year, String data) {
   String fileName = month+" "+year+".dat";
   int daynum = Integer.parseInt(day);
   try {
       File file = new File(fileName);
       if(!file.exists()) {
           file.createNewFile();
       } 
       ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file));
       for(int i=0; i<31; i++){

              output.writeUTF(month+"-"+day+"-"+year+":  "+data);


       }


   } catch (IOException e) {
       e.printStackTrace();
   }

   return true;
   }






public static String retrieve(String month, String day, String year, String data) {
   String fileName = month+" "+year+".dat";
   int daynum = Integer.parseInt(day);
   try {
       File file = new File(fileName);
       if(!file.exists()) {
           return "File not found";
       } 
       ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
       for(int i=0; i<31; i++){
          if(input == null){
              return "Entry not found";
          }
          else{

             data = input.readUTF();

          }
       }


   } catch (IOException e) {
       e.printStackTrace();
   }
return data;



 }
}

然后最后是CalendarTest.java文件:

public class CalendarTest {

public static void main(String[] args) {

   UserInterface calendar = new UserInterface();


}
}

如果有人能告诉我我做错了什么,而且我的代码必须尽可能简单,没有任何我可能没学过的东西,所以想想初学者的java类材料。

1 个答案:

答案 0 :(得分:0)

您没有描述您的确切问题,但经过快速检查后我猜该文件未保存。尝试使用下划线而不是空格String fileName = month+"_"+year+".dat";