2017-11-29 1 views
-1

파일을 열어 파일의 길이에 관계없이 파일을 한 줄씩 읽는 방법을 만들려고합니다. 각 줄은 단어와 문장 부호 (아포스트로피 제외)로 분리되어 문자열의 ArrayList에 저장됩니다. 행을 나타내는 이러한 ArrayLists는 String의 ArrayList ArrayList에 저장됩니다.arraylist의 arraylist와 함께 한 줄씩 파일 읽기 - Java

예를 들어 파일이 들어있는 경우. "안녕하세요", "세계"

Hello World! 
Hi, hello. 

그런 다음 첫 번째 배열 목록은 문자열을 포함하는 것 "!" 두 번째 배열 목록에는 "Hi", ",", "hello", "."문자열이 포함됩니다.

지금까지 다음과 같은 내용을 갖고 있으며 올바른 방향으로 가고 있는지 확실하지 않습니다. 코드 아래

public static void readInputFile(String fileName, ArrayList<ArrayList<String>> fileByLine) 
    throws IOException { 

    File userFile = new File(fileName); 

    try { 
     if (userFile.exists()) { 
      Scanner fileScanner = new Scanner(userFile); 
      do { 
       do { 
        fileByLine.add(fileScanner.next()); 
       } while (fileScanner.next() != null); 
      } while (fileScanner.hasNext()); 
     } 
    } catch (Exception e) { 
     System.out.println("Exception: File '" + fileName + "' not found."); 
    } 
} 
+1

사이트가 어떻게 작동하는지 그리고 여기에 어떤 질문이 주제인지 확인하고 이에 따라 질문을 편집하십시오 [http://tackoverflow.com/tour]. 또한보십시오 : [작은 프로그램을 디버깅하는 방법] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –

답변

0

은 .. 당신은 시도 할 수 있습니다, 나를 위해 작동

public class ListOFList { 

    public static ArrayList<String> stringspecial(String strLine) { 
     String str = strLine; 
     String arr[]=str.split(" "); 

     ArrayList<String> list = new ArrayList<String>(); 

      for(int i=0;i<arr.length;i++) { 
       Pattern pattern = Pattern.compile("[a-zA-Z0-9]*"); 
       Matcher matcher = pattern.matcher(arr[i]); 


      if (!matcher.matches()) { 
       char[] ch = arr[i].toCharArray(); 
       String substring=""; 

       for(int j=0;j<ch.length;j++) { 


        if(!(Character.isLetter(ch[j]))) { 
         String special = Character.toString(ch[j]); 
         list.add(special); 
        }else { 
         substring=substring+ch[j]; 
         if(!(Character.isLetter(ch[j+1]))) { 
          list.add(substring); 
        } 

        } 
       } 


      } else { 
       list.add(arr[i]); 

      } 

    } 
      return list; 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 

     FileInputStream fi = new FileInputStream("<Path of the Input Text File>"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fi)); 
     String strLine; 
     ArrayList<ArrayList<String>> listoflists = new ArrayList<ArrayList<String>>(); 
     ArrayList<String> list = new ArrayList<String>(); 
     while ((strLine = br.readLine()) != null) { 
      list=stringspecial(strLine); 
      listoflists.add(new ArrayList(list)); 
      list.clear();  
       } 
     for(int x = 0; x < listoflists.size(); x++) 
     { 

       System.out.print(listoflists.get(x)); 
       System.out.print(" "); 

     } 

     br.close(); 

       } 

     } 

INPUT :

안녕하세요! 안녕하세요. 안녕하세요. Hello World! 안녕하세요. 안녕하세요. Hello World! 안녕하세요. 안녕하세요.

OUTPUT : [! 안녕하세요, 세계,] [! 안녕하세요, 세계,]

[. 안녕하세요 ,, 안녕하세요,] [! 안녕하세요, 세계,] [. 안녕하세요 ,, 안녕하세요,] [ 안녕하세요, ,, 안녕하세요.]

관련 문제