Java: reading an array of integers from a file

Let's say I have a file called "input.txt" that has a bunch of positive integers in it:

 6 5 6 8 6 2 4 

etc .... (one integer per line)

I want to read this file and make it into an array. The first integer (in this case 6) indicates the number of indices or elements in the array, therefore 6 points. The remaining numbers fill the array starting at 0. Thus, at index 0, the number is 5, at index 1 - 6, etc.

Can someone please show me how to read this file and make it into an array named A and return the integers in each index as n?

this is what i have so far:

 import java.io.*; public class inputFile { public static jobScheduleRecursive(int[] A, int i) { try { FileReader filereader = new FileReader("input.txt"); BufferedReader bufferedreader = new BufferedReader(filereader); String line = bufferedreader.readLine(); //While we have read in a valid line while (line != null) { //Try to parse integer from the String line try { System.out.println(Integer.parseInt(line)); } catch (NumberFormatException nfe) { System.err.println("Failed to parse integer from line:" + line); System.err.println(nfe.getMessage()); System.exit(1); } line = bufferedreader.readLine(); } } catch(FileNotFoundException filenotfoundexception) { System.out.println("File not found."); } catch(IOException ioexception) { System.out.println("File input error occured!"); ioexception.printStackTrace(); } return A; } 

I think I'm doing something completely wrong. please, help.

+7
source share
5 answers

Using Scanner and Scanner.nextInt() , you can solve this in just a few lines:

 Scanner s = new Scanner(new File("input.txt")); int[] array = new int[s.nextInt()]; for (int i = 0; i < array.length; i++) array[i] = s.nextInt(); 
+12
source

I think you need this for ACM-like contests :) I use the following template:

 import java.io.*; import java.util.*; public class Task { private BufferedReader input; private PrintWriter output; private StringTokenizer stoken; String fin = "input"; String fout = "output"; private void solve() { // some solving code... int n = nextInt(); int[] mas = new int[n]; for (int i = 0; i<n; i++){ mas[i] = nextInt(); } } Task() throws IOException { input = new BufferedReader(new FileReader(fin + ".txt")); output = new PrintWriter(new FileWriter(fout + ".txt")); solve(); input.close(); output.flush(); output.close(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextFloat() { return Float.parseFloat(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String nextToken() { while ((stoken == null) || (!stoken.hasMoreTokens())) { try { String line = input.readLine(); stoken = new StringTokenizer(line); } catch (IOException e) { e.printStackTrace(); } } return stoken.nextToken(); } public static void main(String[] args) throws IOException { new Task(); } } 

In the solve () method, you can see how to read one number N (the length of the next sequence of numbers), and after that in the cycle (0..N) I read the integers from the input (in this case, the input is a file).

+5
source

Java 8+

 int[] ints = Files.lines(Paths.get("input.txt")) .mapToInt(Integer::parseInt).toArray(); 
+4
source
 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class filee{ public static void main(String[] args) throws FileNotFoundException { File f = new File("l.txt"); Scanner b = new Scanner(f); int[] arr = new int[b.nextInt()]; for(int i = 0; i < arr.length; i++){ arr[i] = b.nextInt(); } for (int o : arr){ System.out.println(o); } } } 
+1
source

If the file is a classpath resource:

 int[] ints = Files .lines(Paths.get(ClassLoader.getSystemResource("input.txt") .toURI())).mapToInt(Integer::parseInt).toArray(); 

Printing contents from a file:

  Arrays.stream(ints).forEach(System.out::println); 
0
source

All Articles