如何从文本文件中读取并存储在 Java 中的字符串数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19657219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:11:44  来源:igfitidea点击:

How to read from text file and store in String array in Java?

javaarraysbufferedreaderfilereader

提问by NGB

I have a text file textfile.txt which contains data as follows :

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

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

Now, i have to read from this file and store the values in two separate arrays "A"and "B", such that the values with prefix "A-"gets stored in array A and values with prefix "B-" gets stored in array B.

现在,我已经从该文件的读取和所述值存储在两个单独的阵列"A""B",使得与前缀值"A-"被存储在数组A和前缀“B-”的值被存储在阵B.

Also, while storing the data, the prefix needs to be removed i.e. only "abc"needs to be stored in array A.

此外,在存储数据时,需要删除前缀,即只"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]);                 
}

}

回答by Scientist

Try something like this :-

尝试这样的事情:-

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();

回答by mkaminsky

This was written in a hurry, so please pardon any errors:

这是匆忙写的,所以请原谅任何错误:

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