将 .txt 文档中的数据添加到数组

Adding data from .txt document to array

下面是文本文档的样子。第一行是我希望数组包含的元素数。第二行是产品ID,用#隔开,第三行是产品总价,再次用#

隔开
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#

我想使用以下方法将 inputFile 传递给 readProductDataFile 方法:

public static Product[] readProductDataFile(File inputFile){
    // Code here
}

我想创建一个大小为 10 的数组,或者一个 arrayList。最好是客户 ID 和价格的串联,例如 Array[1] = PA/1234_153

这就是完整的 class,完全符合您的要求:

import java.io.BufferedReader; 
import java.io.FileReader;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.io.IOException;
class myRead{
    public static void main(String[] args) throws FileNotFoundException, IOException {
        BufferedReader inputFile = new BufferedReader(new FileReader("test.txt")); 
        String numberOfElements = inputFile.readLine();
        //this is the first line which contains the number "10"
        //System.out.println(numberOfElements);
        String secondLine = inputFile.readLine();
        //this is the second line which contains your data, split it using "#" as a delimiter
        String[] strArray = secondLine.split("#");
        //System.out.println(Arrays.toString(strArray));
        //System.out.println(strArray[0]);
        String thirdLine = inputFile.readLine();
        //this is the third line which contains your data, split it using "#" as a delimiter
        String[] dataArray = thirdLine.split("#");
        //combine arrays
        String[] combinedArray = new String[strArray.length];
        for (int i=0;i<strArray.length;i++) {
        combinedArray[i]=strArray[i]+"_"+dataArray[i];
        System.out.println(combinedArray[i]);
        }
}
}

输出:

PA/1234_153
PV/5732_25
Au/9271_172
DT/9489_95
HY/7195_235
ZR/7413_159
bT/4674_725
LR/4992_629
Xk/8536_112
kD/9767_559

我正在做的事情是使用 BufferedReader 读取文件,readLine 读取三行中的每一行,split("#"); 使用 # 作为分隔符并创建数组,combinedArray[i]=strArray[i]+"_"+dataArray[i]; 将元素放入您想要的组合数组中...!

public static Product[] readProductDataFile(File inputFile){
BufferedReader inputFile = new BufferedReader(new FileReader(inputFile));
// the rest of my previous code goes here 

编辑:所有内容以及从 main 内部调用一个单独的方法,并将文件作为输入参数!

import java.io.BufferedReader; 
import java.io.FileReader;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
class myRead{
    public static void main(String[] args) throws FileNotFoundException, IOException {
        File myFile = new File("test.txt"); 
        readProductDataFile(myFile);
        }
        public static String[] readProductDataFile(File inputFile) throws FileNotFoundException, IOException{
        BufferedReader myReader = new BufferedReader(new FileReader("test.txt")); 
        String numberOfElements = myReader.readLine();
        //this is the first line which contains the number "10"
        //System.out.println(numberOfElements);
        String secondLine = myReader.readLine();
        //this is the second line which contains your data, split it using "#" as a delimiter
        String[] strArray = secondLine.split("#");
        //System.out.println(Arrays.toString(strArray));
        //System.out.println(strArray[0]);
        String thirdLine = myReader.readLine();
        //this is the third line which contains your data, split it using "#" as a delimiter
        String[] dataArray = thirdLine.split("#");
        //combine arrays
        String[] combinedArray = new String[strArray.length];
        for (int i=0;i<strArray.length;i++) {
        combinedArray[i]=strArray[i]+"_"+dataArray[i];
        System.out.println(combinedArray[i]);
}
        return combinedArray;
}
}

输出

PA/1234_153
PV/5732_25
Au/9271_172
DT/9489_95
HY/7195_235
ZR/7413_159
bT/4674_725
LR/4992_629
Xk/8536_112
kD/9767_559

您甚至不需要第一行。只需将第二行直接读入一个字符串,然后使用 String,split() 方法将其拆分。

Read more for split method here.

你可以使用类似这样的东西(请注意我目前无法测试它)


BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader("fileeditor.txt"));
    String read = null;
    String firstLine=in.readLine();
   //reads the first line
    while ((read = in.readLine()) != null) {
   // reads all the other lines 
        read = in.readLine();
        String[] splited = read.split("#");
        //split the readed row with the "#" character
        for (String part : splited) {
            System.out.println(part);
        }
    }
} catch (IOException e) {
    System.out.println("There was a problem: " + e);
    e.printStackTrace();
} finally {
    try {
        //close file
        in.close();
    } catch (Exception e) {
    }
}

这是使用 Java 的方法(不要忘记导入):

public static Product[] readProductDataFile(File inputFile){
    Scanner s = new Scanner(inputFile);
    String data = "";
    while(s.hasNext())
        data += s.nextLine();
String[] dataArray = data.split("#");
}

你可以这样试试.. 逐行读取并将每一行存储在数组中。 存储时使用,以便拆分并保存。

String[] strArray = secondLine.split("#");

现在使用 for 循环并根据需要连接值并保存在第三个数组中。

For(int i=0 ;i< file.readline;i++)
{
   string s = a[customerid];
   s.concat(a[productid]);
a[k] =s;
}