본문 바로가기
프로그래밍/Java

[JAVA 코드] txt 파일 읽기

by purplebulb 2018. 12. 5.
반응형


1. List 형식으로 읽기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

public void danbiExecute() {
                
        // 데이터 가져오기        
        List<String> row = new ArrayList<String>();
        File file = new File("D:/Keyword/description.txt");
 
          try {
               System.out.println(file.getAbsoluteFile() + " reading...");
               BufferedReader inFiles
                = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsolutePath()), "UTF8"));
               
               String line = "";
               while((line = inFiles.readLine()) != null) {
                if(line.trim().length() > 0) {
                 row.add(line);
                }
               }
               
               inFiles.close();
              } catch (Exception e) {
               e.printStackTrace();
              }
          
        // 데이터 출력하기  
        Iterator<String> iterator = row.iterator();
        while(iterator.hasNext()) {
            // 한 개의 객체를 가져옴, 형태소 분석할 문장
            String document = iterator.next();
            List<String> all = extractNoun(document);
            System.out.println(all);
            all.clear();
        }     
    }
}
 

   





2. String 형식으로 읽기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class maintest2 {
 
    public static void main(String[] args) {
 
        String a = "";
        File file = new File("C:/Users/kej82/Desktop/학습자료/JAVA/Example/news.txt");
        
          try {
               System.out.println(file.getAbsoluteFile() + " reading...");
               BufferedReader inFiles
                = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsolutePath()), "UTF8"));
               
               String line = "";
               while((line = inFiles.readLine()) != null) {
                if(line.trim().length() > 0) {
                 a += line + " " ;
                }     
               }
               
               inFiles.close();
              } catch (Exception e) {
               e.printStackTrace();
              }
 

   








댓글