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();
                    }
                }
            }
        }
    }
  • 相关阅读:
    TextFlow with JavaFX 2
    搞IT的技术人员为什么会如此苦逼
    Spring MVC3.0.5搭建全程
    JetNuke笔记 ( by quqi99 )
    What is the difference between application server and web server?
    Customize Netbeans Platform Splash Screen and About Dialog
    caffe-win10-cifar10
    Ubuntu14.04+caffe+CPU
    win10+caffe+GPU
    Majority Element(169) && Majority Element II(229)
  • 原文地址:https://www.cnblogs.com/oldfeel/p/2493660.html
Copyright © 2011-2022 走看看