Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- All you need to do is loop over every line in the file and store the values as integers.
- Easiest way to populate an array of integers from a file:
- <!-- language: lang-java -->
- import java.io.*;
- import java.util.*;
- public class ReadFile {
- public static void main(String[] args){
- BufferedReader br = null;
- Integer[] array = null;
- String filename = "input.txt";
- try {
- List<Integer> list = new ArrayList();
- String line = "";
- br = new BufferedReader(new FileReader(filename));
- while ((line = br.readLine()) != null)
- list.add(Integer.parseInt(line));
- array = list.toArray(new Integer[list.size()]);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (br != null)
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (array != null) {
- for (Integer i : array)
- System.out.printf("%d", i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement