zoukankan      html  css  js  c++  java
  • android 文件内容和 textview 操作

    public class CreateFile extends Activity {
        private BufferedReader reader;
        private TextView tvFileReader;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tvFileReader = (TextView) findViewById(R.id.tvFileReader);
            String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/hadatabase/";
            File fileDir = new File(path);
            // 如果目录不存在就新建目录
            if (!fileDir.exists()) {
                if (!fileDir.mkdirs()) {
                    Log.i("创建目录", "创建目录失败,请检查是否有sdcard读写权限。");
                }
            }
            // 获取 textview 内容
            String str = tvFileReader.getText().toString().trim();
            try {
                FileWriter fw = new FileWriter(path + "bb.txt");
                fw.flush();
                fw.write(str); // 把textview 内容写入到bb.txt文件
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            File readerFile = new File(path + "/bb.txt");
            try {
                reader = new BufferedReader(new FileReader(readerFile));
                String tempString = null;
                while ((tempString = reader.readLine()) != null) {
                    // 把bb.txt文件里的内容添加到 TextView 里
                    tvFileReader.append(tempString); 
                }
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    EasyUI
    EasyUI
    EasyUI
    django MTV架构下的网站开发步骤
    Centos7下安装python3
    python 线程间事件通知
    hadoop MapReduce
    hadoop文件配置
    DataFrame 数据去重
    用selenium获取cookies
  • 原文地址:https://www.cnblogs.com/oldfeel/p/2493660.html
Copyright © 2011-2022 走看看