简体   繁体   中英

How to put the contents of a file to a String of array in java

I have a String array which contains some records ,now i have to put that records in a file and have to read those values and have to check the records with the String array values.Here is my String array..

 public final static String fields[] = { "FileID", "FileName", "EventType",
        "recordType", "accessPointNameNI", "apnSelectionMode",
        "causeForRecClosing", "chChSelectionMode",
        "chargingCharacteristics", "chargingID", "duration",
        "dynamicAddressFlag", "iPBinV4AddressGgsn",
        "datavolumeFBCDownlink", "datavolumeFBCUplink",
        "qoSInformationNeg"};

I have to put these records in a map using these,,

 static LinkedHashMap<String, String> getMetaData1() {
  LinkedHashMap<String, String> md = new LinkedHashMap<>();
  for (String fieldName : fields) md.put(fieldName, "");

  return md;

}

now my file is

        FileID 
        FileName
        EventType
        recordType
        accessPointNameNI
        apnSelectionMode
        causeForRecClosing
        chChSelectionMode
        chargingCharacteristics
        chargingID
        duration
        dynamicAddressFlag
        iPBinV4AddressGgsn
        datavolumeFBCDownlink
        datavolumeFBCUplink
        qoSInformationNeg 

Now i am reading this file with this function

static LinkedHashMap<String, String> getMetaData() {


    LinkedHashMap<String, String> md = new LinkedHashMap<>();
    BufferedReader br = null;
    try {

        String sCurrentLine;
        String file[];
        br = new BufferedReader(new FileReader("./file/HuaGPRSConf"));

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


         md.put(sCurrentLine, "");
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    return md;
}

Now those two functions are returing values in two different ways..The String is giving

{FileID=, FileName=, EventType=, recordType=, accessPointNameNI=, apnSelectionMode=, causeForRecClosing=, chChSelectionMode=, chargingCharacteristics=, chargingID=, duration=, dynamicAddressFlag=, iPBinV4AddressGgsn=, datavolumeFBCDownlink=, datavolumeFBCUplink=, qoSInformationNeg=} 

But the file from which one i am getting the values is giving values with a big spaces..

{            FileID =,             FileName=,             EventType=,             recordType=,             accessPointNameNI=,             apnSelectionMode=,             causeForRecClosing=,             chChSelectionMode=,             chargingCharacteristics=,             chargingID=,             duration=,             dynamicAddressFlag=,             iPBinV4AddressGgsn=,             datavolumeFBCDownlink=,             datavolumeFBCUplink=,             qoSInformationNeg=,             rATType=,             ratingGroup=,             resultCode=,             serviceConditionChange=,             iPBinV4Address=,             sgsnPLMNIdentifier=,             timeOfFirstUsage=,             timeOfLastUsage=,             timeOfReport=,             timeUsage=,             changeCondition=,             changeTime=,.... so on

now when i am trying to check two values using this function they are not equal..

LinkedHashMap<String, String> md1=getMetaData();
    LinkedHashMap<String, String> md2=getMetaData1();

    if(md1.equals(md2)){
        System.out.println(md1);
    }else{
        System.out.println("Not");
    }             

i cannot understand the problem can anyone help...

您应该使用sCurrentLine.trim()删除不必要的空格。

You are checking if 2 different instances of LinkedHashMap are equal and they are not. You have to use get method of LinkedHashMap to compare values.

Also you should remove empty spaces by String trim method.

我建议在比较之前先检查数据,如果发现多余的空间,则首先应用trim()删除空间然后进行比较。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM