java的BufferedReader有关问题,各位大神帮帮忙
java的BufferedReader问题,各位大神帮帮忙
package com.file;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
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.cyan);
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("");
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]);
}
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);
}
}
}
为什么这里打印出来的是null 九个都是null
这是选择的文件
为什么呀,各位帮帮忙,明天需要交了,作业呀。。。
------解决思路----------------------
------解决思路----------------------
BufferedReader有缓存区的,数据量太小的话是存在缓存区的,读不出来的,想读出来的话,加上br.readLine()的后面加上br.flush(),刷新缓存区
------解决思路----------------------
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
------解决思路----------------------
你循环里面的i没有重新计数
------解决思路----------------------
我那块用正则表达式就是去掉多余的逗号
package com.file;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
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.cyan);
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("");
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]);
}
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);
}
}
}
为什么这里打印出来的是null 九个都是null
这是选择的文件
为什么呀,各位帮帮忙,明天需要交了,作业呀。。。
------解决思路----------------------
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(",", ",");
}
}
------解决思路----------------------
BufferedReader有缓存区的,数据量太小的话是存在缓存区的,读不出来的,想读出来的话,加上br.readLine()的后面加上br.flush(),刷新缓存区
------解决思路----------------------
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
------解决思路----------------------
你循环里面的i没有重新计数
------解决思路----------------------
我那块用正则表达式就是去掉多余的逗号