将文件内容写入 jList
谁能向我解释如何将文本文件的内容加载到 jList 中? 我已设法在控制台上打印文件的内容。 我已经完成了一个 Read() 函数,该函数逐行读取文件的内容,以及一个 Load() 函数,该函数应该通过调用函数 Read() 在 jList 中显示内容。
加载代码:
public void Load()
{
Read();
File archive = null;
FileReader fr = null;
BufferedReader br = null;
try {
archive = new File ("Cars.txt");
fr = new FileReader (archive);
br = new BufferedReader(fr);
Vector<String> lines = new Vector<String>();
String line;
while((line=br.readLine())!=null) {
System.out.println(line);
lines.add(line);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try{
if( null != fr ) {
fr.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
读取代码:
public void Read()
{
String name , type , distance, time;
File file=new File("Cars.txt");
FileInputStream fis=null;
BufferedInputStream bis=null;
DataInputStream dis=null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0)
{
String x=dis.readLine();
int k1=x.length();
char []m=x.toCharArray();
int y1=0, y2=0, z=1;
for(int i=1;i<k1;i++)
if(m[i]==' ' && z==1)
{
y1=i;
z++;
}
else if(z==2 && m[i]==' ') {y2=i; z++; }
k1-=y2;
name=x.copyValueOf(x.toCharArray(), 0, y1);
type=x.copyValueOf(x.toCharArray(), 0, y2);
distance=x.copyValueOf(x.toCharArray(), y1+1, y2-y1-1);
time=x.copyValueOf(x.toCharArray(), y2+1, k1-1);
int d=Nr(distance);
int t=Nr(time);
// System.out.println(name + " " +q + " " + n);
list.Add(name, type , d, t);
}
fis.close();
bis.close();
dis.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Can anyone explain to me how can I load the content of a text file into jList ?
I have managed to print the content of the file on the console .
I have done a Read() function that reads the content of the file line by line and a Load() function that should display the content in the jList by calling the function Read() .
the code for load:
public void Load()
{
Read();
File archive = null;
FileReader fr = null;
BufferedReader br = null;
try {
archive = new File ("Cars.txt");
fr = new FileReader (archive);
br = new BufferedReader(fr);
Vector<String> lines = new Vector<String>();
String line;
while((line=br.readLine())!=null) {
System.out.println(line);
lines.add(line);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try{
if( null != fr ) {
fr.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
the code for read:
public void Read()
{
String name , type , distance, time;
File file=new File("Cars.txt");
FileInputStream fis=null;
BufferedInputStream bis=null;
DataInputStream dis=null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0)
{
String x=dis.readLine();
int k1=x.length();
char []m=x.toCharArray();
int y1=0, y2=0, z=1;
for(int i=1;i<k1;i++)
if(m[i]==' ' && z==1)
{
y1=i;
z++;
}
else if(z==2 && m[i]==' ') {y2=i; z++; }
k1-=y2;
name=x.copyValueOf(x.toCharArray(), 0, y1);
type=x.copyValueOf(x.toCharArray(), 0, y2);
distance=x.copyValueOf(x.toCharArray(), y1+1, y2-y1-1);
time=x.copyValueOf(x.toCharArray(), y2+1, k1-1);
int d=Nr(distance);
int t=Nr(time);
// System.out.println(name + " " +q + " " + n);
list.Add(name, type , d, t);
}
fis.close();
bis.close();
dis.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建列表如下:
然后,当您读取每一行数据时,您可以使用:
Create the list like:
Then when you read each line of data you can use: