Android四大组件-File文件操作

来源:互联网 发布:结构优化 编辑:程序博客网 时间:2024/05/20 00:13

一、创建文件

第一种方法

File file=new File("/mnt/sdcard/test");        if(!file.exists()){            try {                file.createNewFile();                Log.i("info","创建成功");            } catch (IOException e) {                e.printStackTrace();            }        }        else {            Toast.makeText(MainActivity.this,"已经存在",Toast.LENGTH_LONG).show();            Log.i("info","已经存在");        }

第二种方法

这种方法打开文件,如果不存在会新建一个同名文件

FileOutputStream fos=openFileOutput("a.txt",MODE_PRIVATE);

二、文件操作

少啰嗦直接上代码MainActivity.java

package com.example.tr.file;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    EditText editText;    Button btn_w;    Button btn_r;    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        editText= (EditText) findViewById(R.id.edit1);        btn_w= (Button) findViewById(R.id.write);        btn_r= (Button) findViewById(R.id.read);        textView= (TextView) findViewById(R.id.text);        btn_w.setOnClickListener(this);        btn_r.setOnClickListener(this);        File file=new File("/mnt/sdcard/test");        if(!file.exists()){            try {                file.createNewFile();                Log.i("info","创建成功");            } catch (IOException e) {                e.printStackTrace();            }        }        else {            Toast.makeText(MainActivity.this,"已经存在",Toast.LENGTH_LONG).show();            Log.i("info","已经存在");        }        //; /mnt/sdard/Android/data<包名>    }    public void WriteFiles(String content){        try {            FileOutputStream fos=openFileOutput("a.txt",MODE_PRIVATE);            fos.write(content.getBytes());            fos.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    public String readFiles(){        String content=null;        FileInputStream fis= null;        try {            fis = openFileInput("a.txt");        ByteArrayOutputStream baos=new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len=0;        while((len=fis.read(buffer))!=-1){            baos.write(buffer,0,len);        }        content=baos.toString();        fis.close();        }        catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return content;    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.write:                WriteFiles(editText.getText().toString());                break;            case R.id.read:                textView.setText(readFiles());                break;        }    }}textView.setText(readFiles());     break;    }}}

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <EditText        android:id="@+id/edit1"        android:layout_width="match_parent"        android:layout_height="50dp" />    <Button        android:id="@+id/write"        android:layout_width="match_parent"        android:layout_height="50dp"        android:text="写入"        />    <Button        android:id="@+id/read"        android:layout_width="match_parent"        android:layout_height="50dp"        android:text="读出"        />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="match_parent" /></LinearLayout>    
0 0
原创粉丝点击