프로그래밍 언어/<Java>

[팁] File 읽어서 파싱하기

소혼 2010. 6. 10. 01:34
반응형
1. File을 new line 단위로 읽기

        InputStream fis = myResources.openRawResource(resource 명)
        InputStreamReader isr = new InputStreamReader(fis);

        BufferedReader br = new BufferedReader(isr);

        try {
            String line = br.readLine();
            Log.w("ENGLISH1", line);
        } catch (IOException e) {
            e.printStackTrace();
        }

2. 특정 delimeter 단위로 읽기
        InputStream fis = myResources.openRawResource(R.raw.myfile); // 안드로이드 코드
        Scanner scn = new Scanner(fis).useDelimiter("\\r");

        String parsedStr, parsedStr2;
        while (scn.hasNext()) {
            parsedStr = scn.next();
        }

몇개의 delimeter를 섞어 쓰려면 
ex) "\\r|," <- ^M 또는 , 단위로 구분
반응형

'프로그래밍 언어 > <Java>' 카테고리의 다른 글

Sort a HashMap by Value  (0) 2011.08.07
JNI on linux  (2) 2010.12.27
[자바] 날짜, 요일 출력.  (0) 2010.11.06
[자바]일련의 숫자 임의 정렬하기  (0) 2010.07.31
[String]String.format  (0) 2010.06.16