Android Studio 7일차
2021. 11. 9. 17:32ㆍAndroid Development
inputstream,outputstream 연습
package com.example.test19;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btWrite = findViewById(R.id.btWrite);
Button btRead = findViewById(R.id.btRead);
btWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileOutputStream fos = openFileOutput("file.txt", Context.MODE_PRIVATE);
String str = "hello,world";
fos.write(str.getBytes());
fos.close();
Toast.makeText(getApplicationContext(),"file.txt created", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}); // write event
btRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fis = openFileInput("file.txt");
byte[] txt = new byte[30];
fis.read(txt);
// fis 객체에서 txt바이트 배열로 문자값을 저장.
String str = new String(txt);
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"
android:calendarViewShown="false"
android:layout_gravity="center"
/>
<EditText
android:id="@+id/edtDiary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:background="#88ff88"
android:minHeight="48dp"
android:lines="8"
android:hint="write"/>
<Button
android:id="@+id/btWrite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:enabled="false"/>
</LinearLayout>
레이아웃
package com.example.test19;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
DatePicker dp;
EditText edtDiary;
Button btnWrite;
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("간단 일기장");
dp = findViewById(R.id.datePicker1);
edtDiary = findViewById(R.id.edtDiary);
btnWrite = findViewById(R.id.btWrite);
Calendar cal = Calendar.getInstance();
int cYear = cal.get(Calendar.YEAR);
int cMonth = cal.get(Calendar.MONTH);
int cDay = cal.get(Calendar.DAY_OF_MONTH);
fileName = Integer.toString(cYear) + "_"
+ Integer.toString(cMonth + 1) + "_"
+ Integer.toString(cDay) + ".txt";
// month 0~11
String str = readDiary(fileName);
edtDiary.setText(str);
btnWrite.setEnabled(true);
// 데이터피커 초기화, file_read
dp.init(cYear, cMonth, cDay, new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
fileName = Integer.toString(year) + "_"
+ Integer.toString(monthOfYear + 1) + "_"
+ Integer.toString(dayOfMonth) + ".txt";
// month 0~11
String str = readDiary(fileName);
edtDiary.setText(str);
btnWrite.setEnabled(true);
}
});
// file_write
btnWrite.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
FileOutputStream outFs = openFileOutput(fileName,
Context.MODE_PRIVATE);
String str = edtDiary.getText().toString();
outFs.write(str.getBytes());
outFs.close();
Toast.makeText(getApplicationContext(),
fileName + " 이 저장됨", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
}
}
});
}
String readDiary(String fName) {
String diaryStr = null;
FileInputStream inFs;
try {
inFs = openFileInput(fName);
byte[] txt = new byte[500];
inFs.read(txt);// 지정한 날짜의 파일을 읽어서, fis 에 저장.
inFs.close();
diaryStr = (new String(txt)).trim();
// trim : 문자열을 지정하면, 해당 문자열로 빈 공간을 채우고,
// 문자열을 지정하지 않는다면, 좌우의 공백 제거
btnWrite.setText("수정 하기");
} catch (IOException e) {
edtDiary.setHint("일기 없음");
btnWrite.setText("새로 저장");
}
return diaryStr;
}
}
기능부분.
'Android Development' 카테고리의 다른 글
Android Studio 10일차 (0) | 2021.11.12 |
---|---|
Android Studio 9일차 (0) | 2021.11.11 |
Android Studio 6일차 (0) | 2021.11.08 |
Android Studio 5일차 (0) | 2021.11.05 |
Android Studio 4일차 (0) | 2021.11.05 |