您的位置:首页 > 其它

百度问题解答

2014-04-24 12:45 274 查看
求助!哪位能帮忙处理一下异常!!!1
提问者:心语芯12

我是在C盘的测试的,String filename = "C:/test.txt";你改下路径就好了,最经没有登百度知道,我想你应该早解决这个问题了。
test.text 内容格式如下:
tom, 7073.8430730000055, 200
lily, 428.7177620000004, 150

测试代码

package helper;

import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;

public class Test0702 extends JFrame implements ActionListener {
//间隔符
private static final String SPACE_CHAR = ", ";

private JButton J1_OPEN = new JButton("打开文件");

private JButton J2_ALTER = new JButton("更改工资");

private JButton J3_CLEAR = new JButton("清空");

private JTextArea T = new JTextArea();

int count = 0;
double total = 0;

String[][] array = new String[20][3];
String filename = "C:/test.txt";

Test0702() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(1, 3, 20, 20)); // 创建行数1、列数3、水平和垂直间距都为20的网格布局

p.add(J1_OPEN, BorderLayout.WEST);
p.add(J3_CLEAR, BorderLayout.CENTER);
p.add(J2_ALTER, BorderLayout.EAST);
JScrollPane jsp = new JScrollPane(T);
add(jsp, BorderLayout.CENTER);
add(p, BorderLayout.SOUTH);

J1_OPEN.addActionListener(this);
J2_ALTER.addActionListener(this);
J3_CLEAR.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == J1_OPEN) { // 打开文件
open();
}
if (e.getSource() == J3_CLEAR) {// 清空
blank();
}
if (e.getSource() == J2_ALTER) {//更改

System.out.println(count);

for (int t = 0; t < count; t++) {
//需要判断array[t][1]有没有值
if(array[t][1] != null) {
total = Double.valueOf(array[t][1]).doubleValue();
total = (double) (total * (1 + 0.1));
array[t][1] = Double.toString(total);
}
}
try {
FileWriter fos = new FileWriter(filename);

BufferedWriter sf = new BufferedWriter(fos);
for (int k = 0; k < count; k++) {
for (int j = 0; j < 3; j++) {
if(array[k][j] != null) {
if(j != array[0].length - 1){
sf.write(array[k][j] + SPACE_CHAR);
}else {
sf.write(array[k][j]);
}

}else {
System.out.println("array[" + k + "][" + j + "] 为 null !");
}
}
sf.write("\n");
}
// sf.flush();
sf.close();
//刷新面板
open();
} catch (FileNotFoundException ex) {
System.out.println("File not found ");
} catch (IOException ex) {
System.out.println("File read error");
}
}
}

private void blank() { // 清空
T.setText("");
array = new String[20][3];// 清空数组
count = 0;
}

private void open() {//打开文件
blank();//打开文件前先清空面板
String inline;
BufferedReader input = null;
int i = 0;
try {
input = new BufferedReader(new FileReader(filename));
while ((inline = input.readLine()) != null) {
// 将字符串添加到JTextArea中(即将文件内容显示在JTextArea中)
if(inline.length() > 0) {
T.append(inline + "\n");
String[] line = inline.split(SPACE_CHAR);
if(line.length == 1) {
array[i] = line;
array[i] = new String[]{line[0],null,null};
}else if(line.length == 2){
array[i] = new String[]{line[0],line[1],null};
}else if(line.length == 3){
array[i] = line;
}
i++;
count++;
}
}
}catch (FileNotFoundException ex) {
System.out.println("File not found :" + filename);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
if (input != null) {
input.close();
}
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Test0702 frame = new Test0702();
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(450, 200, 400, 400);
frame.setVisible(true);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: