当前位置: 技术问答>java相关
请问如何将一个文本文件一行一行读进一个列表框。我用的是vj++.
来源: 互联网 发布时间:2015-01-04
本文导语: 请问如何将一个文本文件一行一行读进一个列表框。我用的是vj++. 感谢大侠了!!! | BufferedReader read=new BufferedReader(new FileReader(filename)); String str = read.readLine(); ........... | ...
请问如何将一个文本文件一行一行读进一个列表框。我用的是vj++.
感谢大侠了!!!
感谢大侠了!!!
|
BufferedReader read=new BufferedReader(new FileReader(filename));
String str = read.readLine();
...........
String str = read.readLine();
...........
|
//给你一个例子,希望对你有帮助。
//不过这是用sun的jdk1.3开发包,写的。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Test extends JFrame {
JList list = new JList();
JScrollPane sp = new JScrollPane(list);
Vector data = new Vector();
JButton button = new JButton("load");
ButtonListener blistener = new ButtonListener();
public Test(){
super("Test");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
list.setVisibleRowCount(10);
button.addActionListener(blistener);
Container contentPane = getContentPane();
contentPane.add(sp,BorderLayout.CENTER);
contentPane.add(button,BorderLayout.NORTH);
pack();
setSize(410,300);
setLocation(200,150);
setResizable(false);
setVisible(true);
}
public void loadData(String filename) {
File inputFile = new File(filename);
if (!inputFile.exists()) {
try{}
catch(Exception e){}
}
FileInputStream input = null;
try { input = new FileInputStream(inputFile);
}
catch(Exception ioe){ }
try { BufferedReader reader = new BufferedReader( new InputStreamReader(input));
if(!data.isEmpty())data.removeAllElements();
while (true) {
String currentLine = reader.readLine();
if (currentLine==null)
break;
data.addElement(currentLine);
list.setListData(data);
}
}
catch (Exception ioe) {
}
}
public static void main(String args[]) {
new Test().show();
}
class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == button) {loadData("loadtry.txt");//写入文件路径和文件名
}
}
}
}
//不过这是用sun的jdk1.3开发包,写的。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Test extends JFrame {
JList list = new JList();
JScrollPane sp = new JScrollPane(list);
Vector data = new Vector();
JButton button = new JButton("load");
ButtonListener blistener = new ButtonListener();
public Test(){
super("Test");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
list.setVisibleRowCount(10);
button.addActionListener(blistener);
Container contentPane = getContentPane();
contentPane.add(sp,BorderLayout.CENTER);
contentPane.add(button,BorderLayout.NORTH);
pack();
setSize(410,300);
setLocation(200,150);
setResizable(false);
setVisible(true);
}
public void loadData(String filename) {
File inputFile = new File(filename);
if (!inputFile.exists()) {
try{}
catch(Exception e){}
}
FileInputStream input = null;
try { input = new FileInputStream(inputFile);
}
catch(Exception ioe){ }
try { BufferedReader reader = new BufferedReader( new InputStreamReader(input));
if(!data.isEmpty())data.removeAllElements();
while (true) {
String currentLine = reader.readLine();
if (currentLine==null)
break;
data.addElement(currentLine);
list.setListData(data);
}
}
catch (Exception ioe) {
}
}
public static void main(String args[]) {
new Test().show();
}
class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == button) {loadData("loadtry.txt");//写入文件路径和文件名
}
}
}
}
|
readline