Splitting a line to objects in java
I am reading a file. I need to convert each value to java object.
String sCurrentLine;
BufferedReader br = null;
int pointer = 1;
try {
br = new BufferedReader(new FileReader("src/main/java/com/ali/trillium/p0f.log"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
System.out.println("---------------");
System.out.println(pointer++);
System.out.println("---------------");
}
Output of program
<Tue Apr 10 09:21:42 2018> 172.20.16.36:62385 - Windows XP/2000 (RFC1323+, w+, tstamp-) (ECN) [low cost] [GENERIC] Signature: [8192:113:1:52:M1352,N,W8,N,N,S:.:Windows:?] -> 172.20.16.23:3391 (distance 15, link: unknown-1392)
Like Lonely Neuron suggested :
You should use delimiting libraries in order to parse your output into a working data structure. You can use the split
function to achieve this:
s.split("delimiter"); // replace delimiter with the actual delimiting in the output
Please note that the parameter of split
is a regex expression, so be cautious with passing things like "."
. Assuming you have a multiple line output - you may want to use multiple calls to the split
method.
There are numerous ways to perform delimiting in java: you can use a Scanner, or a StringTokenizer (second answer) as well.
Good Luck!
链接地址: http://www.djcxy.com/p/78362.html上一篇: 读取/转换InputStream为字符串
下一篇: 将一条线分割成java中的对象