public class Io02 extends Activity
implements View.OnClickListener {
private Button b1, b2, b3, b4;
private EditText et1, et2, et3;
//private static final String EX_PATH = "/sdcard";
private static final String EX_PATH
= Environment.getExternalStorageDirectory().getAbsolutePath();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText)findViewById(R.id.EditText01);
b1 = (Button)findViewById(R.id.Button01);
et2 = (EditText)findViewById(R.id.EditText02);
b2 = (Button)findViewById(R.id.Button02);
b3 = (Button)findViewById(R.id.Button03);
et3 = (EditText)findViewById(R.id.EditText03);
b4 = (Button)findViewById(R.id.Button04);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
//showPath();//특정 하드웨어에 의존하지 않게 android의 기준 Directory 찾기
}
@Override
public void onClick(View v) {
if(v == b1){
toInFromEx(); //1. 외장 -> 내장
}else if(v == b2){
//inFileListShow();
String fname = et2.getText().toString();
toExFromIn(fname); //2. 내장(현재앱) -> 외장
}else if(v == b3){
toExFromInAll();
}else{ //v == b4
String fname = et3.getText().toString();
toExFromInAny(fname); //3. 내장(현재앱X) -> 외장
}
}
//3. 내장(현재앱X) -> 외장 ( 추후 고민해 볼 것! )
private void toExFromInAny(String fname){ //테스팅 결과 확인X
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(fname);
String destDir = EX_PATH + "/io/apks";
File dir = new File(destDir);
if(!dir.exists()) dir.mkdirs();
fos = new FileOutputStream(destDir+"/"+fname);
fileMove(fis, fos);
}catch(FileNotFoundException fnfe){
}catch(IOException ie){
try{
if(fos != null) fos.close();
if(fis != null) fis.close();
}catch(IOException ie2){}
}
}
//2. 내장(현재앱) -> 외장
private void inFileListShow(){
StringBuffer sb = new StringBuffer();
File inFilesDir = getFilesDir(); //중요!
File childs[] = inFilesDir.listFiles();
for(File child : childs){
sb.append(child.getName() + "\n");
}
showMsg(sb.toString(), 1);
}
private void toExFromIn(String fname){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = openFileInput(fname);
String destDir = EX_PATH + "/aa/bb/cc";
File dir = new File(destDir);
if(!dir.exists()) dir.mkdirs();
fos = new FileOutputStream(destDir+"/"+fname);
fileMove(fis, fos);
}catch(FileNotFoundException fnfe){
}catch(IOException ie){
try{
if(fos != null) fos.close();
if(fis != null) fis.close();
}catch(IOException ie2){}
}
}
private void toExFromInAll(){
File inFilesDir = getFilesDir(); //중요!
File childs[] = inFilesDir.listFiles();
for(File child : childs){
toExFromIn(child.getName());
}
showMsg("모든 파일 복사 완료", 1);
}
//1. 외장 -> 내장
private void toInFromEx(){
//et1의 입력정보(dir/file)를 내장메모리로 이동시키시요.
String path = et1.getText().toString();
File f = new File(EX_PATH + path);
if(f.isFile()){
fileCopy(f, getFname(path));
}else{
File fs[] = f.listFiles();
for(File child : fs){
if(child.isFile()){
fileCopy(child, child.getName());
}
}
}
}
private String getFname(String path){
int idx = path.lastIndexOf("/");
String fname = "";
if(idx != -1){
fname = path.substring(idx+1);
}
return fname;
}
private void fileCopy(File f, String fname){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(f);
fos = openFileOutput(fname, MODE_PRIVATE);
fileMove(fis, fos);
}catch(FileNotFoundException fnfe){
showMsg("입력 경로가 잘못됨", 0);
et1.setText("");
et1.requestFocus();
}catch(IOException ie){
showMsg("IO예외", 0);
}
}
private void fileMove(FileInputStream fis,
FileOutputStream fos) throws IOException {
int i = 0;
byte buffer[] = new byte[512];
while((i = fis.read(buffer)) != -1){
fos.write(buffer, 0, i);
fos.flush();
}
showMsg("파일 복사 성공", 0);
}
/*
File f1; //sdcard
File f2; //system
File f3; //data
File f4; //cache
void showPath(){
f1 = Environment.getExternalStorageDirectory();// sdcard
String state = Environment.getExternalStorageState(); //mounted, shared
String path1 = f1.getAbsolutePath();
String path2 = "";
try{
path2 = f1.getCanonicalPath(); //OS 시스템에 맞는 구분자 적용한 경로
}catch(IOException ie){}
String path3 = f1.getPath();
showMsg("path1 : " + path1
+ ", path2 : " + path2
+ ", path3 : " + path3
+ ", state : " + state, 1);
f2 = Environment.getRootDirectory(); //system
f3 = Environment.getDataDirectory(); //data
f4 = Environment.getDownloadCacheDirectory(); //cache
String f2Path = f2.getAbsolutePath();
String f3Path = f3.getAbsolutePath();
String f4Path = f4.getAbsolutePath();
showMsg("f2Path : " + f2Path
+ ", f3Path : " + f3Path
+ ", f4Path : " + f4Path, 1);
}
*/
void showMsg(String msg, int option){
Toast.makeText(this, msg, option).show();
}
}