java 如何阅读管道分隔线 | 从文件和拆分两个不同的 ArrayList 中的整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33424865/
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
How to read Pipe delimited Line | from a File and Splitting Integers in two different ArrayList
提问by Shubham agarwal
The Program requires to interpret pipe delimited lines from a file and store it in two different ArrayLists. I have tried almost every related links on StackOverflow and came to know that | is a special operation and hence it is reserved. So I found several other ways to read the lines, but none of them actually works.
该程序需要从文件中解释管道分隔的行并将其存储在两个不同的 ArrayList 中。我已经尝试了几乎所有 StackOverflow 上的相关链接,然后才知道 | 是一个特殊的操作,因此它被保留。所以我找到了其他几种阅读这些行的方法,但它们都没有真正起作用。
Here is my scenario:
这是我的场景:
The Text File looks like:
文本文件如下所示:
0|0.20
1|0.10
2|0.20
0|0.20
1|0.10
2|0.20
and so on...
等等...
The first Integer needs to go to ArrayList a1 and the second float number after the pipe delimiter need to go to ArrayList a2
第一个Integer需要去ArrayList a1,管道分隔符之后的第二个浮点数需要去ArrayList a2
I tried using scanner and them splitting the lines using split. Having saved them to a String and then doing the String conversion.I used following ways:
我尝试使用扫描仪,他们使用 split 分割线。将它们保存到字符串然后进行字符串转换。我使用了以下方法:
Method 1:
方法一:
public static void readingFunc(String dirPath) throws NumberFormatException, IOException{
Path p = Paths.get(dirPath, "File.dat");
for (String line: Files.readAllLines(p,StandardCharsets.US_ASCII)){
for (String part : line.split("\|")) {
Integer i = Integer.valueOf(part);
intArrayList1.add(i);
}
}
Method 2:
方法二:
try(BufferedReader in = new BufferedReader(new FileReader(dirPath+"/File.dat"))){
String line;
while((line = in.readLine())!=null){
String[] pair = line.split("\|",-1);
//a1.add(Integer.parseInt(pair[0]));
//a2.add(Integer.parseInt(pair[1]))
Method 3:
方法三:
try(BufferedReader in = new BufferedReader(new FileReader(dirPath+"/File.dat"))){
String line;
while((line = in.readLine())!=null){
String[] pair = line.split("\|",-1);
I also used several other Methods such as Scanner and couldn't get the result. I have several files similar to the one above and I need to read them to save it in an ArrayList for their processing.
我还使用了其他几种方法,例如扫描仪,但无法获得结果。我有几个与上面类似的文件,我需要读取它们以将其保存在 ArrayList 中以供处理。
PS: one of the file is having three data like:
PS:其中一个文件包含三个数据,例如:
1|0.21|0.37
2|0.08|0.12
1|0.21|0.37
2|0.08|0.12
and so on. I guess. this would be easy and similar to the two delimiter process. PS: I am developing on Linux Eclipse IDE so paths are:
等等。我猜。这将很容易并且类似于两个分隔符过程。PS:我在 Linux Eclipse IDE 上开发所以路径是:
/home/user/workspace1/Java_Code
I am sending the path as dirPath from the main function and then calling it here in a function. Please suggest me how to go with it?
我从主函数中将路径作为 dirPath 发送,然后在函数中调用它。请建议我如何使用它?
I have checked already following Links:
我已经检查了以下链接:
Java Null value causing issue when reading a pipe delimited file
Read a file and split lines in Java.
Java - Scanner : reading line of record separated by Pipe | as delimiter
Java - 扫描仪:读取由管道分隔的记录行 | 作为分隔符
Java - putting comma separated integers from a text file into an array
采纳答案by janos
You can specify the regex [|\n]
as the delimiter for Scanner
, for example:
您可以将正则表达式指定[|\n]
为 的分隔符Scanner
,例如:
Scanner scanner = new Scanner("0|0.20\n1|0.10\n2|0.20\n");
scanner.useDelimiter(Pattern.compile("[|\n]"));
System.out.println(scanner.nextInt());
System.out.println(scanner.nextDouble());
System.out.println(scanner.nextInt());
System.out.println(scanner.nextDouble());
To read the values into lists:
要将值读入列表:
Scanner scanner = new Scanner("0|0.20\n1|0.10\n2|0.20\n");
scanner.useDelimiter(Pattern.compile("[|\n]"));
List<Integer> intList = new ArrayList<>();
List<Double> doubleList = new ArrayList<>();
while (scanner.hasNext()) {
intList.add(scanner.nextInt());
doubleList.add(scanner.nextDouble());
}
System.out.println(intList);
System.out.println(doubleList);
If the input file is DOS-formatted,
then the delimiter pattern needs to be a bit more complicated,
as the line ending is \r\n
.
This pattern will support both DOS and UNIX line endings:
如果输入文件是 DOS 格式的,那么分隔符模式需要更复杂一些,因为行结尾是\r\n
. 此模式将支持 DOS 和 UNIX 行结尾:
scanner.useDelimiter(Pattern.compile("[|\n]|(\r\n)"));