我写了一段代码从文件中读取值,
public class Test {
public static void main(String[] args) throws Exception {
// pass the path to the file as a parameter
FileReader fr = new FileReader("/home/workspace_ag7_tmv/Message Router/environments/wb/conf/subscriber_content_restriction.conf");
int i;
while ((i=fr.read()) != -1) {
System.out.print((char) i);
}
}
}
在我的文件中,我传递了这些值: PRE|00000000110000200000049U POS|10000000110000200000049U
我可以使用上面的代码获得这些值,现在我想获取第四个索引值。你能帮我做同样的事吗?
发布于 2019-10-04 13:49:58
您可以将字符添加到ArrayList中,然后获取第四个索引处的元素。
int i;
List<Character> charList = new ArrayList<>();
while ((i=fr.read()) != -1)
charList.add((char) i);
System.out.print((char) i);
}
// to get char at index 4
char a = charList.get(4);
读完你的其他问题后:如果你想得到'|‘后面的值的索引,那么你可以把列表转换成一个字符串,得到indexOf '|’,然后在索引上加一个1。
类似于:
int i;
List<Character> charList = new ArrayList<>();
while ((i=fr.read()) != -1)
charList.add((char) i);
System.out.print((char) i);
}
String str= charList.stream()
.map(String::valueOf)
.collect(Collectors.joining());
int firstPos = str.indexOf('|');
System.out.println(str.charAt(firstPos+1));
int secondPos = str.indexOf('|', firstPos+1);
System.out.println(str.charAt(secondPos+1));
https://stackoverflow.com/questions/58230551
复制相似问题