⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appendio.java

📁 java 基础的 一点东西,,可以看看
💻 JAVA
字号:
import java.awt.Color;import java.awt.BorderLayout;import java.awt.event.*;import javax.swing.*;import java.io.*;class AppendIO extends JFrame implements ActionListener {   JLabel text;   JButton button;   JPanel panel;   JTextField textField;   private boolean _clickMeMode = true;   AppendIO() { //Begin Constructor     text = new JLabel("Text to save to file:");     button = new JButton("Click Me");     button.addActionListener(this);     textField = new JTextField(30);     panel = new JPanel();     panel.setLayout(new BorderLayout());     panel.setBackground(Color.white);     getContentPane().add(panel);     panel.add(BorderLayout.NORTH, text);     panel.add(BorderLayout.CENTER, textField);     panel.add(BorderLayout.SOUTH, button);   } //End Constructor   public void actionPerformed(ActionEvent event){     Object source = event.getSource();     if (source == button){       String s = null;       if (_clickMeMode){         try {      //Write to file          String text = textField.getText();          byte b[] = text.getBytes();          String outputFileName = System.getProperty("user.home",                                File.separatorChar + "home" +                                 File.separatorChar + "zelda") +                                 File.separatorChar + "text.txt";          File outputFile = new File(outputFileName);          RandomAccessFile out = new RandomAccessFile(outputFile, "rw");          out.seek(outputFile.length());          out.write(b);     //Write a new line (NL) to the file.         out.writeByte('\n');         out.close();    //Read from file         String inputFileName = System.getProperty("user.home",                             File.separatorChar + "home" +                              File.separatorChar + "zelda") +                              File.separatorChar + "text.txt";         File inputFile = new File(inputFileName);         FileInputStream in = new FileInputStream(inputFile);         byte bt[] = new byte[(int)inputFile.length()];         in.read(bt);         s = new String(bt);         in.close();       } catch(java.io.IOException e) {         System.out.println(e.toString());       }//Clear text field       textField.setText("");//Display text read from file        text.setText("Text retrieved from file:");        textField.setText(s);        button.setText("Click Again");        _clickMeMode = false;      } else {//Save text to file        text.setText("Text to save to file:");        textField.setText("");        button.setText("Click Me");        _clickMeMode = true;      }   } }//end action performed method public static void main(String[] args) {   JFrame frame = new AppendIO();   frame.setTitle("Example");   WindowListener l = new WindowAdapter() {   public void windowClosing(WindowEvent e) {       System.exit(0);     }   };   frame.addWindowListener(l);   frame.pack();   frame.setVisible(true);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -