标题:麻烦看一下这个程序,文件读取不进来
取消只看楼主
zerahfan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2017-3-1
结帖率:0
 问题点数:0 回复次数:0 
麻烦看一下这个程序,文件读取不进来
程序代码:
package jrr;
import *;
import java.util.*;
public class ReadTestquestion {
    String filename="",

    correctAnswer="",//正确答案
    testContent="",//考试题目内容
    selection="";//考生答案           //gai
int score=0;//考生成绩
long time=0;//考试时间
boolean wcks=false;
File f=null;
FileReader in=null;
BufferedReader dqxx=null;
static int t;



public void setFilename(String name){
    filename=name;
    score=0;
    selection="";
try{
    if(in!=null&&dqxx!=null)
        {in.close();
        dqxx.close();}
        f=new File(filename);
        in=new FileReader(f);
        dqxx=new BufferedReader(in);
        correctAnswer=(dqxx.readLine()).trim();
        String temp=(dqxx.readLine()).trim();
        StringTokenizer token=new StringTokenizer(temp,":");
        
        int hour=Integer.parseInt(token.nextToken());//取出小时
        int minute=Integer.parseInt(token.nextToken());//分钟
        int second=Integer.parseInt(token.nextToken());//
        time=1000*(second+minute*60+hour*60*60);
        
    }
    catch(Exception e)
    {
        testContent="没有选择试题";
    }
}
public String getFilename(){return filename;}
public long getTime(){return time;}
public void setWcks(boolean b){wcks=b;}
public boolean getWcks(){return wcks;}
public String getTestContent(){
    try{
        String s=null;
        StringBuffer temp=new StringBuffer();
        if(dqxx!=null){
            while((s=dqxx.readLine())!=null){
                if(s.startsWith("danxuan")){
                    t=1;
                }
                else if(s.startsWith("duoxuan")){
                    t=2;
                }
                else if(s.startsWith("tiankong")){
                    t=3;
                }
                else if(s.startsWith("panduan")){
                    t=4;
                }
            if(s.startsWith("**"))
                break;
            temp.append("\n"+s);
            if(s.startsWith("extend")){
                in.close();
                dqxx.close();
                wcks=true;
            }
            }
            testContent=new String(temp);
        }
        else{testContent=new String("没有选择试题");}
    }
    catch(Exception e){
        testContent="试题内容为空,考试结束";
    }
        return testContent;
    
}

    
    public void setSelection(String s){
        selection=selection+s;
        
    }
    
    public int getScore(){
        score=0;
        int length1=selection.length();
        int length2=correctAnswer.length();
        int min=Math.min(length1,length2);
        for(int i=0;i<min;i++){
            try{
                if(selection.charAt(i)==correctAnswer.charAt(i))
                    score++;
            }
            catch(StringIndexOutOfBoundsException e){
                i=0;}
            
            }
        return score;
        }
    public int getNum(){
        
        return t;
    }
    
    public String getMessages(){
        int length1=selection.length();
        int length2=correctAnswer.length();
        int length=Math.min(length1,length2);
        String message="zhengquedaan"+correctAnswer.substring(0,length)+"\n"+"nidehuida"+selection;
        return message;
    }
}


程序代码:
package jrr;



import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import *;
class FileName implements FilenameFilter{
    String str=null;
    FileName(String s){
        str="."+s;
    }
    public boolean accept(File dir,String name) {
        return name.endsWith(str);
    }
}
public class TestArea extends JPanel implements ActionListener,ItemListener,Runnable{
    Choice list=null;
    JTextArea stxsq=null,xxq=null;
    JCheckBox box[];
    JRadioButton box2[];
    JRadioButton box3[];
    JButton dqxyt,ckfs;
    ReadTestquestion dqst=null;
    JLabel welcomeLabel=null;
    Thread countTime=null;
    static int a=0;
    long time=0;
    
