zoukankan      html  css  js  c++  java
  • ANDROID笔记:File操作

    package com.example.android_file;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // write("测试字符串");
                    writeSDFile("测试字符串");
                }
            });
            Button button2 = (Button) findViewById(R.id.button2);
            button2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // Toast.makeText(MainActivity.this, read(), 200).show();
                    Toast.makeText(MainActivity.this, readSDFile(), 200).show();
                }
            });
        }
    
        /**
         * 向程序作用域文件写入内容,路径:/data/data/com.example.android_file/files
         * 
         * @param content
         */
        private void write(String content) {
            try {
                FileOutputStream fileOutputStream = openFileOutput("file.txt",
                        Activity.MODE_APPEND);
                PrintStream printStream = new PrintStream(fileOutputStream);
                printStream.println(content);
                printStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * 获取程序作用域文件的内容,路径:/data/data/com.example.android_file/files
         * 
         * @return
         */
        private String read() {
            StringBuffer stringBuffer = new StringBuffer();
            FileInputStream fileInputStream = null;
            try {
                // openFileInput
                fileInputStream = openFileInput("file.txt");
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = fileInputStream.read(bytes)) != -1) {
                    stringBuffer.append(new String(bytes, "utf-8"));
    
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return stringBuffer.toString();
    
        }
    
        /**
         * 向SD文件写入内容
         * 
         * @param content
         */
        private void writeSDFile(String content) {
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                try {
                    // 获取sd的路径
                    String path = Environment.getExternalStorageDirectory()
                            .getCanonicalPath() + "/file.txt";
                    FileOutputStream fileOutputStream = new FileOutputStream(path,
                            true);// true:追加方式
                    PrintStream printStream = new PrintStream(fileOutputStream);
                    printStream.println(content);
                    printStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        /**
         * 获取SD卡文件内容
         * 
         * @return
         */
        private String readSDFile() {
            StringBuffer stringBuffer = new StringBuffer();
            FileInputStream fileInputStream = null;
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 获取sd的路径
                try {
                    String path = Environment.getExternalStorageDirectory()
                            .getCanonicalPath() + "/file.txt";
                    fileInputStream = new FileInputStream(path);
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = fileInputStream.read(bytes)) != -1) {
                        stringBuffer.append(new String(bytes, "utf-8"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
            }
            return stringBuffer.toString();
        }
    }
    openFileOutput和openFileInput 只能用于/data/data/包名/files 下文件的操作
  • 相关阅读:
    [CSP-S模拟测试]:甜圈(线段树)
    BZOJ4539 [Hnoi2016]树 【倍增 + 主席树】
    Myhchael原创题系列 Mychael vs Kid 【题解】
    BZOJ2668 [cqoi2012]交换棋子 【费用流】
    BZOJ1596 [Usaco2008 Jan]电话网络 【树形dp】
    BZOJ3427 Poi2013 Bytecomputer 【dp】
    BZOJ3526 [Poi2014]Card 【线段树】
    BZOJ3542 DZY Loves March 【map + 线段树】
    BZOJ3832 [Poi2014]Rally 【拓扑序 + 堆】
    HDU 1083
  • 原文地址:https://www.cnblogs.com/afluy/p/3410808.html
Copyright © 2011-2022 走看看