【发布时间】:2011-05-09 01:36:26
【问题描述】:
所以在这里我得到了可以读取文本文件的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadTextFile {
public static void main(String[] args) {
File file = new File("Test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// show file contents here
System.out.println(contents.toString());
}
}
好的,现在我需要我的文本文件(Test.txt)具有以下结构(示例):
topic:- <climate>subtopic1, <atmosphere_type>subtopic2
subtopic1:- <temperatures>sentence1, <gases>sentence2, <winds>sentence3
subtopic2:- <gas_composition>sentence4, <gravitational_weight>sentence5
sentence1:- temperatures are around 34ºC but they can reach -42ºC in winter
sentence2:- there are significant proportions of nitrogen (13%) and butane (24%)
sentence3:- there are permanent winds with gusts of 118 km/h
sentence4:- methane (48%), nitrogen (13%), butane (24%) and oxygen (12%)
sentence5:- gravity in ecuador is 7.95 atmospheres
我真正需要的是拥有 2 个JList,在第一个JList 中我可以选择一个主题(如“气候”或“大气类型”),然后在我的第二个JList 中选择一个子主题(如果我选择“气候”,那么我可以选择“温度”、“气体”或“风”),所以当我点击JButton 时,程序会显示相应的句子。做这样的事情很难吗?谢谢你的帮助! :)
【问题讨论】:
-
在这里重新提出相同的问题是不可接受的行为。如果您想引起注意您的问题,两天后您将被允许place a bounty on it。您也可以edit your question添加更多信息,这可能会使您的问题更容易理解和回答。