使用javacsv来操作csv文件,此文件为UTF-8格式。
发现有一列通过get(String columnName)方式获取不到,其他列都可以,而且名称反复核对都OK。
单步跟踪发现问题,跟到java8的 HashMap里
到了
代码语言:javascript复制/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* A return value of {@code null} does not necessarily
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
此时key为带获取的csv头的列名supplierId,然后跟进去
代码语言:javascript复制/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
调试页面显示:
此时传入的hash值和key对应的hash值并不对应。
反复调试发现问题
supplierId前面明明没有字符怎么多了个uFEFF?
查阅资料发现 “Windows平台下Unicode文件(UTF-8等)头部插入BOM首字符”,supplierId确实是头部第一列的列名,因此可以确定这个是BOM首字符。
因此将文件保存为UTF-8( without BOM)重新读取就可以了,使用sublime text再File-Save with Encoding中选择UTF-8即可却去掉BOM头。
或者读取第一个字符时应该判断是否以上述unicode字符开头,跳过即可。
可以继承CsvReader重写 其中的readRecord函数 在while循环里,当columsCount =0时,跳过此BOM头。