JAVA如何从文本文档每行中读取需要的内容到数组中?

来源:百度知道 编辑:UC知道 时间:2024/06/07 23:30:53
Tracing route to www.a.shifen.com [119.75.213.36]
over a maximum of 30 hops:
1 <1 ms 1 ms <1 ms 118.229.152.1
2 3 ms 10 ms 8 ms 10.2.1.1
3 <1 ms <1 ms <1 ms 10.0.12.1
4 9 ms 9 ms 8 ms 10.0.24.13
5 10 ms 10 ms 9 ms 202.112.42.5
6 10 ms 10 ms 10 ms 202.112.6.54
7 11 ms 9 ms 10 ms 119.75.212.14
8 10 ms 9 ms 9 ms 119.75.213.36
Trace complete.
需要实现将每行最后的IP地址存到一个string类型的数组中,要如何实现呀?悬赏50分呀
其中每行的有用信息之间有很多空格1 <1 ms 1 ms <1 ms 118.229.152.1

调试了一个 把上面的东西方到f盘下ip.txt中 运行我的代码就可以了
import java.io.*;
public class ReadFileIP {
public static void main(String[] args) {
String[] stringArgs = new String[50];
File file = new File("F:\\ip.txt");
FileReader fr;
String str = null;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int i = 0;
while ((str = br.readLine()) != null) {
str = str.trim();
if (-1 != (str.lastIndexOf("ms"))) {
stringArgs[i] = str.substring(str.lastIndexOf("ms") + 2);
i++;
System.out.println(stringArgs[i - 1]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

用正则表达式
具体怎么用我忘了
你可以去查下

//用到的类
TextReader:

import java.io.BufferedReader;
import java.io.File