简体   繁体   English

我的Java程序未读取我的文件

[英]My files aren't read by my java program

I have some Java code like this : 我有一些这样的Java代码:

Map<Map<String,String>,String> map = new HashMap<>();
int i = 0;
try (BufferedReader br = new BufferedReader(new FileReader("properties.txt")))
{

        String sCurrentLine;
        while ((sCurrentLine = br.readLine()) != null) {
                i++;
                String[] parts = sCurrentLine.split(",");
                System.out.println(parts[2]);
                Map<String,String> tempMap = new HashMap<>();
                tempMap.put("issuing_bank",parts[1]);
                tempMap.put("card_switch",parts[2]);
                tempMap.put("card_Type",parts[3]);
                map.put(tempMap,parts[0]);
        }

} catch (IOException e) {
        e.printStackTrace();
}

It looks strange that my map contains only first 12 elements that are stored from my text file. 我的map仅包含从我的文本文件存储的前12元素,这看起来很奇怪。 For debugging purpose I have used the variable i and print that out, which is printing the value of 22 , which is the exact count in my text file. 出于调试目的,我使用了变量i并将其打印出来,这将打印出22的值,这是我的文本文件中的确切计数。

My text file looks like this: 我的文本文件如下所示:

447747,ICCI,Visa,Credit
421323,ICCI,Visa,Debit
421630,ICCI,Visa,Debit
455451,ICCI,Visa,Debit
469375,ICCI,Visa,Debit
523951,ICCI,MasterCard,Credit
5399,ICCI,MasterCard,Debit
517652,HDFC,MasterCard,Credit
558818,HDFC,MasterCard,Credit 
512622,SBI,MasterCard,Credit
526468,SBI,MasterCard,Credit
400975,Citi,Visa,Credit
402856,Citi,Visa,Credit
461726,Citi,Visa,Credit
552004,Citi,MasterCard,Debit
468805,Axis,Visa,Debit
418157,ICCI,Visa,Debit
524133,Citi,MasterCard,Credit
528945,HDFC,MasterCard,Credit
437748,SBI,MasterCard,Credit
524111,HDFC,MasterCard,Credit
431757,SBI,Visa,Credit

I'm very much confused, why only 12 elements are read into my map . 我非常困惑,为什么我的map只读取了12元素。 Am I missing something here? 我在这里想念什么吗?

Thanks in advance. 提前致谢。

The solution is simple: you have the wrong argument order in this line: 解决方案很简单:您在此行中使用了错误的参数顺序:

map.put(tempMap,parts[0]);

it should say 它应该说

map.put(parts[0],tempMap);

You must change the type parameters of your variable declaration accordingly. 您必须相应地更改变量声明的类型参数。 Where you have 你在哪里

Map<Map<String,String>,String> map = new HashMap<>();

you must put 你必须把

Map<String,Map<String,String>> map = new HashMap<>();

Altogether, after these changes I believe you will have the structure you really want to have: a map from parts[0] to the map of the rest of the record fields. 总之,经过这些更改,我相信您将拥有真正想要的结构:从parts[0]到其余记录字段的映射的映射。

I should add that your solution (in addition to your nick :) gives you away as a developer who primarily codes in a dynamic language like Groovy; 我要补充一点,您的解决方案(除了您的昵称:)还使您摆脱了以主要使用Groovy之类的动态语言进行编码的开发人员的身份。 this style is not a good match for Java's language features. 这种样式与Java的语言功能不是很好的匹配。 In Java you'd be better off defining a specialized bean class: 在Java中,最好定义一个专门的bean类:

public class CardHolder {
  public final String cardNumber, issuingBank, cardSwitch, cardType;

  public CardHolder(String[] record) {
    int i = 0;
    cardNumber = record[i++];
    issuingBank = record[i++];
    cardSwitch = record[i++];
    cardType = record[i++];
  }
}

First, this approach is nicer since your reading loop becomes simpler and more to the point: 首先,这种方法更好,因为您的阅读循环变得更简单,而且更有意义:

while ((sCurrentLine = br.readLine()) != null) {
  final CardHolder ch = new CardHolder(sCurrentLine.split(","));
  map.put(ch.cardNumber, ch);
}

Also this will allow you finer control over other aspects of your record; 同样,这将使您可以更好地控制记录的其他方面; for example a nice custom toString and similar. 例如漂亮的自定义toString和类似的东西。 Note also that this has hardly resulted in more code: it just got reorganized by the separation-of-concerns principle. 还请注意,这几乎不会产生更多代码:只是通过关注分离原则对其进行了重组。

(A minor observation at the end: in Java the s-prefix to String variables is not customary because it is redundant in statically-typed languages; rest assured that you will never encounter a bug in Java due to an Integer occuring where a String was expected.) (最后的一个小发现:在Java中,String变量的s前缀不是惯用的,因为它在静态类型的语言中是多余的;请放心,由于在其中String所在的位置发生了整数,因此您永远不会遇到Java错误。预期。)

You should change, how you have created your Map.. In place of your below declaration: - 您应该更改创建地图的方式。代替下面的声明:-

Map<Map<String,String>,String> map = new HashMap<>();

Rather than this, you should have a : - 而不是此,您应该具有:-

Map<String, Map<String,String>> map = new HashMap<>();

You should always have an immutable type as a key in your Map.. 您的地图中应该始终有一个不可变的类型作为键。

And after this change , you should change: - 在更改之后 ,您应该更改:-

map.put(tempMap,parts[0]);

to: - 至: -

map.put(parts[0], tempMap);

I think you have the arguments in 我认为你有争论

map.put(tempMap,parts[0]);

reversed. 逆转。 You are mapping tempMap to the number, where you probably want to be mapping the number to tempMap: 您正在将tempMap映射到数字,您可能希望将数字映射到tempMap:

map.put(parts[0], tempMap);

Using 使用

Map<String, Map<String,String>> map = new HashMap<>();

You should use Map as value since HashMap equality is based on key value pairs it has. 您应该将Map用作值,因为HashMap相等性基于它具有的键值对。 .

map.put(parts[0],tempMap);

A simple program below will illustrate this fact 下面的一个简单程序将说明这一事实

    Map<String, String> tempMap1 = new HashMap<String, String>();
    tempMap1.put("issuing_bank", "ICICI");
    tempMap1.put("card_switch", "Visa");
    tempMap1.put("card_Type", "Debit");

    Map<String, String> tempMap2 = new HashMap<String, String>();

    tempMap2.put("issuing_bank", "ICICI");
    tempMap2.put("card_switch", "Visa");
    tempMap2.put("card_Type", "Debit");

    System.out.println(tempMap1.equals(tempMap2));//Prints true

Output: 输出:

true

So your declaration should like below. 因此,您的声明应如下所示。 Remember you should always use immutable object as key in HashMap. 请记住,在HashMap中,应始终将不可变对象用作键。

Map<String,Map<String,String>> map = new HashMap<String,Map<String,String>>();

That's because you're using a map (the tempMap) as a key. 那是因为您使用地图(tempMap)作为键。 This is wrong. 错了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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