Java FileReader Create readTextFile(File file, int header)

Here you can find the source of readTextFile(File file, int header)

Description

Generic function to read a text file into a double[][] with an arbitrary number of columns.

License

Open Source License

Parameter

Parameter Description
file is the full path of the text file to read.
header is the number of lines of header to skip

Return

double[][] {column_1, ..., column_N}

Declaration

public static String[][] readTextFile(File file, int header) 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.IOException;
import java.util.ArrayList;

public class Main {
    /**//from  w ww . ja v  a2  s .c om
     * Generic function to read a text file into a double[][] with an arbitrary number of columns.
     * 
     * @param file is the full path of the text file to read.
     * @param header is the number of lines of header to skip
     * @return double[][] {column_1, ..., column_N}
     */
    public static String[][] readTextFile(File file, int header) {

        String[][] output = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            // skip the header
            for (int i = 1; i <= header; i++) {
                reader.readLine();
            }
            // first line, set up number of columns
            String line = reader.readLine();
            String[] tokens = tokenize(line);
            int columns = tokens.length;
            // initialize
            ArrayList[] listArr = new ArrayList[columns];
            for (int a = 0; a < columns; a++) {
                listArr[a] = new ArrayList<String>();
            }
            // get strings from first line
            for (int s = 0; s < columns; s++) {
                listArr[s].add(tokens[s]);
            }
            // read the rest of the file, adding token strings to the lists
            while ((line = reader.readLine()) != null) {
                tokens = tokenize(line);
                if (tokens.length < columns) {
                    for (int i = 0; i < tokens.length; i++) {
                        System.out.println(i + " = " + tokens[i]);
                    }
                }
                for (int s = 0; s < columns; s++) {
                    String string = tokens[s];
                    listArr[s].add(string);
                }
            }
            // arraylists full of strings, read to arrays
            output = new String[columns][listArr[0].size()];
            for (int i = 0; i < listArr[0].size(); i++) {
                for (int a = 0; a < columns; a++) {
                    output[a][i] = (String) listArr[a].get(i);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return output;
    }

    /**
     * Tokenizing helper.  Works on space, tab and comma separated files.
     * 
     * @param input is a line to be tokenized
     * @return a String[] of the strings in between the delimiters
     */
    public static String[] tokenize(String input) {
        if (input.contains(",")) { // comma delimited
            String[] ret = input.split(",");
            // trim leading whitespace
            for (String s : ret) {
                s.trim();
            }
            return ret;
        } else if (input.contains("\\t")) { // tab delimited
            return input.split("\\t+");
        } else { // whitespace delimited
            return input.trim().split("\\s+");
        }
    }
}

Related

  1. readTextFile(File file)
  2. readTextFile(File file)
  3. readTextFile(File file)
  4. readTextFile(File file)
  5. readTextFile(File file, boolean newline)
  6. readTextfile(final String filename)
  7. readTextFile(InputStream in)
  8. readTextFile(InputStream in)
  9. readTextFile(String completePath)