Java,从文本文件中读取(文本文件的格式很奇怪)

Java, reading from a text file (text file has a strange format)

我知道如何从所有信息都由行分隔的文本文件中读取数据,但是这个文本文件的格式让我难以读入和循环。我想要做的是阅读第一行,这是那里的产品数量所以我想制作一个有那么多地方的数组(不是 ArrayList),然后阅读第二行的产品代码,然后阅读第三行价格并重复,直到所有产品都已添加到数组中。

这是文件

productData.txt(#分隔值)

10
PA/1234#PV/5732#Au/9271#DT/9489#HY/7195#ZR/7413#bT/4674#LR/4992#Xk/8536#kD/9767#
153#25#172#95#235#159#725#629#112#559#

我正在为此苦苦挣扎,希望你们中的一位能告诉我它是怎么回事,或者给我指明正确的方向。

注意: 没有验证 performed.it 仅适用于完美的场景,即如果文件与您提到的结构完全相同

public static void main(String[] args) {
  
  try {
   InputStreamReader in = new InputStreamReader(new FileInputStream("D:/productData.txt"));
   BufferedReader buffer = new BufferedReader(in);
   
   String readline;
   int line_count=0;// no of line in the file
   int num_of_prod = 0; 
   float[] prod_price = null;
   String[] prod_code = null;
   
   while((readline=buffer.readLine())!=null){
    line_count++;
    // read the first line from the file 
    if(line_count==1){
     // read the first line and creating the array 
     num_of_prod = Integer.parseInt(readline);
     prod_code = new String[num_of_prod];
     prod_price = new float[num_of_prod];
    }
    
    else if(line_count==2){
     
     prod_code = readline.split("#");
    }
    
    else if(line_count == 3){
     String[] string_price_arr = readline.split("#");
     
     // converts the string array into float array for performing summation
     for(int i=0;i<num_of_prod;i++)
      prod_price[i]=Integer.parseInt(string_price_arr[i]);
     
     
    }
    
   }
   buffer.close();
   System.out.println("Product code");
   for(int i=0;i<num_of_prod;i++){
    
    System.out.println(prod_code[i]);
   }
   
   // do the same for product price
   
  } catch (IOException e) {e.printStackTrace();}
 }

如果您不想使用正则表达式,看起来您可以获取每一行并按数字字符拆分它们。这是一个例子:

public class MyTest {
    public static final void main (String[] args)
    {
        String str = "10\n" +
            "PA/1234#PV/5732#Au/9271#DT/9489#HY/7195#ZR/7413#bT/4674#LR/4992#Xk/8536#kD/9767#\n" +
            "153#25#172#95#235#159#725#629#112#559#";

        String[] arr = str.split("\n");
        ArrayList<Object[]> al = new ArrayList<>();
        String[] product = arr[1].split("#");
        String[] price = arr[2].split("#");

        for (int i = 0; i < product.length; i++)
        {
            al.add(new Object[] { product[i], Integer.parseInt(price[2]) });
        }

        System.out.println(Arrays.deepToString(al.toArray()));
    }
}

请记住,我没有进行任何完整性检查或异常检查 catching/handling,您绝对应该这样做。这只是一个简单的示例,可以帮助您继续前进。

注意:我没有文件,也没有带有包含价格和产品代码的构造函数的 class 产品,但我相信他的会起作用。

我不得不做类似的事情,我在 csv 文件中分隔值(逗号分隔值),所以我使用了类似的东西。

File productData = new File("productData.txt");
Scanner sc = new Scanner(productData);
Integer size = new Integer(sc.nextLine());
Product[] products = new Product[size];
String[] code = new String[size];
int[] price = new int[size];
int countLine = 0;

while (sc.hasNextLine())
{
    int count = 0;
    String line = new String(sc.nextLine());
    for (String retval: line.split("#"))
    {
        if (countLine == 0) {
            code[count] = retval;
        } else if (countLine == 1) {
            price[count] = Double.parseDouble(retval);
        }
        count++;
    }
    countLine++;
}

for (int i = 0; i < size; i++)
{
    products[i] = new Product(code[i], price[i]);
}

我在这里做的是首先导入文件,然后创建一个新的扫描仪。我从第一行中的整数创建了一个名为 size 的新 int。接下来,我制作了 3 个阵列。一份用于最终产品,一份用于产品代码,一份用于价格。我还做了一个整数来计算我在哪一行。现在,我做了一个 while 循环,一直持续到没有更多行为止。我创建了一个名为 line 的字符串,即文件中的行,我用 # 将其分开。接下来我创建了一个 for 循环,遍历每个产品 code/price 并将其转换为一个名为 retval(return value) 的字符串。然后我做了一个 if 语句,如果它在产品代码行上,然后将这些代码中的每一个设置为数组 code。否则,如果它在价格线上,则将这些价格中的每一个都设置到数组 price 中。最后,我使用 for 循环将代码数组和价格数组组合到产品数组中。