zoukankan      html  css  js  c++  java
  • 09_读写外部sd卡文件


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.widget.TextView;

    public class ExternalStorageTest extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView textView = new TextView(this);
            setContentView(textView);

            String state = Environment.getExternalStorageState();
            if (!state.equals(Environment.MEDIA_MOUNTED)) {
                textView.setText("No external storage mounted");
            } else {
                File externalDir = Environment.getExternalStorageDirectory();
                File textFile = new File(externalDir.getAbsolutePath()
                        + File.separator + "text.txt");
                try {
                    writeTextFile(textFile, "This is a test. Roger");
                    String text = readTextFile(textFile);
                    textView.setText(text);
                    if (!textFile.delete()) {
                        textView.setText("Couldn't remove temporary file");
                    }
                } catch (IOException e) {
                    textView.setText("something went wrong! " + e.getMessage());
                }
            }
        }

        private void writeTextFile(File file, String text) throws IOException {
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write(text);
            writer.close();
        }

        private String readTextFile(File file) throws IOException {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            StringBuilder text = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                text.append(line);
                text.append("\n");
            }
            reader.close();
            return text.toString();
        }
    }

  • 相关阅读:
    【Codeforces 1172F】—Nauuo and Bug(线段树+双指针)
    【Codeforces 1172F】—Nauuo and Bug(线段树+双指针)
    【BZOJ4671】—异或图(斯特林反演+线性基)
    【BZOJ4671】—异或图(斯特林反演+线性基)
    【Codeforces #453 E】—Little Pony and Lord Tirek(线段树/均摊分析)
    【Codeforces #453 E】—Little Pony and Lord Tirek(线段树/均摊分析)
    【Codeforces 480E】—Parking Lot(线段树+单调队列)
    【Codeforces 480E】—Parking Lot(线段树+单调队列)
    web安全
    Jodd——java瑞士军刀
  • 原文地址:https://www.cnblogs.com/xl711436/p/3060462.html
Copyright © 2011-2022 走看看