2014-02-11 24 views
0

考慮下列文件「sample.txt的」作爲輸入包含以下信息的java程序:提取與文件管分離信息,並將其保存到另一個文件

#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d 
$1|1391436893 
?[176.08179, -13.839829, -1.0054213] 
%PKKV7|00:7f:28:3f:17:9d|-67|2437 
%DC2VJ|f8:e4:fb:a0:06:f8|-71|2412 
%VVWSP|00:7f:28:d5:92:65|-71|2462 
%SVT8H|f8:e4:fb:8e:d6:9b|-77|2437 
%ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452 
%2X4C8|00:7f:28:44:23:da|-75|2437 
%STDGD|f8:e4:fb:70:86:f4|-82|2462 
%DeathStar|00:7f:28:be:c8:94|-84|2412 
%Freeinternet|00:1e:58:f4:15:f7|-59|2437 
%QB657|00:26:62:b7:16:4b|-88|2462 
%375F2|00:26:b8:3e:0a:14|-70|2412 
%E1K38|00:26:62:cf:90:37|-81|2412 

我試圖讓一個「output.txt」文件如下:

00:7f:28:3f:17:9d|-67 
f8:e4:fb:a0:06:f8|-71 
00:7f:28:d5:92:65|-71 
f8:e4:fb:8e:d6:9b|-77 
20:10:7a:14:6a:f7|-66 
00:7f:28:44:23:da|-75 
f8:e4:fb:70:86:f4|-82 
00:7f:28:be:c8:94|-84 
00:1e:58:f4:15:f7|-59 
00:26:62:b7:16:4b|-88 
00:26:b8:3e:0a:14|-70 
00:26:62:cf:90:37|-81 

如何在java中實現這個任何建議?

回答

0

下面的正則表達式將提取此:

\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}\W{1}-\d{2} 

http://ideone.com/FwpV7z

import java.util.*; 
import java.lang.*; 
import java.io.*; 
import java.util.regex.*; 


class Ideone 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     String testData = "#1|77|1391436891|1|1|00:1e:58:f4:15:f7|Nexus 4, 4.4, MAKOZ30d $1|1391436893 ?[176.08179, -13.839829, -1.0054213] %PKKV7|00:7f:28:3f:17:9d|-67|2437 %DC2VJ|f8:e4:fb:a0:06:f8|-71|2412 %VVWSP|00:7f:28:d5:92:65|-71|2462 %SVT8H|f8:e4:fb:8e:d6:9b|-77|2437 %ThreeBestFriends|20:10:7a:14:6a:f7|-66|2452 %2X4C8|00:7f:28:44:23:da|-75|2437 %STDGD|f8:e4:fb:70:86:f4|-82|2462 %DeathStar|00:7f:28:be:c8:94|-84|2412 %Freeinternet|00:1e:58:f4:15:f7|-59|2437 %QB657|00:26:62:b7:16:4b|-88|2462 %375F2|00:26:b8:3e:0a:14|-70|2412 %E1K38|00:26:62:cf:90:37|-81|2412"; 
     String regularExpression = "\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}\\W{1}-\\d{2}"; 

     Pattern pattern = Pattern.compile(regularExpression); 
     Matcher matcher = pattern.matcher(testData); 
     while(matcher.find()) { 
      System.out.println(matcher.group(0)); 
     } 

    } 
} 

輸出:

00:7f:28:3f:17:9d|-67 
f8:e4:fb:a0:06:f8|-71 
00:7f:28:d5:92:65|-71 
f8:e4:fb:8e:d6:9b|-77 
20:10:7a:14:6a:f7|-66 
00:7f:28:44:23:da|-75 
f8:e4:fb:70:86:f4|-82 
+0

謝謝你的工作。有沒有辦法輸入和輸出文件? – user3294395

+0

http://www.cstutoringcenter.com/tutorials/java/java11.php –

0

如果您將該行讀取爲字符串,則可以使用split將其拆分爲管道。

+0

我必須使用哪個函數來分離管道? – user3294395

+1

yourstring.split(「|」) – Popo

0

這裏有一個很好的開始(可能是拼寫錯誤的,因爲我沒直寫在這裏)

//Read all lines, one line at a time with a simple scanner: 
Scanner sc = new Scanner(new File("sample.txt")); 
while (sc.hasNextLine()){ 
    String line = sc.nextLine(); 
    //If the line starts with a % do something, else ignore 
    if (line.charAt(0) == '%'){ 
     int firstPipe = line.indexOf("|"); 
     int secondPipe = line.indexOf("|",firstPipe); 

     //Now use String.substring() and perhaps a temporary StringBuffer storage followed by, for example, a FileWriter to create the output file 
相關問題