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

时间:2015-03-17 17:47:12

标签: java arrays arraylist concatenation

下面是文本文档的样子。第一行是我希望数组包含的元素数。第二个是产品的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

5 个答案:

答案 0 :(得分:2)

你去全班,完全按照自己的意愿行事:

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("#");以使用{{1}分割每个标记作为分隔符并创建数组,#将元素放在一个组合数组中......!

combinedArray[i]=strArray[i]+"_"+dataArray[i];

编辑:所有东西一起从main内部调用一个单独的方法,文件作为输入参数!

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

输出

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;
}
}

答案 1 :(得分:1)

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

Read more for split method here.

答案 2 :(得分:0)

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


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) {
    }
}

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

你可以尝试这种方式.. 逐行读取并将每行存储在数组中。 存储时使用,以便分割并保存。

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

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

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