2017-09-13 1 views
-3

특정 형식의 텍스트 파일을 정렬해야합니다. 내 파일 :트리 형식 파일 정렬

AAAA 
    BBBB 
     CCCC 
     CCCC 
     CCCC 
AAAA 
    BBBB 
     CCCC 
    BBBB 
     CCCC 

내가 BBBB에서 별도로 AAAA (first), BBBB (second)AAAACCCC (third)를 정렬해야하지만 난 그것을 확인하는 방법을 모르겠어요. 나는 AAAA 사이에, BBBB, CCCC이 4 칸임을 안다.

JSON으로 구문 분석 할 수는 있지만 작동한다고 생각하지만 파일을 정렬하는 간단한 방법이있을 것입니다. 아이디어 좀 줄 수있어?

전체 내용은 AAAA 단락으로되어 있지만 내용은 무엇입니까?

결과가 공백 (예 :보기) 일 필요는 없지만 시각적으로 좋습니다.

제발 당신의 의견을 말해줘.

예 : 내 프로그램의

Def 
    Abc 
     Xyz 
     Ghi 
     Def 
Abc 
    Def 
     Xyz 
    Abc 
     Abc 

출력 :

Abc 
    Abc 
     Abc 
    Def 
     Xyz 
Def 
    Abc 
     Def 
     Ghi 
     Xyz 

편집 : 이름 AAAA, BBBB는, CCCC는 동일하지 않습니다. 당신이 당신의 문제를 해결하기 위해이 프로그램을 사용할 수 있도록

+2

나는 당신이 무엇을 요구하고 있는지 전혀 모른다. 어떤 결과를 기대합니까? 그리고 입력 파일에 JSON과 같은 내용이 없는데 JSON에 대해 생각하는 이유는 무엇입니까? – ajb

+0

StackOverflow에 대한 질문이 너무 광범위합니다. 게시하기 전에 [도움말]을 방문하여 [ask]를 읽어보십시오. –

+0

최소한 원하는 출력/결과를 표시해야합니다. –

답변

0

좋아, 난 당신이있는 디렉토리의 목록을 얻을 것이다이 프로그램의 도움으로,

class Directory { 
    public String name; 
    public List<Directory > children; 
} 

그래서 여기에 디렉토리 자료 구조를 :: 사용했다 필수 정렬 형식. 이것은 정렬 된 형식으로 된 목록을 인쇄 할 때 출력됩니다.

[Directory [name = Abc, children = [Directory [name = Abc, children = null]]] 디렉토리 [name = Def, children = [Directory [name = Xyz Directory [이름 = Ghi, 자식 = null], 자식 = null]], 디렉토리 [이름 = Xyz, 어린이 = null]]]]]]

프로그램 자체를 설명하고 의심 할 여지가 있으면 코멘트를 참조하십시오.

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class test { 

    public static void main(String[] args) throws IOException { 
     BufferedReader br = new BufferedReader(new FileReader("Input.txt")); 
     List<Directory> dirList = new ArrayList<>(); 
     try { 
      String line = br.readLine(); 
      while (line != null) { 
       int spaces = line.length() - line.replaceAll(" ", "").length(); 
       while (spaces == 0) { 
        Directory directory = new Directory(); 
        directory.name = line.replaceAll(" ", ""); 
        directory.children = new ArrayList<>(); 
        line = br.readLine(); 
        spaces = line.length() - line.replaceAll(" ", "").length(); 
        while (spaces == 4) { 
         Directory directory2 = new Directory(); 
         directory2.name = line.replaceAll(" ", ""); 
         directory2.children = new ArrayList<>(); 
         line = br.readLine(); 
         if (line == null) 
          break; 
         spaces = line.length() - line.replaceAll(" ", "").length(); 
         while (spaces == 8) { 
          Directory directory3 = new Directory(); 
          directory3.name = line.replaceAll(" ", ""); 
          directory2.children.add(directory3); 
          line = br.readLine(); 
          if (line == null) 
           break; 
          spaces = line.length() - line.replaceAll(" ", "").length(); 
         } 
         Collections.sort(directory2.children); 
         directory.children.add(directory2); 
        } 
        Collections.sort(directory.children); 
        System.out.println(directory); 
        dirList.add(directory); 
       } 
       line = br.readLine(); 
      } 
      Collections.sort(dirList); 
      System.out.println(dirList); 
     } finally { 
      br.close(); 
     } 
    } 
} 

class Directory implements Comparable<Directory> { 
    public String name; 
    public List<Directory> children; 

    @Override 
    public String toString() { 
     return "Directory [name=" + name + ", children=" + children + "]"; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Directory other = (Directory) obj; 
     if (name == null) { 
      if (other.name != null) 
       return false; 
     } else if (!name.equals(other.name)) 
      return false; 
     return true; 
    } 

    public void sort() { 
     Collections.sort(children); 
    } 

    @Override 
    public int compareTo(Directory o) { 
     return name.compareTo(o.name); 
    } 

} 
+0

또한 간단한지도를 사용하여 0.0.0, 0.1.0, 0.1.1, 0.1.2의 형식으로 색인을 저장 한 다음 정렬 할 수 있으므로 그렇게하는 것이 더 쉽습니다. –