2016-06-11 7 views
0

텍스트 파일의 각 줄을 배열에 저장하는 데 문제가 있습니다. 지금까지 내 프로그램은 파일을 읽을 수만 있습니다. 여기 내가 지금까지 가지고있는 것이있다.텍스트 파일의 각 줄을 배열에 저장

public class assignment1 { 

    public static void main(String[] args) { 
     System.out.println(System.getProperty("user.dir")); 
     File fileName = new File("competitors.txt"); 
     try { 
      Scanner scnr = new Scanner(fileName); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 
} 
+0

당신이 다른 질문을 확인 할 수 있습니다 http://stackoverflow.com/a/12857731/6383857 – StaticBeagle

답변

0

당신 수는 더 이상 선이없는 때까지 Scanner 돌이 :

List<String> list = new LinkedList<>(); 
try (Scanner scanner = new Scanner(fileName)) { 
    while (scanner.hasNextLine()) { 
     list.add(scanner.nextLine()); 
    } 
} catch (FileNotFoundException e) { 
    System.out.println(e); 
} 
String[] array = list.toArray(new String[list.size()]); 
+0

@ D.Nik 당신은 올바른 전체 경로를 제공해야합니다. – Mureinik

관련 문제