簡體   English   中英

如何在Java中跳過第一個int逐行讀取文件到數組?

[英]How to read file line by line to an array in Java skipping first int?

我試圖讀取Java中的文件並將其存儲到數組中。 現在的問題是該文件具有以下結構。 我將在下面給出2個示例文件:

輸入文件結構

<number of lines>
<line number> <value>
.
.
.
<line number> <value>

input1.txt

5 
1 34
2 19
3 43
4 62
5 36

input2.txt

4
1 10.3430423
2 -34.234923
3 -100.39292
4 22

如您所見,文件以行數開頭(例如4或5)。 在普通輸入文本中,我有超過100000行。

因此,我的代碼基本上可以獲取用戶輸入,打開文件,創建數組大小和該大小的數組。 現在,我繼續閱讀下一行並將值添加到我的elements數組中。 行號不應添加到數組中。 現在您可以看到,我的elements數組被聲明為String。 是否可以實際讀取文件,獲取值的類型並創建該類型的數組? 我認為可以節省從字符串到int或double或float的轉換?

下面是我的代碼:

   public static void main(String args[]) throws NumberFormatException, IOException{
        String inFile; //Input file name.
        int filterSize; //Filter size (odd integer >= 3).
        String outFile; //Output file name.
        int arraySize;
        String[] elements;
        int index = 0;

        //Scanner to take input file name, filter size and output file name.
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Enter your keyboard input as follows: <data file name> <filter size(odd int >= 3> <output file name>");

        //Assigning values to variables.
        inFile = keyboardInput.next();
        filterSize = keyboardInput.nextInt();
        outFile = keyboardInput.next();


        //Reading file into array using BufferReader
        BufferedReader fileInput = new BufferedReader(new FileReader(inFile));
        arraySize = Integer.parseInt(fileInput.readLine()); //Get Array Size

        elements = new String[arraySize];

        while(fileInput.readLine() != null){
            elements[index] = fileInput.readLine();
            index += 1;
        }               
    }

任何幫助表示贊賞。 謝謝。

嘗試這樣做:

    Scanner sc = new Scanner(new File(inFile));
    arraySize = sc.nextInt();
    elements = new String[arraySize];

    while(sc.hasNext())
    {
        sc.nextInt();
        elements[index] = sc.next();
        index += 1;
    }               

創建新的掃描儀后,無需任何轉換即可讀取整數,布爾值等。 因為您不需要當前編號的行,所以只需閱讀該編號即可。 您無需將其保存在任何地方。 然后,下一個數字/字符串必須保存在elements[index] 而已

基於流:

Files.lines(pathToFile).skip(1) // skip the line counter. Don't trust input
   .map(line -> line.replaceFirst("\\d+", "")) // remove digits at the start
   .collect(Collectors.toList()); // collect it into a list

您仍然可以使用.toArray()將其存儲到數組中

但是實際上您應該使用try-with-resources來做到這一點:

try (Stream<String> lines = Files.lines(pathToFile).skip(1)) {
    elements = lines.map(line -> line.replaceFirst("\\d", "")).collect(Collectors.toList());
} catch (IOException e) {
    // sensible handling...
}

閱讀號碼,切勿使用。

File file = new File("input1.txt");
Scanner in = new Scanner(file);
int lines = in.nextInt();
int []a = new int[lines];
for(int i = 0; i < lines; i++)
{
 int fakeNumber = in.nextInt();//read it but never used it
 a[i] = in.nextInt(); 
}

您也可以in#hasNextLine()使用。

真正的問題是如何刪除行號並獲取其值。

double[] array = .... // initialize your array
int v = 0;
while(file.readLine() != null){
      // your array
      array[v++] = Double.parseDouble(file.readLine().split(" ")[1]);
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM