请编写一个字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class AddLine
{
public static void main(String[] args)
{
JFrame jf = new JFrame("Hello");
jf.setSize(600,400);
jf.setLocation(100,100);
JPanel p = new JPanel();
JLabel l = new JLabel("Open File:");
final JTextField tf = new JTextField(30);
p.add(l);
p.add(tf);
jf.getContentPane().add(p,"North");
final JTextArea ta = new JTextArea();
jf.getContentPane().add(ta,"Center");
JButton btn = new JButton("Open");
jf.getContentPane().add(btn,"north");
byte b[] = new byte [1024*10];
String s;
final String str = tf.getText();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
FileInputStream fis = new FileInputStream(str);
DataInputStream din = new DataInputStream(fis);
String line = System.getProperty("line.separator");
int len = 0;
byte b[] = new byte[1024*10];
while((b = din.readLine())!=null)
{
ta.append(++len + ": ");
ta.append(b);
ta.append(line.getBytes());
}
fis.close();
}
catch (Exception ex)
{
ex.printStackTrace();
} }
});
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf.show();
}
}
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class AddLine
{
public static void main(String[] args)
{
JFrame jf = new JFrame("Hello");
jf.setSize(600,400);
jf.setLocation(100,100);
JPanel p = new JPanel();
JLabel l = new JLabel("Open File:");
final JTextField tf = new JTextField(30);
p.add(l);
p.add(tf);
jf.getContentPane().add(p,"North");
final JTextArea ta = new JTextArea();
jf.getContentPane().add(ta,"Center");
JButton btn = new JButton("Open");
jf.getContentPane().add(btn,"north");
byte b[] = new byte [1024*10];
String s;
final String str = tf.getText();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
FileInputStream fis = new FileInputStream(str);
DataInputStream din = new DataInputStream(fis);
String line = System.getProperty("line.separator");
int len = 0;
byte b[] = new byte[1024*10];
while((b = din.readLine())!=null)
{
ta.append(++len + ": ");
ta.append(b);
ta.append(line.getBytes());
}
fis.close();
}
catch (Exception ex)
{
ex.printStackTrace();
} }
});
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf.show();
}
}
帮忙顶
从要求来看,你只要把中间的一段
try
{
FileInputStream fis = new FileInputStream(str);
DataInputStream din = new DataInputStream(fis);
String line = System.getProperty("line.separator");
int len = 0;
byte b[] = new byte[1024*10];
while((b = din.readLine())!=null)
{
ta.append(++len + ": ");
ta.append(b);
ta.append(line.getBytes());
}
fis.close();
}包装成一个类,提供一个readline方法就好了。
{
public static void main(String[] args)
{
JFrame jf = new JFrame("Hello");
jf.setSize(600, 400);
jf.setLocation(100, 100);
JPanel p = new JPanel();
JLabel l = new JLabel("Open File:"); final JTextField tf = new JTextField(30);
p.add(l);
p.add(tf);
jf.getContentPane().add(p, "North");
final JTextArea ta = new JTextArea();
jf.getContentPane().add(ta, "Center");
JButton btn = new JButton("Open");
jf.getContentPane().add(btn, "North");
byte b[] = new byte[1024 * 10]; btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str = ta.getText();
try
{
FileInputStream fis = new FileInputStream(new File(str));
ta.setText("");
BufferedReader br = new BufferedReader(new InputStreamReader(fis)) ;
// DataInputStream din = new DataInputStream(fis); String line = System.getProperty("line.separator");
int len = 0;
byte b[] = new byte[1024 * 10];
String s ;
while ((s = br.readLine()) != null)
{
ta.append(++len + ": ");
ta.append(s);
// ta.append(line.getBytes());
ta.append(line);
} fis.close();
} catch (Exception ex)
{
ex.printStackTrace();
} }
}); jf.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
jf.setVisible(true);
}
}大概改了一下,需要在textarea中输入文件的绝对路径
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;/**
* @author songy
*
*/
public class NewInputStream {
static BufferedReader bufferedReader = null;
static int index = 0;
/**
*
*/
public NewInputStream(BufferedReader bufferedReader) {
this.bufferedReader = bufferedReader;
} /* (non-Javadoc)
* @see java.io.InputStream#read()
*/
public String readLine() throws IOException {
index ++;
String currentLine = bufferedReader.readLine();
if (currentLine != null ){
return String.valueOf(index) + ": " + currentLine;
} else {
return null;
}
}
public void close() throws IOException {
bufferedReader.close();
}
public static void main(String[] args) {
NewInputStream newInputStream = null;
if (args.length != 1) {
System.err.println("Usage: java NewInputStream [fileName]");
System.exit(1);
}
try {
newInputStream = new NewInputStream(new BufferedReader(new FileReader(args[0])));
String currentLine = "";
while ((currentLine = newInputStream.readLine()) != null) {
System.out.println(currentLine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {newInputStream.close();} catch (IOException e) { }
}
}
}所谓包装类应该类似于BufferdReader之于FileReader,提供一个更高层次的接口,所以并不应该写成LZ的形式。