JAVA如何从文本文档每行中读取需要的内容到数组中?
Tracingroutetowww.a.shifen.com[119.75.213.36]overamaximumof30hops:1<1ms1ms<1ms118.229...
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 展开
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 展开
3个回答
展开全部
调试了一个 把上面的东西方到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();
}
}
}
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;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TextReader {
private BufferedReader breader = null;
private FileReader freader = null;
public TextReader(File file) {
init(file);
}
public BufferedReader getBufreredReader() {
return breader;
}
private void init(File file) {
try {
freader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (freader != null) {
breader = new BufferedReader(freader);
}
}
public String readAll() {
StringBuffer result = new StringBuffer("");
String temp;
while ((temp = readLine()) != null) {
result.append(temp + "\n");
}
return result.toString();
}
public String readLine() {
String line = null;
try {
line = breader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
public void close() {
if (breader != null) {
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else if (freader != null) {
try {
freader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//这里匹配,存到ArrayList里了,可以转化为String数组的
import java.io.File;
import java.util.ArrayList;
import java.util.regex.*;
public class Pri{
public static void main(String args[]){
String ipRegex = "(((\\d{1})|(\\d{2})" +
"|(1\\d{2})|(2[0-4]\\d{1})|" +
"(25[0-5]))\\.){3}((\\d{1})|" +
"(\\d{2})|(1\\d{2})|(2[0-4]\\d{1})" +
"|(25[0-5]))";
Pattern p = Pattern.compile(ipRegex);
TextReader tr = new TextReader(new File("F:\\jtest\\ip.txt"));
String con = tr.readAll();
Matcher m = p.matcher(con);
ArrayList<String> list = new ArrayList<String>();
while(m.find()) {
list.add(m.group());
}
Object [] ips = list.toArray();
for(int i = 0; i < ips.length; i ++) {
System.out.println(ips[i]);
}
}
}
TextReader:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TextReader {
private BufferedReader breader = null;
private FileReader freader = null;
public TextReader(File file) {
init(file);
}
public BufferedReader getBufreredReader() {
return breader;
}
private void init(File file) {
try {
freader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (freader != null) {
breader = new BufferedReader(freader);
}
}
public String readAll() {
StringBuffer result = new StringBuffer("");
String temp;
while ((temp = readLine()) != null) {
result.append(temp + "\n");
}
return result.toString();
}
public String readLine() {
String line = null;
try {
line = breader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
public void close() {
if (breader != null) {
try {
breader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else if (freader != null) {
try {
freader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//这里匹配,存到ArrayList里了,可以转化为String数组的
import java.io.File;
import java.util.ArrayList;
import java.util.regex.*;
public class Pri{
public static void main(String args[]){
String ipRegex = "(((\\d{1})|(\\d{2})" +
"|(1\\d{2})|(2[0-4]\\d{1})|" +
"(25[0-5]))\\.){3}((\\d{1})|" +
"(\\d{2})|(1\\d{2})|(2[0-4]\\d{1})" +
"|(25[0-5]))";
Pattern p = Pattern.compile(ipRegex);
TextReader tr = new TextReader(new File("F:\\jtest\\ip.txt"));
String con = tr.readAll();
Matcher m = p.matcher(con);
ArrayList<String> list = new ArrayList<String>();
while(m.find()) {
list.add(m.group());
}
Object [] ips = list.toArray();
for(int i = 0; i < ips.length; i ++) {
System.out.println(ips[i]);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用正则表达式
具体怎么用我忘了
你可以去查下
具体怎么用我忘了
你可以去查下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询