Итак, я получил свой код для чтения текстового файла:
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
, программа покажет мне соответствующее предложение. Трудно сделать что-то подобное? Спасибо за вашу помощь! :)