Improving data extraction from a text file in Java

I have a CSV file with sample data in this form:

220 30    255   0   0     Javascript
200 20      0 255 128     Thinking in java

where the first column is the height, the second thickness, the next three are the rgb values ​​for the color, and the last is the header. Everything should be considered as separate variables. I already wrote my own solution for this, but I am wondering if there is a better / easier / shorter way to do this. The extracted data will then be used to create the Book object, toss each book into an array of books and print it using swing. Here is the code:

private static Book[] addBook(Book b, Book[] bookTab){
        Book[] tmp = bookTab;
        bookTab = new Book[tmp.length+1];
        for(int i = 0; i < tmp.length; i++){
                bookTab[i] = tmp[i];
        }
        bookTab[tmp.length] = b;

        return bookTab;
}

public static void main(String[] args) {

    Book[] books = new Book[0];

    try {
        BufferedReader file = new BufferedReader(new FileReader("K:\\books.txt"));

        String s;
        while ((s = file.readLine()) != null) {
            int hei, thick, R, G, B;
            String tit;

            hei = Integer.parseInt(s.substring(0, 3).replaceAll(" ", ""));
            thick = Integer.parseInt(s.substring(4, 6).replaceAll(" ", ""));
            R = Integer.parseInt(s.substring(10, 13).replaceAll(" ", ""));
            G = Integer.parseInt(s.substring(14, 17).replaceAll(" ", ""));
            B = Integer.parseInt(s.substring(18, 21).replaceAll(" ", ""));

            tit = s.substring(26);

            System.out.println(tyt+wys+grb+R+G+B);

            books = addBook(new Book(wys, grb, R, G, B, tyt),books);
        }
        file.close();
    } catch (IOException e) {
        //do nothing
    }
}
+3
source share
4 answers

I have a CSV file with sample data in this form

CSV. .

, / /

CSV. / Java CSV API. OpenCSV. List beans (, Book ) CSV .

( ) , . ?

Regex , , ! , CSV, , , . replaceAll(" ", "") trim(), ( , - ). Book[] List<Book> , . books.add(book). . .

+1

, / . , CSV ( , ), StringTokenizer. ( , , ).

0

java.util.Scanner, Java 5. .

, (. /):

import java.util.Scanner;
import java.io.File;
import java.lang.String;

class Dummy
{
    public static void main(String[] args) throws Exception
    {
       Scanner sc = new Scanner(new File("file.txt"));
       while(sc.hasNext())
       {
               int hei = sc.nextInt();
               int thick = sc.nextInt();
               int r = sc.nextInt();
               int g = sc.nextInt();
               int b = sc.nextInt();
               String title = sc.nextLine().trim();

               System.out.println("Book(" + hei + "," + thick + "," + 
               r + "," + g + "," + b + "," + title + ")");
       }
    }
}

The good thing about the Scanner is that it has conductors that take strings, files, or other input sources, so you can use them in almost everything. Hope this helps!

0
source

Source: https://habr.com/ru/post/1744425/


All Articles