• android 文本文件(含SD卡)读写



    -----------------------------------------------

    package com.eboy.filereadwrite;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.eboy.filereadwrite.R.id;
    import com.eboy.filereadwrite.R.string;
    import com.eboy.service.FileService;

    public class MainActivity extends Activity {
        private EditText edtFileName = null;
        private EditText edtFileContent = null;
        private Button btnRead = null;
        private Button btnWrite = null;
        private Button btnSDRead = null;
        private Button btnSDWrite = null;
        private String FileName;
        private String FileContent;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            edtFileName = (EditText) findViewById(R.id.FileName);
            edtFileContent = (EditText) findViewById(R.id.FileContent);
            btnRead = (Button) findViewById(id.btnRead);
            btnWrite = (Button) findViewById(id.btnWrite);
            btnRead.setOnClickListener(new ReadOnClickListener());
            btnWrite.setOnClickListener(new WriteOnClickListener());
            
            btnSDRead = (Button) findViewById(id.btnSDRead);
            btnSDWrite = (Button) findViewById(id.btnSDWrite);
            btnSDRead.setOnClickListener(new SDReadOnClickListener());
            btnSDWrite.setOnClickListener(new SDWriteOnClickListener());
        }
        
        private final class WriteOnClickListener implements View.OnClickListener{

            public void onClick(View v) {
                FileService service = new FileService(getApplicationContext());
                FileName = edtFileName.getText().toString();
                FileContent = edtFileContent.getText().toString();
                try {
                    service.write(FileName, FileContent);
                    Toast.makeText(getApplicationContext(), string.writeSuccess, Toast.LENGTH_LONG).show();
                }
                catch (Exception e) {
                    Toast.makeText(getApplicationContext(), string.writeFail + "," + e.toString(), Toast.LENGTH_LONG).show();
                }
            }        
        } 
        
        private final class ReadOnClickListener implements View.OnClickListener{

            public void onClick(View v) {
                FileService service = new FileService(getApplicationContext());
                FileName = edtFileName.getText().toString();
                FileContent = edtFileContent.getText().toString();
                try {
                    FileContent = service.read(FileName);
                    edtFileContent.setText(FileContent);
                    Toast.makeText(getApplicationContext(), string.readSuccess, Toast.LENGTH_LONG).show();
                }
                catch (Exception e) {
                    Toast.makeText(getApplicationContext(), string.readFail + "," + e.toString(), Toast.LENGTH_LONG).show();
                }
            }        
        } 
        
        private final class SDWriteOnClickListener implements View.OnClickListener{

            public void onClick(View v) {
                FileService service = new FileService(getApplicationContext());
                FileName = edtFileName.getText().toString();
                FileContent = edtFileContent.getText().toString();
                try {    
                    //判断SD卡是否存在
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                        service.writeToSDCard(FileName, FileContent);
                    } else {
                        Toast.makeText(getApplicationContext(), string.SDCardNotFound, Toast.LENGTH_LONG).show();
                    }
                    Toast.makeText(getApplicationContext(), string.writeSuccess, Toast.LENGTH_LONG).show();
                }
                catch (Exception e) {
                    Toast.makeText(getApplicationContext(), string.writeFail + "," + e.toString(), Toast.LENGTH_LONG).show();
                }
            }        
        }
        
        private final class SDReadOnClickListener implements View.OnClickListener{

            public void onClick(View v) {
                FileService service = new FileService(getApplicationContext());
                FileName = edtFileName.getText().toString();
                FileContent = edtFileContent.getText().toString();
                try {
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                        FileContent = service.readFromSDCard(FileName);
                        edtFileContent.setText(FileContent);
                        Toast.makeText(getApplicationContext(), string.readSuccess, Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), string.SDCardNotFound, Toast.LENGTH_LONG).show();
                    }
                }
                catch (Exception e) {
                    Toast.makeText(getApplicationContext(), string.readFail + "," + e.toString(), Toast.LENGTH_LONG).show();
                }
            }        
        } 


    /Files/jxgxy/FileReadWrite.rar 

  • 相关阅读:
    线程锁(互斥锁Mutex)及递归锁
    threading模块,python下的多线程
    笔试面试题实现
    Socket编程,SocketServer模块
    iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
    iOS 根据文件名获取到文件路径
    IOS UILabel自动换行
    🐤小记
    iOS 获取当前月份的天数(转)、
    IOS 在Ipad 横屏 上使用UIImagePickerController
  • 原文地址:https://www.cnblogs.com/jxgxy/p/2608366.html
Copyright © 2020-2023  润新知