这里能在TextArea里 显示  可为什么st数组是空的呢?

解决方案 »

  1.   


    import java.io.*;
    import java.util.regex.Pattern;
    import java.awt.*;
    import java.awt.event.*;import org.apache.tools.ant.util.regexp.Regexp;public class File4 { public static void main(String[] args) {
    File4Frm frm = new File4Frm();
    }
    }class File4Frm extends Frame implements ActionListener {
    FileDialog sv, op; // 定义文件对话框对象sv, op
    Button btn1, btn2, btn3, btn4;
    TextArea tarea; File4Frm() {
    super("打开和保存文件");
    setLayout(null);
    setBackground(Color.white);
    setSize(600, 300);
    setVisible(true);
    btn1 = new Button("OPEN");
    btn2 = new Button("SAVE");
    btn3 = new Button("CLOSE");
    btn4 = new Button("+1.1");
    tarea = new TextArea("");
    add(btn1);
    add(btn2);
    add(btn3);
    add(btn4);
    add(tarea);
    tarea.setBounds(30, 50, 460, 220);
    btn1.setBounds(520, 50, 50, 30);
    btn2.setBounds(520, 100, 50, 30);
    btn3.setBounds(520, 150, 50, 30);
    btn4.setBounds(520, 200, 50, 30);
    sv = new FileDialog(this, "保存", FileDialog.SAVE); // 保存功能
    op = new FileDialog(this, "打开", FileDialog.LOAD); // 打开功能 btn1.addActionListener(this);
    btn2.addActionListener(this);
    btn3.addActionListener(this);
    btn4.addActionListener(this); addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    } public void actionPerformed(ActionEvent e) {
    String str;
    String st[][] = new String[3][3];
    if (e.getSource() == btn1) {
    op.setVisible(true);
    try {
    File f1 = new File(op.getDirectory(), op.getFile());
    FileReader fr = new FileReader(f1);
    BufferedReader br = new BufferedReader(fr);
    tarea.setText("");
    int i=0;
    while ((str = br.readLine()) != null) {
    tarea.append(str+'\n');
    Pattern p=Pattern.compile("\\x20+");
    st[i] = p.split(str);
    for (int j = 0; j < st.length; j++) {
    System.out.println(st[i][j]);
    }
    i++;
    }
    fr.close();
    } catch (Exception e1) {
    }
    } if (e.getSource() == btn4) {
    for (int j = 0; j < st.length; j++) {
    System.out.println(st[j][1]);
    Double d = Double.parseDouble(st[j][1]);
    d *= 1.1;
    st[j][1] = String.valueOf(d);
    }
    System.out.println("-------+1.1--------");
    for (int j = 0; j < st.length; j++)
    tarea.append(st[j][1] + '\n'); }
    if (e.getSource() == btn2) {
    sv.setVisible(true);
    try {
    File f1 = new File(sv.getDirectory(), sv.getFile());
    FileWriter fw = new FileWriter(f1);
    BufferedWriter bw = new BufferedWriter(fw);
    String gt = tarea.getText();
    bw.write(gt, 0, gt.length());
    bw.flush();
    bw.close();
    } catch (Exception e1) {
    }
    } if (e.getSource() == btn3) {
    System.exit(0);
    }
    }

    public String replace(String str) {
        if (str == null || str.length()==0) {
            return "";
        }
        return str.replaceAll(" ", " ").replaceAll(",", ",");
    }
    }
      

  2.   

    import org.apache.tools.ant.util.regexp.Regexp;
    这句的作用是什么呀,在我这里的环境是出现错误的注释import org.apache.tools.ant.util.regexp.Regexp;
    点 btn4 按钮时出现这样的还是没把中间那个数加10%在下面显示
      

  3.   

    BufferedReader有缓存区的,数据量太小的话是存在缓存区的,读不出来的,想读出来的话,加上br.readLine()的后面加上br.flush(),刷新缓存区
      

  4.   


    BufferedReader里没有flush()这个方法 
      

  5.   

    while((str=br.readLine())!=null){
                        int i=0;
                        tarea.append(str+'\n');
                        st[i] = str.split("  ");
                        for(int j=0; j<st.length;j++){
                            System.out.println(st[i][j]);
                        }
                        i += 1;
                    }循环写错了 你打印st[i][j]的时候 i++了 所以永远访问的是一个null
      

  6.   

    import org.apache.tools.ant.util.regexp.Regexp;   这个包没用,可以删除。
      

  7.   


    Pattern p=Pattern.compile("\\x20+");
    这个不是匹配空格的吗
      

  8.   

    while ((str = br.readLine()) != null) {
                        tarea.append(str+'\n');
                        Pattern p=Pattern.compile("\\x20+");
                        st[i] = p.split(str);
                        for (int j = 0; j < st.length; j++) {
                            System.out.println(st[i][j]);
                        }
                        i++;
                    }为什么这里可以打印  if (e.getSource() == btn4) {
                for (int j = 0; j < st.length; j++) {
                    System.out.println(st[j][1]);
                    Double d = Double.parseDouble(st[j][1]);而这里却是空值呢?