2011-10-09 134 views
7

說我有一個名爲"input.txt"文件中有一堆正整數:
的Java:整數讀陣列從文件

6 
5 
6 
8 
6 
2 
4 

等....(每行一個整數)

我想讀取此文件並將其製作成數組。第一個整數(在本例中爲6)告訴數組中的索引或元素的數量,因此有6個點。其他數字從0開始填充數組。所以在索引0處,數字是5,在索引1處數字是6,依此類推。

有人可以告訴我如何讀取這個文件,並將其放入一個名爲A的數組中,並將每個索引中的整數返回爲n?

這是我到目前爲止有:

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; 
} 

我覺得我做的事情完全錯誤的。請幫忙。

+2

聽起來像...功課? – 2011-10-09 18:42:11

+0

如果您使用List結構來存儲數字,則不必將條目數放在第一行。一旦通過調用list.toArray() –

回答

12

使用ScannerScanner.nextInt()方法,你可以在短短的幾行解決這個問題:

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(); 
+0

完成閱讀,您可以輕鬆地將List轉換爲數組(一個固定長度)。對於每個值,循環顯示看起來是一個糟糕的表現。 – AndroidDev

+0

可能有更快的解決方案。直到應用程序被分析並識別出瓶頸之後,我纔會去探索這些問題。 – aioobe

5

我認爲你需要這個ACM般的比賽:)我用下面的模板:

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(); 
    } 

} 

在solve()方法中,您可以看到如何讀取一個數字N(以下數字序列的長度),然後在循環(0..N)中從輸入讀取整數(在這種情況下,輸入是文件) 。

1
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); 
     } 
    } 
} 
4

的Java 8+

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

你也可以做Files.lines(Paths.get(「input.txt」))。mapToInt(Integer :: parseInt).boxed();得到一個非基元列表。 –

0

如果文件是classpath資源:

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

從打印文件中的內容:

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