Android txt 파일 읽어 오기

public void onCreate(Bundle savedInstanceState){
?? super.onCreate(savedInstanceState);
???setContentView(R.layout.text_test);

?? EditText txt_test = (EditText)findViewById(R.id.et_1);

?? String?test_file = getStringFromAsset2(safe_join_provision.this, “test.txt”);
}

public static String getStringFromAsset2(Activity activity, String file){
?? String result =”";
?? AssetManager assetManager = activity.getAssets();
? ?InputStream is = null;
? ?try {
????? is = assetManager.open(file);
????? result = convertStreamToString2(is);
? ?} catch (Exception e) {
????? // TODO: handle exception
? ?} finally{
????? try {
???????? is.close();
????? } catch (Exception e2) {
???????? // TODO: handle exception
???? ?}
?? }
?? return result;
}

 

?public static String convertStreamToString2(InputStream is) {
?? ByteArrayOutputStream baos = new ByteArrayOutputStream();
?? BufferedReader br = null;
?? String temp = “”;
?? try {
???br = new BufferedReader(new InputStreamReader(is));
?? } catch (Exception e1) {
???? ?// TODO Auto-generated catch block
????? e1.printStackTrace();
?? }
?? try {
????? String tmp = “”;
???
????? while((tmp = br.readLine()) != null){
??????? ?temp += tmp+”\n”;
???? ?}
?? } catch (Exception e) {
???? ?// TODO: handle exception
? ?} finally{
???? ?try {
??????? ?if(br != null)
??????? ?br.close();
????? } catch (IOException e) {
????????? // TODO Auto-generated catch block
???????? e.printStackTrace();
????? }
?? }

?? return temp;
}

댓글 남기기

당신의 이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.

*

다음의 HTML 태그와 속성을 사용할 수 있습니다: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>