简体   繁体   English

Java使用外部文本文件中的数据

[英]Java Use Data from External Text File

I have a text file containing the first 1000 prime numbers and I have written a method to read the data from said text file. 我有一个包含前1000个质数的文本文件,并且我编写了一种从所述文本文件读取数据的方法。

I would like to know how to use the data from this file and apply it to another method. 我想知道如何使用此文件中的数据并将其应用于另一种方法。

Something along the lines of: 类似于以下内容:

read data from file;
use first eight numbers and apply to this method;

Any help would be appreciated. 任何帮助,将不胜感激。

Read the file and store each number into an array or list. 读取文件并将每个数字存储到数组或列表中。 You can then get the numbers you need by using the index of the array. 然后,您可以使用数组的索引获取所需的数字。

Reading from file is simple, assuming you have one line per number - 假设每个数字只有一行,那么从文件中读取数据很简单-

BufferedReader br = new BufferedReader(new FileReader("<your-text-file>"));
String txtNum;
while((txtNum = br.readLine()) != null)
{
   //txtNum is the number read, use it however you need
   if (txtNum.length() > 8) {
      thisMethod(txtNum.substring(0, 8));
   }
}

Use Scanner to read the data from the file and store each line in an array (or ArrayList ) of int s. 使用Scanner从文件中读取数据,并将每一行存储在int的数组(或ArrayList )中。 The get a subset of that array and pass it to a method supporting variable-length arguments (for example). 获取该数组的子集,然后将其传递给支持可变长度参数的方法(例如)。 Something like : 就像是 :

public static void main(String[] args) {
   ArrayList<Integer> primes = new ArrayList<Integer>();
   readPrimeData(new File("path/to/data"), primes);
   someMethod(primes.subList(0, 8).toArray(new Integer[0]));
}

static public boolean readPrimeData(File dataFile, ArrayList<Integer> data) {
   boolean err = false;
   try {
      Scanner scanner = new Scanner(dataFile);
      String line;
      while (scanner.hasNext()) {
         line = scanner.nextLine();
         try {
            data.add(Integer.parseInt(line));
         } catch (NumberFormatException e) {
            e.printStackTrace();
            err = true;
         }
      }
      scanner.close();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
      err = true;
   }

   return err;
}


static public void someMethod(Integer...primes) {
   // primes is Integer[]
}

The method someMethod can also be called like 方法someMethod也可以像

someMethod(1, 2, 3, 5, 7);

First, you have to know how you information is organized. 首先,您必须知道信息的组织方式。 For instance, you could have your 1000 first prime nunbers organized this way: 例如,您可以以这种方式组织您的1000名第一任主要修女:

1
2
3
5
7...

Or this way: 或者这样:

1-2-3-5-7-11...

You can use the StringBuilder (or just a String) to keep the numbers of your file (assuming that your text is like the second way above). 您可以使用StringBuilder(或仅使用String)来保留文件编号(假设您的文本类似于上面的第二种方式)。 As the numbers are separated by a dash you could use a substring that can take the first 8 numbers to some method. 由于数字之间用短划线隔开,因此您可以使用可以将前8个数字带入某种方法的子字符串。

BufferedReader br = new BufferedReader(new FileReader("Prime Nunbers.txt"));
String num;
int count = 1;
while((num= br.readLine()) != null) {
      if( count <= 8 )     
        someMethod( num.subString(0, num.indexOf("-")) );
}

But if you have your numbers organized like the 1st way (one number per line) you could do something like this: 但是,如果您像第一种方法那样组织数字(每行一个数字),则可以执行以下操作:

BufferedReader br = new BufferedReader(new FileReader("Prime Nunbers.txt"));
String num;
int count = 1;
while((num = br.readLine()) != null) {
        if( count <= 8 )
           someMethod( num );
        num = "";
}

If you want to use these first 8 numbers at once you can just read the file entirely and then use some substring depending the way these numbers are read. 如果要一次使用前8个数字,则可以完全读取文件,然后根据这些数字的读取方式使用一些子字符串。

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

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