Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为文件输入和输出到JTextArea创建适当的数组_Java_Arrays_Jtextarea_Objectinputstream_Objectoutputstream - Fatal编程技术网

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

Java 为文件输入和输出到JTextArea创建适当的数组,java,arrays,jtextarea,objectinputstream,objectoutputstream,Java,Arrays,Jtextarea,Objectinputstream,Objectoutputstream,因此,我不仅对java非常陌生,而且对一般编程也非常陌生。说到这里,我正试图编写一个程序,创建一个基于日历的便笺,这样,根据选择的日期,您可以制作一个便笺,并根据月份和年份将其放入一个文件中,然后每天检索该便笺和任何其他便笺。这必须以某种方式使用一个数组,我不知道如何完全实现它,因为我完全不知道如何使用数组(教程没有帮助)。这就是我到目前为止的情况 首先是UserInterface.java文件: import java.awt.*; import javax.swing.*; import

因此,我不仅对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);

   }
   }


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



 }
}
public class CalendarTest {

public static void main(String[] args) {

   UserInterface calendar = new UserInterface();


}
}
然后是CalendarManager.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);

   }
   }


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



 }
}
public class CalendarTest {

public static void main(String[] args) {

   UserInterface calendar = new UserInterface();


}
}

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

您没有描述您的确切问题是什么,但快速检查后,我猜文件没有保存。尝试
stringfilename=month+““+year+”.dat”;

使用下划线而不是空格。

您在这里面临的确切问题是什么?您在哪里需要帮助?您的“retrieve”方法应该返回一个数组,而不是字符串,然后根据月份的日期,从那里将当天的文本加载到JTextArea。每次更改日期时,您都需要从JTextArea获取值,在更新新日期的文本之前将值放入数组中的正确位置。此外,JTextArea是一个多行组件,这意味着你需要考虑到这样一个事实,即任何给定的条目在你的文件中都可能有多行……因此,使用代码示例,我将如何将JTextArea中的行保存到数组中,然后使用另一个代码示例,我将如何将其恢复,因为我认为这可能是我需要知道的全部操作,或者我错了,需要更多的帮助。1-你要吸引一些负面的注意,这应该是一个评论,是的,我知道你没有代表,但它是;2-为什么在文件名中添加“u”会有任何不同?文件确实会保存,尽管实际的保存方式应该是创建一个新目录,但我不知道如何做。