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();
                    }
                }
            }
        }
    }
  • 相关阅读:
    个人对BFC的见解
    事件简单示例
    visual studio .net 2003
    C# Dictionary 的几种遍历方法
    C# 监听文件夹
    调用SAP dll 出现 试图加载格式不正确的程序
    SAP Connector 类概述
    Sapnco3.0 RFC Server Programs Receive Idocs
    微软工具下载地址
    Sapnco3.0 RFC Client Programs
  • 原文地址:https://www.cnblogs.com/oldfeel/p/2493660.html
Copyright © 2011-2022 走看看