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");
}
}
} }
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());
}
} }
3,编译器(编译可以,链接有bug
主类
package testWin; import java.awt.BorderLayout;
import java.awt.EventQueue; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField; public class TestFrame extends JFrame implements ActionListener{ private JPanel contentPane;
private JTextField text1;
private JTextField text2;
JButton button1 = new JButton("\u7528\u8BB0\u4E8B\u672C\u7F16\u8F91\u6E90\u6587\u4EF6");
JButton button2 = new JButton("\u7F16\u8BD1");
JButton button3 = new JButton("\u8FD0\u884C");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 869, 111);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); contentPane.add(button1); JLabel label = new JLabel(" \u8F93\u5165\u6587\u4EF6\u540D \uFF1A");
contentPane.add(label); text1 = new JTextField();
contentPane.add(text1);
text1.setColumns(15); contentPane.add(button2); JLabel label_1 = new JLabel(" \u8F93\u5165\u4E3B\u7C7B\u540D\uFF1A");
contentPane.add(label_1); text2 = new JTextField();
contentPane.add(text2);
text2.setColumns(13); contentPane.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getSource()==button1){
System.out.println("dd");
Runtime ce=Runtime.getRuntime();
File file1=new File("D:\\javaWorkSpace\\Test\\src\\test22","test2.java");
// File file1=new File("D:\\file","test.java");
try{
ce.exec(file1.getAbsolutePath());
}
catch(Exception e1){
System.out.println(e1.getMessage());
}
}
else if(e.getSource()==button2){
CompileDialog compileDialog=new CompileDialog();
String name=text1.getText();
compileDialog.compile(name);
compileDialog.setVisible(true);
}
else if(e.getSource()==button3){
RunDialog runDialog=new RunDialog();
String name=text2.getText();
runDialog.run(name);
runDialog.setVisible(true);
}
} }
编译类
package testWin; import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea; public class CompileDialog extends JDialog { private final JPanel contentPanel = new JPanel();
JTextArea textArea = new JTextArea(14,70);
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
CompileDialog dialog = new CompileDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the dialog.
*/
public CompileDialog() {
setBounds(100, 100, 871, 502);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane);
{
scrollPane.setViewportView(textArea);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
} public void compile(String name){
try{
Runtime ce=Runtime.getRuntime();
Process proccess =ce.exec("javac "+name);
InputStream in1=proccess.getErrorStream();
BufferedInputStream in2=new BufferedInputStream(in1);
int n;
boolean bn=true;
byte error[]=new byte[100];
for(;(n=in2.read(error,0,100))!=-1;){
String s=null;
s=new String(error,0,n);
textArea.append(s);
if(s!=null)bn=false;
}
if(bn)
textArea.append("编译正确");
}
catch(IOException e2){
System.out.println(e2.getMessage());
}
}
}
运行类
package testWin; import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import javax.swing.JScrollPane;
import javax.swing.JTextArea; public class RunDialog extends JDialog { private final JPanel contentPanel = new JPanel();
JTextArea textArea = new JTextArea(14,20);
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
RunDialog dialog = new RunDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the dialog.
*/
public RunDialog() {
setBounds(100, 100, 686, 448);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane);
{
scrollPane.setViewportView(textArea);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
public void run(String name){
try{
Runtime ce1=Runtime.getRuntime();
Process proccess=ce1.exec("java "+name);
InputStream in1=proccess.getInputStream();
BufferedInputStream in2=new BufferedInputStream(in1);
byte a[]=new byte[100];
int n;
for(;(n=in2.read(a,0,100))!=-1;){
String s=new String(a,0,n);
textArea.append(s);
if(s==null)
System.out.println("333");
}
System.out.println(n);
System.out.println("444");
}
catch(Exception e){
System.out.println(e.getMessage());
}
} }
4,和第一个一样,读取数据库来答题,
import java.io.*;
import java.util.Scanner;
import java.sql.*; public class Test {
public static void main(String args[]){
ReadTest test1=new ReadTest();
test1.setDatabase("Kooing");
test1.setTable("test");
int number=test1.getNumber();
System.out.println("There are "+number+" question what are you want to answer");
Scanner scanner1=new Scanner(System.in);
for(;scanner1.hasNextInt();){
int num=scanner1.nextInt();
String huiche=scanner1.nextLine();
test1.setQusetion(num);
String s=test1.getQusetion();
System.out.println(s);
System.out.println("input your answer :");
String s2=scanner1.nextLine();
// String huiche=scanner1.nextLine();
if(s2.equals(test1.getAnswer().trim())){
System.out.println("right");
}
else{
System.out.println("error");
}
System.out.println("There are "+number+" question what are you want to answer");
}
}
} class ReadTest{
private Connection con;
private Statement sql;
private ResultSet rs;
private String database,table,daan,wenti;
private int question;
ReadTest(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(Exception e){
System.out.println(e.getMessage());
System.out.println("1111");
}
}
void setDatabase(String a){
database=a;
try{
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName="+database,"sa","kenn5666225");
}
catch(Exception e2){
System.out.println(e2.getMessage());
System.out.println("2222222222");
}
}
void setTable(String a){
table=a;
try{
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select *from "+table);
}
catch(Exception e3){
System.out.println(e3.getMessage());
System.out.println("33333333");
}
}
int getNumber(){
int n=0;
try{
rs.last();
n=rs.getRow();
}
catch(Exception e4){
System.out.println(e4.getMessage());
System.out.println("444444444");
}
return n;
}
void setQusetion(int a){
question=a;
}
String getQusetion(){
try{
rs.absolute(question);
wenti=rs.getString(1)+"\n"+rs.getString(2)+"\n"+rs.getString(3)+"\n"+rs.getString(4)+"\n"+rs.getString(5);
}
catch(Exception e5){
System.out.println(e5.getMessage());
System.out.println("55555555");
}
return wenti;
}
String getAnswer(){
try{
daan=rs.getString(6);
}
catch(Exception e6){
System.out.println(e6.getMessage());
System.out.println("6666666666");
}
return daan;
}
}
IO流+数据库课后习题的更多相关文章
- java基础IO流综合加习题
IO流初学者在学习时都有一点迷糊,今天我们就讲讲IO流,希望通过讲解可以帮助大家 IO流分为字节流,字符流,缓冲流.我们只要记住这三个就可以了. 1*字节流有:字节输入流(FileInputStrea ...
- IO流的练习 —— 创建用户注册、登陆案例
把上次用集合做的时候的分析拿出来 需求: 用户登录注册案例 按照如下操作,可以让我们更符合面向对象思想: A:这个案例有哪些类 B:每个类中又有哪些东西 C:类与类之间的关系 分析: A:这个案例有哪 ...
- JavaEE基础(二十一)/IO流
1.IO流(字符流FileReader) 1.字符流是什么 字符流是可以直接读写字符的IO流 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出. 2 ...
- java中的IO流之文件复制
O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...
- 【Android】数据存储-java IO流文件存储
1.数据持久化:将在内存中的瞬时数据保存在存储设备中.瞬时数据:设备关机数据丢失.持久化技术提供一种机制可以让数据在瞬时状态和持久状态之间转换. 2.Android中简单的三种存储方式:文件存储.Sh ...
- IO流(一)
一.异常 概述 异常就是Java程序在运行过程中出现的错误. 由来 问题也是现实生活中一个具体事务,也可以通过java的类的形式进行描述,并封装成对象.其实就是Java对不正常情况进行描述后的对象体现 ...
- Java IO 流总结篇
1. 写在前面的话 I/O ,I 是 Input (输入)的缩写,O是Output (输出) 的缩写,众所周知,人与人之间想要沟通交流,就需要讲彼此都能听懂的语言,比如大家都统一说英语. 人类如果想和 ...
- JavaSE学习总结(十七)—— IO流
一.IO流概要 1.1.概念 开发中经常要进行输入输出操作,掌握Java中的IO流显得非常必要. 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流, ...
- Java IO流学习
Java IO流学习 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是 ...
随机推荐
- Scut:参数导入方式(有遗留疑问)
先上一段代码: public EnvironmentSetting() { var appServer = GetServerSection(); var protocol = GetProtocol ...
- sqlserver中Profiler的使用
1.单击开始--程序--Microsoft SQL Server 2008--性能工具--SQL Server Profiler,如下图: 2.然后进入SqlServ ...
- Extjs4.1.x使用Application动态按需加载MVC各模块
我们知道Extjs4之后提出了MVC模块开发,将以前肥厚的js文件拆分成小的js模块[model\view\controller\store\form\data等],通过controller拼接黏合, ...
- 你以为PHP那么好自定义升级?
X,PHP如果只是安装操作系统之后,YUM INSTALL之后就可以使用最好啦. 但如果YUM安装的官方PHP版本太低怎么办? 据我所知,现在也只是到PHP5.3.3版本,如果APP应用需要PHP5. ...
- BZOJ 1011 [HNOI2008]遥远的行星
1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2559 Solved ...
- LU分解(2)
接着上次LU分解的讲解,这次给出使用不同的计算LU分解的方法,这种方法称为基于GaxPy的计算方法.这里需要了解lapapck中的一些函数.lapack中有一个函数名为gaxpy,所对应的矩阵计算公式 ...
- Shell遍历文件的每一行
由于使用for来读入文件里的行时,会自动把空格和换行符作为一样分隔符,因为当行里有空格的时候,输出的结果会很乱,所以…… cat input.txt |while read line> do&g ...
- oracle防火墙端口问题
Oracle服务端口方面会有很多的问题,下面就将为您介绍在防火墙上开放Oracle服务端口的方法,希望对您学习Oracle服务端口方面能有所帮助. 要使Oracle客户端能正常连接到设置有防火墙的安装 ...
- 从无到有开发连麦直播技术<转>
转贴地址:http://blog.csdn.net/heisedelangzi/article/details/52400333 从无到有开发连麦直播技术点整理-AnyRTC 直播关键字 采集.前处理 ...
- SheetOffice控件使用分享
1. 控件属性及说明 Template:套用的模板目录(套用模板会使用到) 模板中必须包含书签: Body,这个是在代码中写死了的,是把当前文档的内容插入到模板的Body书签中. 如果使用印章,必须 ...