一道java的输入流问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 13:56:47
下面这段程序想要实现的是打开一个文件,然后读取文件里的内容。可是我不想用传统按行读取的方法,给修改成按一个字符读取,结果程序就出错了,请帮忙修改一下,谢谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Test {
JFrame frame = new JFrame("读取文件");
JTextArea ta = new JTextArea();

public Test() {
JButton b1 = new JButton("open");
b1.addActionListener(new al());
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(b1, BorderLayout.NORTH);
cp.add(ta, BorderLayout.CENTER);
frame.setSize(600, 500);
ta.setLineWrap(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

class al implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int value = fc.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPT

按字符读取会不可避免地撞上编码问题,建议还是按行读取吧。

ta.append("\n")应该在大括号外面吧

你这不多此一举么?除了降低效率还能做什么?