Android四大组件-File文件操作

一、创建文件

第一种方法
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>    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值