-1

我有一个文本文件 textfile.txt ,其中包含如下数据:

A-abc , A-xyz , B-mno , A-ijk , B-pqr

现在,我必须从这个文件中读取并将值存储在两个单独的数组中"A""B"这样带有前缀的值"A-"就存储在数组 A 中,而前缀为“B-”的值存储在数组 B 中。

此外,在存储数据时,需要删除前缀,即只"abc"需要存储在array A.

FileInputStream fstream = new FileInputStream("C:\opt\New_Workspace\Salary.txt"); 
// use DataInputStream to read binary NOT text
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null)   {
String[] arrayLine1= strLine.split(" , ");
for(String s:arrayLine1)

String[] arrayLine2 = s.split(": ");
{
if(s.matches("Basic: "))
{
basic = Double.parseDouble(arrayLine[1]);                 
}
else if(s.matches("Perc-D ");
{
percD = Double.parseDouble(arrayLine[3]);                
}
else if(s.matches("Perc-A: "))
{
percA = Double.parseDouble(arrayLine[5]);                 
}

}
4

2 回答 2

1

尝试这样的事情: -

FileInputStream in = new FileInputStream("file.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine;
    String[] filearray;
    filearray = new String[10];

    while ((strLine = br.readLine()) != null) {

    for (int j = 0; j < myarray.length; j++){
    filearray[j] = br.readLine();
    }

    }
    in.close();
于 2013-10-29T11:42:30.270 回答
0

写的比较匆忙,如有错误请见谅:

String aStore = "";
String bStore = "";
String aFinal[];
String bFinal[];
try{
    Scanner input = new Scanner(new File("file.txt"));
    while(input.hasNextLine()){
        String message = input.nextLine();
        message = message.replace(" ", "");
        String store[] = message.split(",");
        for(int a = 0; a < store.length; a++){
            if((store[a]).contains("A-"){
                String t[] = (store[a]).split("-");
                aStore = aStore + "_" + t[1];
            }
            if((store[a]).contains("B-"){
                String t[] = (store[a]).split("-");
                bStore = bStore + "_" + t[1];
            }
        }
        aFinal = aStore.split("_");
        bFinal = bStore.split("-");
    }
    input.close();
}
catch(Exception e){}
于 2013-10-29T11:53:37.670 回答