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

Java 将.txt文档中的数据添加到数组,java,arrays,arraylist,concatenation,Java,Arrays,Arraylist,Concatenation,下面是文本文档的外观。第一行是我希望数组包含的元素数。第二行是产品的ID,用#分隔,第三行是产品的总价,再次用#分隔# 我想使用以下方法将inputFile传递到readProductDataFile方法: public static Product[] readProductDataFile(File inputFile){ // Code here } 我想创建一个大小为10的数组,或者一个arrayList。最好是客户ID和价格的串联,例如数组[1]=PA/1234_153在这里

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

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

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

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

在这里,您可以使用完整的类,它完全满足您的需要:

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]);
        }
}
}
我所做的技巧是使用
BufferedReader
读取文件,
readLine
读取三行中的每一行,
split(#)
使用
#
作为分隔符分割每个令牌并创建数组,以及
组合数组[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;
}
}

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


您可以使用类似的方法(请注意,我目前无法对其进行测试)



这是使用Java实现的方法(别忘了导入):

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

String[] strArray = secondLine.split("#");
现在使用for循环和concat值,并保存在第三个数组中

For(int i=0 ;i< file.readline;i++)
{
   string s = a[customerid];
   s.concat(a[productid]);
a[k] =s;
}
For(int i=0;i
这毫无意义,一定是字符串secondLine=inputFile.readLine();只是要再次读取整个文件,而不是第二行..这非常有意义。我编写了整个类,它打印您想要的元素
PA/1234
。。只需编译并运行该类,然后自己查看..查看我的编辑,完全按照您的要求执行,甚至像您所希望的那样使用“u”符号连接两个数组…啊,我理解,谢谢。我也在为传递什么而挣扎。在我的例子中,我希望传递给方法类型文件,而在您的示例中,您使用了FileReadery,您可以将该文件作为参数传递,然后像我一样读取并创建数组和所有内容。。请接受我在这里描述的答案:如果你还有其他问题,请发布另一个问题。好的,那么我如何将其放入数组?请看我提供给你的链接。函数声明为“公共字符串[]拆分(字符串正则表达式)”。它返回一个字符串数组。只需执行以下操作:String[]myArray=LineYourReadFromFile.split(“#”);
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 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) {
    }
}
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(int i=0 ;i< file.readline;i++)
{
   string s = a[customerid];
   s.concat(a[productid]);
a[k] =s;
}