更容易做到这一点?
我正在尝试导入四行文件:
name phone mobile address
我正在使用:
public void importContacts() {
try {
BufferedReader infoReader = new BufferedReader(new FileReader(
"../files/example.txt"));
int i = 0;
String loadContacts;
while ((loadContacts = infoReader.readLine()) != null) {
temp.add(loadContacts);
i++;
}
int a = 0;
int b = 0;
for (a = 0, b = 0; a < temp.size(); a++, b++) {
if (b == 4) {
b = 0;
}
if (b == 0) {
Name.add(temp.get(a));
}
if (b == 1) {
Phone.add(temp.get(a));
}
if (b == 2) {
Mobile.add(temp.get(a));
}
if (b == 3) {
Address.add(temp.get(a));
}
}
}
catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
txtName.setText(Name.get(index));
txtPhone.setText(Phone.get(index));
txtMobile.setText(Mobile.get(index));
txtAddress.setText(Address.get(index));
}
是他们更容易的方式吗?看起来很啰嗦!
答案 0 :(得分:2)
您可以使用扫描仪类。
Scanner s = new Scanner(new File("input.txt"));
name = s.nextLine();
phone = s.nextLine();
mobile = s.nextLine();
address = s.nextLine();
答案 1 :(得分:1)
Apache Fileutils readFileToString()或readLines()使代码更加干净。
import org.apache.commons.io.FileUtils;
...
File file = new File("foobar.txt");
try
{
List<String> data = FileUtils.readLines(file);
// Iterate the result to print each line of the file.
Iterator<String> iter = data.iterator();
while(iter.hasNext()) {
Name.add(iter.next());
if (iter.hasNext()) {
Phone.add(iter.next());
}
if (iter.hasNext()) {
Mobile.add(iter.next());
}
if (iter.hasNext()) {
Address.add(iter.next());
}
}
} catch (IOException e)
{
e.printStackTrace();
}
通过使用像
这样的结构,你甚至可以缩短它if (iter.hasNext()) Phone.add(iter.next());
但我个人认为丢弃大括号会使代码更容易出错。不过,你可以把它放在一行上。
答案 2 :(得分:0)
创建表示您的数据集的数据对象。使用新对象,接受一个字符串并在新对象中本地解析它。
Driver Class:
readInFromFile
EntityClass
EntityClass(String) < calls the parse method
get[data elements]
parseFromString(String info) <- this is responsible for all of your reading
“readFromFile”方法将变为:
....
while ((String line= reader.readLine) != null) {
list.add(new Entity(line));
}
答案 3 :(得分:0)
BufferedReader infoReader = new BufferedReader(new FileReader("../files/example.txt"));
String loadContacts;
List<People> list = new ArrayList<People>();
while ((loadContacts = infoReader.readLine()) != null) {
String[] singleContact = loadContacts.split(REGEXP_FOR_SPLIT_VALUES);
People p = new People();
p.setName(singleContact[0]);
p.setPhone(singleContact[1]);
p.setMobile(singleContact[2]);
p.setAddress(singleContact[3]);
list.add(p);
}
答案 4 :(得分:0)
这个怎么样?
while(infoReader.hasNext()) {
Name.add(infoReader.readLine());
Phone.add(infoReader.readLine());
Mobile.add(infoReader.readLine());
Address.add(infoReader.readLine());
}
虽然我更喜欢将Name,Phone等类更改为代表一个联系人的一个类。
答案 5 :(得分:0)
以下是我将如何操作,使用新的Scanner类轻松读取并处理IOExceptions,使用ClassLoader查找文件,并使用简单的@Data类来存储数据。
public void importContacts() {
Scanner scanner = new Scanner(ClassLoader.getSystemClassLoader().getResourceAsStream("example.txt"));
List<Contact> list = Lists.newArrayList();
while(scanner.hasNext()) {
list.add(new Contact(
scanner.nextLine(),
scanner.nextLine(),
scanner.nextLine(),
scanner.nextLine()
));
}
Contact c = list.get(index);
txtName.setText(c.getName());
txtAddress.setText(c.getAddress());
txtPhone.setText(c.getPhone());
txtMobile.setText(c.getMobile());
}
private static @Data class Contact {
private final String name, phone, mobile, address;
}
答案 6 :(得分:0)
如果文件只包含一个联系人,并且您可以控制源文本文件的格式,则可以将其重新格式化为属性文件:
name=value
然后你将其作为属性文件阅读(参见ResourceBundle),最终变得简单:
Mobile.add(properties.getProperty("mobile"))
答案 7 :(得分:-1)
为什么不呢:
public String readLine(BufferedReader pReader) {
try {
return pReader.readLine();
} catch(IOException IOE) {
/* Not a very good practice but let say "We don't care!" */
// Return null if the line is not there (like there was no 4 lines in the file)
return null;
}
}
public void importContacts() {
try {
BufferedReader infoReader = new BufferedReader(new FileReader("../files/example.txt"));
txtName .setText(readLine(infoReader));
txtPhone .setText(readLine(infoReader));
txtMobile .setText(readLine(infoReader));
txtAddress.setText(readLine(infoReader));
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
}
希望这有帮助。