    JTextField timeShow=null;
    boolean sfgbjsq=false,sfztjs=false;
    JButton zthjxjs=null;
    
    

    
    
    
    public TestArea() {
        list=new Choice();
        String dqml=System.getProperty("user.dir");
        File dir=new File(dqml);
        FileName fileTxt=new FileName("txt");
        String fileName[]=dir.list(fileTxt);
        for(int i=0;i<fileName.length;i++) {
            list.add(fileName[i]);
        }
        stxsq=new JTextArea(15,12);
        stxsq.setLineWrap(true);
        stxsq.setWrapStyleWord(true);
        stxsq.setFont(new Font("TimesRoman",Font.PLAIN,14));
        stxsq.setForeground(Color.BLUE);
        xxq=new JTextArea(8,8);
        xxq.setForeground(Color.BLUE);
        xxq.setLineWrap(true);
        xxq.setWrapStyleWord(true);
        countTime=new Thread(this);        
    
        
        
        
        
        
        zthjxjs=new JButton("暂停计时");
        zthjxjs.addActionListener(this);
        //tjbtda=new JButton("提交本题答案");
        dqxyt=new JButton("读取第一题");
        dqxyt.setEnabled(false);           //kaishibuduqu
        dqxyt.setForeground(Color.BLUE);
        //tjbtda.setForeground(Color.BLUE);
        ckfs=new JButton("查看分数");
        ckfs.setForeground(Color.BLUE);
        //tjbtda.setEnabled(false);
        //tjbtda.addActionListener(this);
        dqxyt.addActionListener(this);
        ckfs.addActionListener(this);
        list.addItemListener(this);
        dqst=new ReadTestquestion();
        
        
        
            
        
    String s[]= {"A","B","C","D"};//多选
    box=new JCheckBox[4];
    for(int i=0;i<4;i++) {
        box[i]=new JCheckBox(s[i]);}
        
        String s2[]= {"A","B","C","D"};//单选
            box2=new JRadioButton[4];
            ButtonGroup group=new ButtonGroup();
            for(int i=0;i<4;i++) {
                box2[i]=new JRadioButton(s2[i]);
                group.add(box2[i]);}
            
            String s3[]= {"T","F"};//单选
            box3=new JRadioButton[2];
            ButtonGroup group2=new ButtonGroup();
            for(int i=0;i<2;i++) {
                box3[i]=new JRadioButton(s3[i]);
                group2.add(box3[i]);}
            
        
        
        
        JPanel pAddbox=new JPanel();
        for(int i=0;i<4;i++) {
            pAddbox.add(box[i]);
            pAddbox.add(box2[i]);
        }
        for(int i=0;i<2;i++) {
            pAddbox.add(box3[i]);
            
        }
        
    
        
        
        
        Box boxH1=Box.createVerticalBox();
        Box boxH2=Box.createVerticalBox();
        Box baseBox=Box.createHorizontalBox();
        boxH1.add(new JLabel("选择试题文件"));
        boxH1.add(list);
        boxH1.add(new JScrollPane(xxq));
        boxH1.add(ckfs);
        timeShow=new JTextField(20);
        timeShow.setHorizontalAlignment(SwingConstants.RIGHT);
        timeShow.setEditable(false);
        JPanel p1=new JPanel();
        p1.add(new JLabel("剩余时间或者成绩显示"));
        p1.add(timeShow);
        p1.add(zthjxjs);
        boxH1.add(p1);
        boxH2.add(new JLabel("试题内容:"));
        boxH2.add(new JScrollPane(stxsq));
        JPanel p2=new JPanel();
        p2.add(pAddbox);
        //p2.add(tjbtda);
        p2.add(dqxyt);
        boxH2.add(p2);
        baseBox.add(boxH1);
        baseBox.add(boxH2);
        setLayout(new BorderLayout());
        add(baseBox,BorderLayout.CENTER);
        welcomeLabel=new JLabel("欢迎考试,提高技能水平",JLabel.CENTER);
        welcomeLabel.setFont(new Font("隶书",Font.PLAIN,24));
        welcomeLabel.setForeground(Color.BLUE);
        add(welcomeLabel,BorderLayout.NORTH);
    }
    
    
    public void itemStateChanged(ItemEvent e) {
        timeShow.setText(null);
        sfgbjsq=false;
        sfztjs=false;
        zthjxjs.setText("暂停计时");
        String name=(String)list.getSelectedItem();
        dqst.setFilename(name);
        dqst.setWcks(false);
        time=dqst.getTime();
        
        if(countTime.isAlive()) {
            sfgbjsq=true;
            countTime.interrupt();
        }
        countTime=new Thread(this);
        xxq.setText(null);
        stxsq.setText(null);
        dqxyt.setText("读取第一题");
        //tjbtda.setEnabled(false);
        dqxyt.setEnabled(true);
        welcomeLabel.setText("你选择的试题:"+dqst.getFilename()+"沉着冷静,胆大心细");
    }
    public void actionPerformed(ActionEvent e) {
        

        
        
        if(e.getSource()==dqxyt) {
            String contentTest=dqst.getTestContent();
            int t=dqst.getNum();
            if(t==1){
                for(int i=0;i<4;i++) {
                box[i].setVisible(false);
                box2[i].setVisible(true);
                }
            }
            else if(t==2){
                for(int i=0;i<4;i++) {
                box[i].setVisible(true);
                box2[i].setVisible(false);}
            }
            
            
            dqxyt.setText("读取下一题");
            //tjbtda.setEnabled(true);
            
            stxsq.setText(contentTest);
            xxq.setText(null);
            dqxyt.setEnabled(true);
            
            try {
                countTime.start();
            }
            catch(Exception event) {}
            String answer="?";//bianchengyige
            for(int i=0;i<4;i++) {
                if(box[i].isSelected()) {
                    answer=box[i].getText();////////////////////////////////
                    box[i].setSelected(false);
                    break;
                }
            }
            if(a>0)
            {
            dqst.setSelection(answer);
            
            }
            a++;
        }
        
        //if(e.getSource()==tjbtda) {
            //dqxyt.setEnabled(true);
            //tjbtda.setEnabled(false);
            //String answer="?";
            //for(int i=0;i<4;i++) {
            //    if(box[i].isSelected()) {
            //        answer=box[i].getText();
            //        box[i].setSelected(false);
            //        break;
            //    }
            //}
            //dqst.setSelection(answer);
        //}
        
        if(e.getSource()==ckfs) {
            int score=dqst.getScore();
            String messages=dqst.getMessages();
            xxq.setText("\n"+messages);
        }
        if(e.getSource()==zthjxjs) {
            if(sfztjs==false) {
                zthjxjs.setText("继续计时");
                sfztjs=true;
            }
            else if(sfztjs==true) {
                zthjxjs.setText("暂停计时");
                sfztjs=false;
                countTime.interrupt();
            }
        }
    }
    public synchronized void run() {
        while(true) {
            if(time<=0) {
                sfgbjsq=true;
                countTime.interrupt();
                //tjbtda.setEnabled(false);
                dqxyt.setEnabled(false);
                timeShow.setText("考试时间到,考试自动结束,请看清成绩并离开考场!");
            }
            else if(dqst.getWcks()) {
                sfgbjsq=true;
                timeShow.setText("您的考试成绩是="+dqst.getScore());
                countTime.interrupt();
                //tjbtda.setEnabled(false);
                dqxyt.setEnabled(false);
            }
            else if(time>=1) {
                time=time-1000;
                long leftTime=time/1000;
                long leftHour=leftTime/3600;
                long leftMinute=(leftTime-leftHour*3600)/60;
                long leftSecond=leftTime%60;
                timeShow.setText(""+leftHour+"小时"+leftMinute+"分"+leftSecond+"秒");
            }
            try{Thread.sleep(1000);}
            catch(InterruptedException ee) {
                if(sfgbjsq==true)
                    return;
            }
            while(sfztjs==true) {
                try {wait();}
                catch(InterruptedException ee) {
                    if(sfztjs==false) {
                        notifyAll();
                    }
                }
            }
        }
    }
}

程序代码:
package jrr;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
    TestArea testPanel=null;    //调用TestArea类
Container con=null;   //dingyi rongqi
public Test(){
    super("计算机考试系统");
    testPanel=new TestArea();
    con=getContentPane();
    con.add(testPanel,BorderLayout.CENTER);
    addWindowListener(new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    });
     setVisible(true);
     setBounds(60,40,760,560);
     con.validate();
     validate();}
     public static void main(String args[]){
         new Test();
     
}

}

在学校电脑可以,自己的不好使,就是点完TXT文档读取下一题的纽还是不可用的,文件没有读进来

TXT文件
BDBAB
0:10:00
danxuan
1.java是一种(  )的语言
A、面向过程                  B、面向对象
C、面向过程同时面向对象      D、无法确定
*****
danxuan
2.以下软件中,(   )是数据库软件?
A、windows                   B、office
C、ie                        D、sql server
*****
duoxuan
3.java通过(    )访问数据库
A、DBMS、                    B、JDBC
C、ODBC                   D、XML
*****
duoxuan
4.JAVA中,通过()命令引入包
A、import                    B、download
C、javac                     D、include
*****
Ended
搜索更多相关主题的帖子: String int null new add 
2017-12-28 13:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-483795-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.196952 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved