简体   繁体   English

java文件int数组读取

[英]java file int array read

I want to get an array of integers from the file .But when i get an array unwanted zeros are in the array as the size is 10 and there are only 5 integers in file(18,12,14,15,16). 我想从文件中获取一个整数数组。但是当我得到一个数组时,不需要的零在数组中,大小为10,文件中只有5个整数(18,12,14,15,16)。 How to remove those zeros. 如何删除那些零。 Code is: 代码是:

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;



public class TxtFile {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    File inFile=new File("H:\\Documents\\JavaEclipseWorkPlace\\ReadTextFile\\src\\txt.txt");
    Scanner in=null;
    int []contents = new int[10];
    int i=0;
    try {
        in=new Scanner(inFile);

        while(in.hasNextInt()){
             contents[i++]=in.nextInt();
        }
        System.out.println(Arrays.toString(contents));
    }
    catch(IOException e){
        e.printStackTrace();
    }
    finally{
        in.close();
    }

}

} }

Output is: [18, 12, 14, 15, 16, 0, 0, 0, 0, 0]. 输出为:[18,12,14,15,16,0,0,0,0,0]。

This is because you allocate an array of size 10, and the values are initialized to 0 by default. 这是因为您分配了一个大小为10的数组,默认情况下这些值初始化为0。 Then you read the 5 values from the file, and this only overwrites the first 5 values in the array, the untouched 0's are still there. 然后你从文件中读取5个值,这只会覆盖数组中的前5个值,未触及的0仍然存在。

You have a few options: 你有几个选择:

You could count the number of values you read from the file, then resize the array to match, eg: 您可以计算从文件中读取的值的数量,然后调整数组的大小以匹配,例如:

while(in.hasNextInt()){
    contents[i++]=in.nextInt();
}

// 'i' now contains the number read from the file:
contents = Arrays.copyOf(contents, i);
// contents now only contains 'i' items.
System.out.println(Arrays.toString(contents));

You could count the number of values you read from the file, then only explicitly print that many values, eg: 您可以计算从文件中读取的值的数量,然后只显式打印那么多值,例如:

while(in.hasNextInt()){
    contents[i++]=in.nextInt();
}

// 'i' now contains the number read from the file:
for (int n = 0; n < i; ++ n)
    System.out.println(contents[n]);

You could use a dynamic container like ArrayList<Integer> , and simply add values to it as you read them. 您可以使用像ArrayList<Integer>这样的动态容器,只需在读取时为其添加值即可。 Then you can support any number from the file automagically, eg: 然后,您可以自动支持文件中的任何数字,例如:

ArrayList<Integer> contents = new ArrayList<Integer>();

...
while(in.hasNextInt()){
    contents.add(in.nextInt());
}

System.out.println(contents);

I recommend the third option. 我推荐第三种选择。 It is the most flexible and easiest to deal with. 它是最灵活,最容易处理的。

将输入文件读入ArrayList<Integer>然后调用toArray返回整数数组

You can use a dynamic vector for this 您可以使用动态矢量

import java.io.File;
import java.io.IOException;
import java.util.*;
class St{
 public static void main(String args[]){
 File inFile=new File("H:\\Documents\\JavaEclipseWorkPlace\\ReadTextFile\\src\\txt.txt");
Scanner in=null;
Vector<Integer> arr=new Vector<Integer>(5,2); //5 is initial size of vector, 2 is increment in size if new elements are to be added
try {
    in=new Scanner(inFile);

    while(in.hasNextInt()){
         arr.addElement(in.nextInt());
    }
arr.trimToSize(); // This will make the vector of exact size
    System.out.println(arr.toString());
}
catch(IOException e){
    e.printStackTrace();
}
finally{
    in.close();
}
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM