我想将文件中的字符串添加到StringBuilder
,以便我可以将信息显示为一个String
。为什么行System.out.println(sb.toString());
没有在控制台中显示?我无法判断代码是否正常工作,并且它没有显示任何错误。
文件示例:
姓氏
名字
ID
故乡
HomeState
电话号码
String input = textFieldLastName.getText()
从JTextField
private String filename = "test.txt"
private static File file;
private static PrintWriter outputFile;
@SuppressWarnings("rawtypes")
private static JList list;
private static Scanner inputFile;
public void Read() throws IOException {
if(file == null || filename.equals("")) {
System.out.println("File not selected!");
}else{
inputFile = new Scanner(filename);
StringBuilder sb = new StringBuilder();
sb.delete(0, sb.length());
DefaultListModel listModel = new DefaultListModel();
while(inputFile.hasNext()){
// Read the next name
String ln = inputFile.nextLine();
String input = textFieldLastName.getText();
if(input.equalsIgnoreCase(ln)){
// Save friend information to variables
sb.append(ln);
for(int i = 0; i <=5; i++){
sb.append(", " + inputFile.nextLine());
}
listModel.addElement(sb.toString());
System.out.println(sb.toString());
}
}
list = new JList();
list.setModel(listModel);
}
}