使用流进行文件读写
注意:raw里面的文件只能读取,不能修改
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showFileOperator"
android:text="演示文件操作"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showContentProvider"
android:text="演示ContentProvider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FileOperatorActivity">
<EditText
android:id="@+id/et_input"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="80dp"
android:ems="10"
android:hint="请输入"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_out"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="@+id/et_input"
app:layout_constraintStart_toStartOf="@+id/et_input"
app:layout_constraintTop_toBottomOf="@+id/et_input" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:onClick="write"
android:text="写入"
app:layout_constraintStart_toStartOf="@+id/et_input"
app:layout_constraintTop_toBottomOf="@+id/tv_out" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="read"
android:text="读取"
app:layout_constraintEnd_toEndOf="@+id/et_input"
app:layout_constraintTop_toTopOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:onClick="readRaw"
android:text="raw文件读取"
app:layout_constraintStart_toStartOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:onClick="writeExternal"
android:text="外部文件写入"
app:layout_constraintStart_toStartOf="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="readExternal"
android:text="外部文件读取"
app:layout_constraintEnd_toEndOf="@+id/button3"
app:layout_constraintTop_toTopOf="@+id/button5" />
androidx.constraintlayout.widget.ConstraintLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileOperatorActivity extends AppCompatActivity {
private EditText et_input;
private TextView tv_out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_operator);
et_input = findViewById(R.id.et_input);
tv_out = findViewById(R.id.tv_out);
}
/**
* 演示文件写入
* @param view
*/
public void write(View view){
String content = et_input.getText().toString();
FileOutputStream out = null;
try {
out = openFileOutput("demo.txt", MODE_PRIVATE);
out.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 演示文件读取
* @param view
*/
public void read(View view){
FileInputStream input = null;
try {
input = openFileInput("demo.txt");
int i = input.read();
StringBuffer content = new StringBuffer();
while(i != -1) {
content.append((char) i);
i = input.read();
}
tv_out.setText(content.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 演示raw文件夹下文件读取
* @param view
*/
public void readRaw(View view){
InputStream inputStream = getResources().openRawResource(R.raw.raw_demo);
try {
int i = inputStream.read();
StringBuffer content = new StringBuffer();
while(i != -1) {
content.append((char)i);
i = inputStream.read();
}
tv_out.setText(content.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 演示外部文件写入
* @param view
*/
public void writeExternal(View view) {
//获取外部设备状态,判断外部存储设备是否可用
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
//就是输入输出流的使用
//获取路径
File file = getExternalFilesDir(null);
File demo = new File(file,"demo.txt");
String content = et_input.getText().toString();
FileOutputStream out = null;
try {
out = new FileOutputStream(demo);
out.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(this, "写入完成", Toast.LENGTH_SHORT).show();
}
}
/**
* 演示外部文件读取
* @param view
*/
public void readExternal(View view) {
//判断存储设备是否可用
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(getExternalFilesDir(null),"demo.txt");
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
int i = fileInputStream.read();
StringBuffer content = new StringBuffer();
while(i != -1) {
content.append((char)i);
i = fileInputStream.read();
}
tv_out.setText(content.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 跳转文件操作演示
* @param view
*/
public void showFileOperator(View view){
Intent intent = new Intent(this, FileOperatorActivity.class);
startActivity(intent);
}
/**
* 演示ContentProvider
* @param view
*/
public void showContentProvider(View view) {
/*
*
* 1.获取ContentResolver
* 2.执行操作
* */
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.example.day12.demo");
resolver.delete(uri, null, null);
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.day12">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FileOperatorActivity">activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<provider
android:authorities="com.example.day12.demo"
android:name="com.example.day12.MyContentProvider"
android:exported="true"/>
application>
manifest>
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MyContentProvider extends ContentProvider {
/*
* https://www.baidu.com/
* https://:协议名称
* www.baidu.com:地址
* 以上两部分组成URI
*
* ContentProvider就是通过URI进行访问的
* content://xxxxxxxxx
* */
/**
* 创建时调用,用于初始化必要的数据
* @return
*/
@Override
public boolean onCreate() {
Log.i("demo", "onCreate方法被调用了");
return false;
}
/**
* 查询数据库
* @param uri
* @param projection
* @param selection
* @param selectionArgs
* @param sortOrder
* @return
*/
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return null;
}
/**
* 获取请求类型的
* @param uri
* @return
*/
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
/**
* 向数据库插入数据
* @param uri
* @param values
* @return
*/
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
return null;
}
/**
* 从数据库删除数据
* @param uri
* @param selection
* @param selectionArgs
* @return
*/
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.i("demo", "delete方法被调用了");
return 0;
}
/**
* 更新数据库数据
* @param uri
* @param values
* @param selection
* @param selectionArgs
* @return
*/
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
}