파일시스템에 파일 생성하여 데이터 저장하기

 

package com.andr;

 

import java.io.BufferedWriter;

import java.io.OutputStreamWriter;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

 

public class main extends Activity{

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

 

  TextView t=(TextView) findViewById(R.id.TextView01);

  BufferedWriter br=null;

  try {

 

   br=new BufferedWriter(new OutputStreamWriter(openFileOutput("data1.txt", MODE_WORLD_WRITEABLE)));

   br.append("안녕하세요");

   br.append("반갑습니다");

   t.setText("파일이 정상적으로 생성되었습니다.");

  } catch (Exception e) {

   Log.i("IO", "File Input Error");

   t.setText("오류");

  }finally {

   try {

   if(br!=null) br.close();

   } catch (Exception e2) {e2.printStackTrace();}

  }

 }

}

//main.xml에는 TextView 1개 TextView가 있어야 한다.

 

 

 

 

 

파일 시스템에 파일 불러오기 예:(위에 저장한 data1.txt 파일을 불러온다.)

 

package com.andr;

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

 

public class main extends Activity{

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

 

  TextView t=(TextView) findViewById(R.id.TextView01);

  BufferedReader br=null;

  try {

 

   br=new BufferedReader(new InputStreamReader(openFileInput("data1.txt")));

   String msg=br.readLine();

  t.setText(msg);

  } catch (Exception e) {

   Log.i("IO", "File Input Error");

  }finally {

   try { 

   if(br!=null) br.close();

   } catch (Exception e2) {e2.printStackTrace();}

  }

 }

}

//main.xml에는 TextView 1개 TextView가 있어야 한다.

번호 제목 글쓴이 날짜 조회 수
128 PC를 위한 최고의 안드로이드 에뮬레이터 황제낙엽 2023.01.18 3136
127 [Jetpack Compose UI / 상추님] GDG Jetpack compose 코드랩 수료 후기 (feat. 굿즈) 황제낙엽 2022.12.24 2372
126 [Jetpack Compose UI / kyj1991719] compose - Compose 이해 황제낙엽 2022.12.24 4025
125 [Jetpack Compose UI / 꾸준] Compose 가 뭔데...? 황제낙엽 2022.12.24 4036
124 [Jetpack Compose UI / IIVIKI] Compose 학습과 분석 황제낙엽 2022.12.24 4050
123 [Jetpack Compose UI] 스터디 시작하기 황제낙엽 2022.12.24 4001
122 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 바텀네비게이션 뷰, 네비게이션 AAC file 황제낙엽 2022.12.22 3882
121 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 뷰바인딩 file 황제낙엽 2022.12.22 3852
120 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 라이브데이터 뷰모델 file 황제낙엽 2022.12.21 3827
119 [대왕놀] ImageButton in Compose (IconButton) 황제낙엽 2022.12.20 4107
118 [대왕놀] compose instrumentation test tutorial 황제낙엽 2022.12.20 3986
117 [대왕놀] Switch in Compose 황제낙엽 2022.12.20 4106
116 [대왕놀] EditText in Compose (TextField) 황제낙엽 2022.12.20 4022
115 [대왕놀] margin in Compose 황제낙엽 2022.12.20 4105
114 [대왕놀] modifier in compose 황제낙엽 2022.12.20 4181
113 [대왕놀] Button in Compose file 황제낙엽 2022.12.20 4225
112 [대왕놀] TextView in Compose (Text) 황제낙엽 2022.12.20 4083
111 [대왕놀] ImageView in Compose (Image) 황제낙엽 2022.12.20 4019
110 [대왕놀] LinearLayout in Compose (Row, Column) 황제낙엽 2022.12.20 4114
109 [대왕놀] Introduction to Jetpack compose 황제낙엽 2022.12.20 3965