[英]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#
我为此感到挣扎,希望你们中的一个可以向我展示它的状况如何或向正确的方向指出。
注意:我没有该文件,也没有包含带有价格和产品代码的构造函数的Product类,但是我认为他可以使用。
我必须做类似的事情,我将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]);
}
我在这里所做的是先导入文件,然后创建一个新的扫描仪。 我从第一行中的整数创建了一个新的int,称为size。 接下来,我制作了3个数组。 一种用于最终产品,一种用于产品代码,一种用于价格。 我还做了一个整数来计算我在哪一行。 现在,我做了一个while循环,直到没有更多的行了。 我创建了一个名为line的字符串,即文件内的那一行,然后用#将其拆分。 接下来,我创建了一个for循环,遍历每个产品代码/价格,并将其转换为名为retval(返回值)的字符串。 然后,我做了一个if语句,如果它在产品代码行上,那么将这些代码中的每一个都设置为数组code。 否则,如果它在价格线上,则将这些价格中的每一个设置为价格数组。 最后,我使用for循环将codes数组和prices数组组合到products数组中。
如果您不想使用正则表达式,则看起来只需将每一行并用数字字符分隔即可。 这是一个例子:
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()));
}
}
请记住,我没有做任何健全性检查或异常捕获/处理,您绝对应该这样做。 这只是一个快速的示例,可以帮助您入门。
注意:不执行任何验证。它仅在理想情况下起作用,即文件是否与您提到的结构完全相同
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();} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.