首页 > 代码库 > IO流+数据库课后习题

IO流+数据库课后习题

1,读取 试题文件 然后做题算分

        File file1=new File("D:\\file","test.txt");
        try{
            FileReader in1=new FileReader(file1);
            BufferedReader in2=new BufferedReader(in1);
            String s;
            int count=0;
            for(;(s=in2.readLine())!=null;){
                if(!s.startsWith("-")){
                    System.out.println(s);
                }
                else{
                    System.out.print("input your answer: ");
                    s=s.replaceAll("-","");
                    String daan;
                    Scanner scanner1=new Scanner(System.in);
                    daan=scanner1.next();
                    if(s.equals(daan)){
                        count++;
                    }
                }
            }
            System.out.println("point "+count);
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    

技术分享


2,用卡片布局做两个页面,来输入和输出

技术分享
package testWin;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.CardLayout;

public class TestWin implements ActionListener {
    //使用卡片式布局,由菜单栏调用两个页面
    private JFrame frame;
    File file1;
    JMenuItem input = new JMenuItem("input");
    JMenuItem show = new JMenuItem("show");
    InputArea inputMessage;
    CardLayout card;
    JPanel panel;
    JTextArea textArea;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestWin window = new TestWin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestWin() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        file1=new File("D:\\file","test.txt");
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        
        JMenu menu = new JMenu("\u83DC\u5355");
        menuBar.add(menu);
        
        menu.add(input);
        
        menu.add(show);
        
        input.addActionListener(this);
        show.addActionListener(this);
        
        textArea=new JTextArea(12,50);
        inputMessage=new InputArea(file1);
        card=new CardLayout();
        panel=new JPanel();
        panel.setLayout(card);
        panel.add("input",inputMessage);
        panel.add("show",textArea);
        frame.add(panel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        if(e.getSource()==input){
            card.show(panel, "input");
        }
        else if(e.getSource()==show){
            int number=1;
            textArea.setText(null);
            card.show(panel, "show");
            try{
                RandomAccessFile in=new RandomAccessFile(file1,"r");
                String name=null;
                for(;(name=in.readUTF())!=null;){
                    textArea.append("\n"+"name :"+name);
                    textArea.append("\t"+in.readUTF());
                    textArea.append("\t"+in.readUTF());
                    textArea.append("\n----------------------------------------------------------------------------");
                    number++;
                }
                
                in.close();
            }
            catch(Exception e1){
                System.out.println(e1.getMessage());
                System.out.println("2222");
            }
        }
    }

}
View Code
技术分享
package testWin;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

public class InputArea extends JPanel implements ActionListener{

    /**
     * Create the panel.
     */
    File file1;
    RandomAccessFile out1;//运用盒式布局,然后用随机流输入到文件
    Box baseBox,box1,box2;
    JButton button1;
    private JLabel lblNewLabel;
    private JLabel lblNewLabel_1;
    private JLabel lblNewLabel_2;
    private JLabel lblNewLabel_3;
    private JTextField text1;
    private JTextField text2;
    private JTextField text3;
    private JButton button;
    public InputArea(File f) {
        setForeground(Color.CYAN);
        this.file1=f;
        baseBox=Box.createHorizontalBox();
        box1=Box.createVerticalBox();
        box2=Box.createVerticalBox();
        baseBox.add(box1);
        
        lblNewLabel = new JLabel("\u8F93\u5165\u59D3\u540D");
        box1.add(lblNewLabel);
        
        lblNewLabel_1 = new JLabel("\u8F93\u5165qq");
        box1.add(lblNewLabel_1);
        
        lblNewLabel_2 = new JLabel("\u8F93\u5165\u7535\u8BDD");
        box1.add(lblNewLabel_2);
        
        lblNewLabel_3 = new JLabel("\u5355\u51FB\u5F55\u5165");
        box1.add(lblNewLabel_3);
        baseBox.add(box2);
        
        text1 = new JTextField();
        box2.add(text1);
        text1.setColumns(10);
        
        text2 = new JTextField();
        box2.add(text2);
        text2.setColumns(10);
        
        text3 = new JTextField();
        box2.add(text3);
        text3.setColumns(10);
        
        button = new JButton("\u5F55\u5165");
        box2.add(button);
        add(baseBox);
        button.addActionListener(this);
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        try{
            RandomAccessFile out1=new RandomAccessFile(file1,"rw");
            long length=file1.length();
            out1.seek(length);
            out1.writeUTF("姓名 : "+text1.getText());
            out1.writeUTF("qq : "+text2.getText());
            out1.writeUTF("电话 : "+text3.getText());
            out1.close();
            text1.setText(null);
            text2.setText(null);
            text3.setText(null);
        }
        catch(Exception e1){
            System.out.println(e1.getMessage());
        }
    }
    
}
View Code

技术分享

技术分享

 

 

3

IO流+数据库课后习题