read Int Array from InputStream - Java java.lang

Java examples for java.lang:Math Array Function

Description

read Int Array from InputStream

Demo Code


//package com.java2s;

import java.io.*;
import java.util.*;

public class Main {
    public static int[] readIntArray(InputStream stream) throws IOException {
        List<Integer> listRep = new ArrayList<Integer>();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                stream));// w  ww  .  ja  va 2 s. co  m
        while (reader.ready()) {
            String line = reader.readLine();
            String[] vals = line.trim().split("\\s+");
            for (String val : vals) {
                listRep.add(Integer.parseInt(val));
            }
        }
        reader.close();

        int size = listRep.size();
        int[] arrayRep = new int[size];

        for (int i = 0; i < size; i++) {
            arrayRep[i] = listRep.get(i);
        }

        return arrayRep;
    }
}

Related Tutorials