百度问题解答
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);
}
}
提问者:心语芯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);
}
}
相关文章推荐
- 百度谷歌博客关键字SEO最常见问题解答
- 做百度优化常见的十个问题及解答
- 海盗分金问题解答---来自百度
- 解答百度上面一个停机问题的疑惑
- 百度官方解答百度快照的4大问题
- 跨域请求jQuery的ajax jsonp使用常见问题解答
- 回溯法解答整数划分问题(Java版本)
- Topcoder问题解答目录
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.14
- 轻松解决IE7百度中文乱码问题
- Java hashCode() 和 equals()的若干问题解答
- 求高手解答FL2440的问题
- 汇编语言学习:精华问题解答
- [裴礼文数学分析中的典型问题与方法习题参考解答]4.3.28
- 解决github-pages无法被百度抓取问题(octopress)
- 百度社会化分享组件使用问题
- 一道百度的面试题解答
- 非沈阳户口 沈阳 买房 问题解答
- 摘之网上,两个很基本的问题的解答。
- 127名选手1:1淘汰赛决出优胜者问题 - Google, 谷歌,百度,baidu,阿里巴巴,alibaba,微软,华为,huawei面试题