2014-03-25 3 views
0

나는 다음과 같은 텍스트 파일 (offline.txt)이 있습니다정렬 행은 행에 포함

# Timestamp, X, Y, MAC Address of AP, RSS 
1395444273179 35.19967269897461 19.1965389251709 28:c6:8e:85:80:d3 -71 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a1 -75 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a2 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b1 -84 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b2 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b0 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a0 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:41 -75 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:40 -73 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:42 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:52 -96 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:50 -97 

내가 수에 따라 파일의 라인을 정렬하고 싶습니다를 값이 반복 될 경우 내림차순으로 파일의 다섯 번째 열은 반복 된 값의 순서와 상관이 없습니다.

예를 들어이있다 원하는 출력 (offline_out.txt) 나는 이전의 특정 텍스트 파일에 대해 원하는 :

# Timestamp, X, Y, MAC Address of AP, RSS 
1395444273179 35.19967269897461 19.1965389251709 28:c6:8e:85:80:d3 -71 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:40 -73 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:42 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a0 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a2 -74 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a1 -75 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b1 -84 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b2 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b0 -85 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:52 -96 
1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:50 -97 

내가 파일을 읽는 방법을 알고, 내가 아는 그 "종류"기능에 자바 정렬 나를 도울 수 있습니다. 제 생각에 제 5 행에있는 모든 숫자를 추출하여 벡터에 저장 한 다음 벡터를 정렬하고 번호가 정렬되면 특정 행에 숫자를 할당하는 방법을 찾고 숫자를 정렬하고 행을 정렬 한 다음 그들을 다른 파일에 저장하십시오. 이것을 프로그램하는 방법에 대한 아이디어가 있습니까?

내가 지금까지 가지고있는 프로그램입니다 :

public class extract { 
    public static void main (String[] args) throws java.lang.Exception 
    { 
    File inputFile = new File("offline.txt"); 
    File tempFile = new File("offline_out.txt"); 

    BufferedReader reader = new BufferedReader(new FileReader(inputFile)); 
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); 

    //while to read all the lines, but how can I store only the numbers to a vector an associate them to a specific row? 
     while((currentLine = reader.readLine()) != null) { 
      } 
    } 

    //to save the output file 
    boolean successful = tempFile.renameTo(inputFile); 

    } 
+0

나는 좋은 접근 방법이 있다고 생각합니다. 귀하의 접근 방식에서 각 단계를 개별적으로 수행하고 원하는 결과에 도달해야합니다. 귀하의 접근 방식을 각자의 방법으로 세분화하는 것이 좋습니다. – Bernard

+0

각 행을 객체로 읽어 들인 다음 마지막 열에서 'Comparator'를 사용하여 객체 목록을 정렬 할 수 있습니까? –

+0

각 줄을 객체로 처리하면 "comparator"라는 줄에서 5 번째 줄의 숫자에 따라 줄을 정렬해야합니다. – user3349667

답변

0

사용에 대한 트리 맵을 사용하려고하는 방법 las에 해당하는 행을 저장하는 TreeMap 특정 라인을 마지막으로

TreeMap<Integer, String> map = new TreeMap<Integer, String>(); 
while((currentLine = reader.readLine()) != null) { 

    // split the line and use the last value as key 
    if (!currentLine.contains("Timestamp")) 
     map.put(Integer.parseInt(currentLine.split("\\s+")[4]), currentLine); 
    else 
     map.put(0, currentLine); 
} 

당신이 (하시기 바랍니다 또는 당신이 같은 파일이 쓰기 가능) 인쇄하고 결과를 볼 수 있습니다에서 t 번호 :

for(Integer key : map.descendingKeySet()) 
    System.out.println(map.get(key)); 

반복 라인은 촬영되지 않습니다 상술에 구조를 갱신하는 특정 번호

0123에 대응하는 라인을 저장하도록 상기 Arraylist와 맵을 사용하여 그것을

을 캡처

TreeMap<Integer, ArrayList<String>> map = new TreeMap<Integer, ArrayList<String>>(); 
while((currentLine = reader.readLine()) != null) { 

    int key; 
    if (!currentLine.contains("Timestamp")) 
     // split the line and use the last value as key 
     key = Integer.parseInt(currentLine.split("\\s+")[4]; 
    else 
     key = 0; 
    ArrayList<String> lines; 
    if (!map.contains(key)) //if the key doesn't exist create a new arraylist 
     lines = new ArrayList<String>();    
    else // if the key exists use the arraylist in the map 
     lines = map.get(key); 
    lines.add(currentLine); 
    map.put(key, lines); 
} 

및 인쇄:

for(Integer key : map.descendingKeySet()) 
    for(String line : map.get(key)) 
     System.out.println(line); 
+0

부분적으로 작동 해 주셔서 감사합니다. 문제는 반복되는 줄을 무시한다는 것입니다. – user3349667

+0

지도에서 키는 고유 할 것이므로 반복되는 줄은 무시됩니다.반복 라인의 경우에도 정렬 우선 순위가 있습니까 ?? – AKS

+0

업데이트 된 답변을 살펴보십시오. – AKS

1

은 두 개의 필드, numberline을 갖는 값 객체 빈 클래스를 만듭니다. comparable을 구현하고 해당 클래스의 compareTo 메서드를 재정의하십시오. 파일을 스캔 할 때이 Bean 클래스의 ArrayList을 채 웁니다. 그런 다음 ArrayList을 정렬하십시오.

+0

행을 인쇄하기위한 업데이트 된 대답을 확인하거나 @ shree.pat18 주석으로 다른 값으로 정렬하려는 경우 'Comparator'를 사용하십시오. 그러나, 나는 'Comparable'이 괜찮을 것이라고 생각한다. – MadcoreTom