Android 读写文件整理

Posted xiaoleiacm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 读写文件整理相关的知识,希望对你有一定的参考价值。

很简单的整理 ,最近实在心力憔悴,贴上整理代码:

package xiaolei.sun.readwrite;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.RandomAccessFile;

public class MainActivity extends AppCompatActivity 

    private TextView textview;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview = (TextView) findViewById(R.id.textview);


        imageView =
                (ImageView) findViewById(R.id.iv_imageview);
    


    public void btn_imagefuc(View view) 


        String path = null;
        try 
            path = Environment.getExternalStorageDirectory().getCanonicalPath() + "/a.png";
           //字节流
            FileInputStream fs = new FileInputStream(path);
           //字节缓冲流
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fs); //bufferedInputStream作为一个流的缓冲池,提高流的写入性能呢


            //在加载前可计算图像大小,进行图片压缩

            // 将流转换为bitmap
            Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
            imageView.setImageBitmap(bitmap);

            bufferedInputStream.close();
            fs.close();
            bitmap = null;
         catch (IOException e) 
            e.printStackTrace();
        


        Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show();

    


    public void getSDfuc(View view) 
        boolean equals = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        File externalStorageDirectory = Environment.getExternalStorageDirectory();
        Toast.makeText(MainActivity.this, externalStorageDirectory + "", Toast.LENGTH_SHORT).show();


    

    public void SDwritefuc(View view) 


        writetoSDcard("SD card example !!!");

        Toast.makeText(MainActivity.this, "SD write !!!", Toast.LENGTH_SHORT).show();
    

    /**
     * 将数据写入SD卡
     *
     * @param content
     */
    public void writetoSDcard(String content) 
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))// 判断SD卡是否进行挂载
        
            File externalStorageDirectory = Environment.getExternalStorageDirectory();// 获取SD卡路径


            // 创建一个文件,并使用RandomAccessFile进行文件写入
            try 
                File file = new File(externalStorageDirectory.getCanonicalPath() + "/text.txt");

                RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
                randomAccessFile.seek(file.length());
                randomAccessFile.write(content.getBytes());//数据写入
                randomAccessFile.close();

             catch (IOException e) 
                e.printStackTrace();
            
        

    

    /**
     * 读取SD卡中的内容
     *
     * @return
     */
    public String ReadFromSDcard() 


        File externalStorageDirectory = Environment.getExternalStorageDirectory(); //获取SD卡路径
        try 
            //获取读取流
            FileInputStream fileInputStream = new FileInputStream(externalStorageDirectory.getCanonicalPath() + "/text.txt");


            //将读取流转换为bufferReader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));


            StringBuilder stringBuilder = new StringBuilder("");
            String line = null;
            //使用bufferReader将stringBuilder拼接起来
            while ((line = bufferedReader.readLine()) != null) 
                stringBuilder.append(line);
            

            bufferedReader.close();
            return stringBuilder.toString();

         catch (IOException e) 
            e.printStackTrace();
        

        return null;
    

    public void SDreadfuc(View view) 
        textview.setText(ReadFromSDcard());
        ;
        Toast.makeText(MainActivity.this, "SD read !!!", Toast.LENGTH_SHORT).show();
    


    public void getdirfuc(View view) 

        File dir = getDir("dirtext.txt", MODE_APPEND);//  在应用程序下获取路径
        File filesDir = getFilesDir();
        String[] strings = fileList();
        deleteFile("text.txt");
        Toast.makeText(MainActivity.this, strings[0], Toast.LENGTH_SHORT).show();
    

    public void writefuc(View view) 

        write("this is a text file!!!");
        Toast.makeText(MainActivity.this, "write", Toast.LENGTH_SHORT).show();
    

    public void readfuc(View view) 
        textview.setText(read());
        Toast.makeText(MainActivity.this, "read", Toast.LENGTH_SHORT).show();
    

    public void write(String content) 
        try 
            FileOutputStream fileOutputStream = openFileOutput("text.txt", MODE_APPEND);
            PrintStream printStream = new PrintStream(fileOutputStream);
            printStream.println(content);
            printStream.close();
         catch (FileNotFoundException e) 
            e.printStackTrace();
        
    

    public String read() 
        try 
            FileInputStream fileInputStream = openFileInput("text.txt");
            byte[] bytes = new byte[1024];
            int hasread = 0;
            StringBuilder stringBuilder = new StringBuilder("");
            while ((hasread = fileInputStream.read(bytes)) > 0) 
                stringBuilder.append(new String(bytes, 0, hasread));
            
            fileInputStream.close();
            return stringBuilder.toString();
         catch (FileNotFoundException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        
        return null;
    





以上是关于Android 读写文件整理的主要内容,如果未能解决你的问题,请参考以下文章

Android - 文件读写操作 总结

Android - 文件读写操作 总结

Android - 文件读写操作 总结

Android - 文件读写操作 总结

Android数据存储- File

android 如何读写文件?