2009-11-23 171 views
0

任何方式更容易做到这一点?导入文本文件

我试图导入一个文件,该文件是四行:

 
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

哪里吨他'infoReader.close()'! – 2009-11-23 21:40:42

+0

你可以去掉变量i。它似乎没有被引用(读取)在任何地方。 – 2009-11-23 22:54:30

回答

-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()); 
    } 
} 

希望这有助于。

+0

我会在readLine中抛出异常,并将其设为私有。你只在importContacts中使用它,并且无论如何都要捕获异常! – extraneon 2009-11-23 21:34:24

+0

@extraneon:确实如此:-D – NawaMan 2009-11-23 21:59:38

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()); 

但我个人觉得,丢弃括号使代码更容易出错。不过,你可以把它放在一行上。

+0

我收到一个错误,当我把该进口行,我把我的程序名称,它说FileUtils?我真的不明白这个java malarky!它说system.out。println,我如何获得文本到Jtextfields? 谢谢 – addiosamigo 2009-11-23 21:28:44

+0

您需要在您的类路径中使用Apache Commons IO JAR文件才能使用该导入行;它正在导入外部功能。 – 2009-11-23 21:31:33

+0

您确实需要下载Apache Fileutils并将其放入类路径中。 – extraneon 2009-11-23 21:31:43

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)); 
} 
+0

这对我来说就像外星人的语言!我只是不明白!你可以贬低它给我谢谢 – addiosamigo 2009-11-23 21:30:23

+0

我主要描述这两个班的结构。 – monksy 2009-11-23 21:47:39

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); 
    } 
2

您可以使用扫描仪类。

Scanner s = new Scanner(new File("input.txt")); 
name = s.nextLine(); 
phone = s.nextLine(); 
mobile = s.nextLine(); 
address = s.nextLine(); 
0

这个怎么样?

while(infoReader.hasNext()) { 
    Name.add(infoReader.readLine()); 
    Phone.add(infoReader.readLine()); 
    Mobile.add(infoReader.readLine()); 
    Address.add(infoReader.readLine()); 
} 

尽管我更喜欢将Name,Phone等类更改为代表一个联系人的一个类。

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; 
} 
0

如果该文件将只包含一个接触,你必须在源文本文件的格式控制,你可以重新格式化它像一个属性文件:

name=value

,那么你会在读它作为一个属性文件(参见ResourceBundle),其最终被简单:

Mobile.add(properties.getProperty("mobile"))

+0

没有它的四行四开始,但是当我添加更多的联系人时,它会变得更多。谢谢 – addiosamigo 2009-11-23 22:08:23

+0

然后,您可能会更好地使用BufferedReader.readLine()或Scanner.nextLine(),正如其他用户建议的那样。 – spork 2009-11-24 17:43:17