import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; class wj extends JFrame implements ActionListener { FileReader r_file; FileWriter w_file; BufferedReader buf_reader; BufferedWriter buf_writer; JTextArea txt; JButton an1,an2; JPanel p; wj() { setSize(200,200); setVisible(true); txt=new JTextArea(10,10); an1=new JButton("读取"); an2=new JButton("显示"); an1.addActionListener(this); an2.addActionListener(this); p=new JPanel(); add(txt,"center"); add(p,"south"); p.setLayout(new FlowLayout()); p.add(an1); p.add(an2); validate(); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if (e.getSource()==an1) { readFile(); } if (e.getSource()==an2) { writeFile(); } } public void readFile() { String s; try { File f=new File("D:/jtext/","input.txt"); r_file=new FileReader(f); buf_reader=new BufferedReader(r_file); } catch (IOException ef) { System.out.println(ef); } try { while ((s=buf_reader.readLine())!=null); { txt.append(s+'\n'); } } catch (IOException er) { System.out.println(er); } } public void writeFile() { try { w_file=new FileWriter("output.txt"); buf_writer=new BufferedWriter(w_file); String str= txt.getText(); buf_writer.write(str,0,str.length()); buf_writer.flush(); } catch (IOException ew) { System.out.println(ew); } } } public class 文件流 { public static void main(String[] args) { wj w=new wj(); } }修改代码使其在input中输入一段短文,统计文件中字符个数并将结果写入另一个文件output中
时间: 2024-02-14 09:16:54 浏览: 126
修改后的代码如下:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
class WJ extends JFrame implements ActionListener {
FileReader r_file;
FileWriter w_file;
BufferedReader buf_reader;
BufferedWriter buf_writer;
JTextArea txt;
JButton an1, an2;
JPanel p;
WJ() {
setSize(200, 200);
setVisible(true);
txt = new JTextArea(10, 10);
an1 = new JButton("读取");
an2 = new JButton("显示");
an1.addActionListener(this);
an2.addActionListener(this);
p = new JPanel();
add(txt, "center");
add(p, "south");
p.setLayout(new FlowLayout());
p.add(an1);
p.add(an2);
validate();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == an1) {
readFile();
}
if (e.getSource() == an2) {
writeFile();
}
}
public void readFile() {
String s;
try {
File f = new File("D:/jtext/", "input.txt");
r_file = new FileReader(f);
buf_reader = new BufferedReader(r_file);
} catch (IOException ef) {
System.out.println(ef);
}
try {
while ((s = buf_reader.readLine()) != null) {
txt.append(s + '\n');
}
} catch (IOException er) {
System.out.println(er);
}
}
public void writeFile() {
try {
w_file = new FileWriter("D:/jtext/output.txt");
buf_writer = new BufferedWriter(w_file);
String str = txt.getText();
int length = str.length();
buf_writer.write("字符个数为:" + length);
buf_writer.flush();
} catch (IOException ew) {
System.out.println(ew);
}
}
}
public class 文件流 {
public static void main(String[] args) {
WJ w = new WJ();
}
}
```
修改后的代码在读取按钮读取输入文件后,计算文件中字符个数并将结果写入output文件中。
阅读全文