텍스트(.txt)파일 읽기
java)
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package com.ghj.blog_042; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { TextView txtRead; final static String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/TestLog/logfile.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtRead = (TextView)findViewById(R.id.txtRead); } public void mOnFileRead(View v){ String read = ReadTextFile(filePath); txtRead.setText(read); } //경로의 텍스트 파일읽기 public String ReadTextFile(String path){ StringBuffer strBuffer = new StringBuffer(); try{ InputStream is = new FileInputStream(path); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line=""; while((line=reader.readLine())!=null){ strBuffer.append(line+"\n"); } reader.close(); is.close(); }catch (IOException e){ e.printStackTrace(); return ""; } return strBuffer.toString(); } } | cs |
xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="파일읽기" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mOnFileRead"/> <TextView android:id="@+id/txtRead" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> | cs |
AndroidManifest.xml)
1 2 | <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | cs |
결과
'IT > - 프로그래밍' 카테고리의 다른 글
안드로이드 통신사, 전화번호 가져오기 (0) | 2016.12.03 |
---|---|
안드로이드 카메라앱 호출하여 사진찍고 이미지뷰에 출력하기 (0) | 2016.12.01 |
안드로이드 텍스트(.txt)파일 쓰기 (0) | 2016.11.25 |
안드로이드 리스트 다이얼로그 예제 (0) | 2016.11.24 |
안드로이드 구글맵3 마커, 정보창 클릭 이벤트 (4) | 2016.11.23 |