• 12_文件的保存与读取


    package cn.itcast.files;
    
    import cn.itcast.service.FileService;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class FileActivity extends Activity {
        /** Called when the activity is first created. */
        private EditText filenameText;
        private EditText contentText;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            filenameText = (EditText) findViewById(R.id.filename);
            contentText = (EditText) findViewById(R.id.filecontent);
            
            Button button=(Button) this.findViewById(R.id.button);
            button.setOnClickListener(new ButtonClickListener());
        }
        
        private final class ButtonClickListener implements View.OnClickListener{
            
            public void onClick(View v){
                String filename = filenameText.getText().toString();
                String content = contentText.getText().toString();
                FileService service = new FileService(getApplicationContext());
                try {
                    service.save(filename, content);
                } catch (Exception e) { 
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(), "failed"/*R.string.fail*/, Toast.LENGTH_SHORT);
                    e.printStackTrace();
                }
                
                Toast.makeText(getApplicationContext(),"R.string.success"/*R.string.success*/, Toast.LENGTH_SHORT);
            }
        }
    }
    package cn.itcast.service;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import android.content.Context;
    
    public class FileService {
        private Context context;
        
        public FileService(Context context) {
            super();
            this.context = context;
        }
    
        /**
         * 写入文件
         * @param filename
         * @return
         * @throws Exception
         */
        public void save(String filename, String content) throws Exception{
            // TODO Auto-generated method stub
            //    MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容
            FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
            outStream.write(content.getBytes());
            outStream.close();
        }
    
        /**
         * 读取文件
         * @param filename
         * @return
         * @throws Exception
         */
        public String read(String filename) throws Exception{
            FileInputStream instream = context.openFileInput(filename);
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            int len = 0;
            while ((len=instream.read(buffer))!=-1){
                outStream.write(buffer, 0, len) ;
            }
            
            byte[] data = outStream.toByteArray();
            return new String(data);
        }
        
    }
    package cn.itcast.test;
    
    import android.test.AndroidTestCase;
    import android.util.Log;
    import cn.itcast.service.FileService;
    
    public class FileSericeTest extends AndroidTestCase {
        
      public void testRead() throws Throwable{ FileService svr = new FileService(this.getContext()); String res = svr.read("a.txt"); Log.i(Log.INFO, res); } } public void save(String filename, String content) throws Exception{ // TODO Auto-generated method stub // MODE_PRIVATE:创建出来的文件仅能被本应用访问,另外采用私有模式创建的文件写入文件中的内容会覆盖原文件的内容 FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE); outStream.write(content.getBytes()); outStream.close(); } }
  • 相关阅读:
    Spring、实例化Bean的三种方法
    Spring、编码剖析Spring管理Bean的原理
    Spring、Hello Spring
    Spring、控制反转与依赖注入(概念)
    Hibernate、批量操作数据
    Hibernate、3.6.7在线帮助文档
    Hibernate、乐观锁和悲观锁
    JQuery UI 精品UI推荐
    java 为pdf添加水印图片
    Hibernate 、继承关联映射
  • 原文地址:https://www.cnblogs.com/carl2380/p/4159337.html
Copyright © 2020-2023  润新知