使用BufferedReader从文本文件中解析交替的字符串和整数行 [英] Parsing alternating string and integer lines from text file using BufferedReader

查看:73
本文介绍了使用BufferedReader从文本文件中解析交替的字符串和整数行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名相当新的Java程序员。我的问题是......对于以下文本文件内容:



http://textuploader.com/ga7x



我试图将书籍参考号码(整数)和书籍标题(字符串)使用缓冲重新分析器解析成单独的arraylists,然后将arraylists的内容转移到单独的数组中。



我尝试过以下方法:

I am a fairly new programmer in Java. My question is...for the following text file contents:

http://textuploader.com/ga7x

I am trying to parse the book reference numbers (the integers) and the book titles (the strings) using a bufferedreader into separate arraylists and then later on transfer the contents of the arraylists into separate arrays.

I have tried the following:

ArrayList<integer> tempBooksReference = new ArrayList<>();
ArrayList<string> tempBooksTitle = new ArrayList<>();

BufferedReader br = null;
try {

    br = new BufferedReader(new FileReader("booklist.txt"));
    String nextLine;
    String bookTitle;
    int bookNum;

    while ((nextLine = br.readLine()) != null) {

        bookNum = Integer.parseInt(nextLine);
        tempBooksReference.add(bookNum);
        bookTitle = nextLine;
        tempBooksTitle.add(bookTitle);

    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Integer[] booksReference = new Integer[tempBooksReference.size()];
String[] booksTitle = new String[tempBooksTitle.size()];
tempBooksReference.toArray(booksReference);
tempBooksTitle.toArray(booksTitle);



不幸的是它似乎不起作用。有人可以告诉我如何使用BufferedReader进行此操作(我想使用BufferedReader而不是Scanner来完成此任务,虽然如果我能弄清楚如何使用BufferedReader进行此操作,那么我会非常高兴通过扫描仪学习如何做到这一点)?



非常感谢!


Unfortunately it doesn't seem to work. Can someone please tell me how I would go about doing this using BufferedReader (I would like to use BufferedReader rather than a Scanner to accomplish this task, although if I can figure out how to do it using BufferedReader, then I would be more than happy to learn how to do this via a Scanner also)?

Thank you very much!

推荐答案

List<Integer> tempBooksReference = new ArrayList<>();
List<String> tempBooksTitle = new ArrayList<>();
 
BufferedReader br = null;
try 
{
 
    br = new BufferedReader(new FileReader("C:\\Users\\SGGS\\Documents\\NetBeansProjects\\JavaApplication3\\src\\javaapplication3\\input"));

\*Here you can give full path of your input file or you have to resolve it using URL class method check documentation of URL class and pay attention to cases it String and Integer in Generics*/


    String nextLine;
    String bookTitle;
    int bookNum;
 
    while ((nextLine = br.readLine()) != null) {
 
        bookNum=Integer.parseInt(nextLine);
        tempBooksReference.add(bookNum);
        bookTitle=br.readLine();
        tempBooksTitle.add(bookTitle);
 
    }
    System.out.println(tempBooksTitle.size());
     br.close();
     Integer[] booksReference = new Integer[tempBooksReference.size()];
     String[] booksTitle = new String[tempBooksTitle.size()];
     tempBooksReference.toArray(booksReference);
     tempBooksTitle.toArray(booksTitle);
     System.out.println(Arrays.toString(booksTitle));
     
}
catch (IOException e)
{
    System.out.println(e.getMessage());
} 


这篇关于使用BufferedReader从文本文件中解析交替的字符串和整数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