入口文件MainActivity.java
package com.example.aimee.aimee_dealfile; import android.content.Intent; import android.os.Environment; import android.support.v7.app.AppCompatActivity; 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); } public void CreateFile(View view) { Intent i=new Intent(this,InputFileName.class); startActivity(i); } public void OpenFile(View view) { Intent i=new Intent(this,openFileName.class); startActivity(i); } public void delfile(View view) { Intent i=new Intent(this,delFileName.class); startActivity(i); } }
layout文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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/CreateFile" android:onClick="CreateFile" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:text="创建文件" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/OpenFile" android:onClick="OpenFile" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="16dp" android:layout_marginEnd="8dp" android:text="打开文件" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/CreateFile" /> <Button android:id="@+id/button" android:onClick="delfile" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:text="删除文件" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/OpenFile" /> </android.support.constraint.ConstraintLayout>
创建文件InputFileName.java
package com.example.aimee.aimee_dealfile; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.IOException; public class InputFileName extends AppCompatActivity { private EditText inputName; private final int REQUESTCODE=101; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.input_file_name); inputName=(EditText) findViewById(R.id.inputName); } public void startCreate(View view) throws IOException { switch (view.getId()){ case R.id.ok: String path_directory=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"save"; String file_name=inputName.getText().toString().trim(); fileMethodSet fileMethodSet=new fileMethodSet(); fileMethodSet.create_file(path_directory,file_name); Toast.makeText(this,file_name+"文件创建成功",Toast.LENGTH_LONG).show(); inputName.setText(""); Intent i=new Intent(this,MainActivity.class); startActivity(i); break; case R.id.cancel: inputName.setText(""); default: break; } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults); if(requestCode==REQUESTCODE){ if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0]== PackageManager.PERMISSION_GRANTED){ Toast.makeText(this,"ok",Toast.LENGTH_LONG).show(); }else { Toast.makeText(this,"无权限",Toast.LENGTH_LONG).show(); } } } }
layout文件input_file_name.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/inputName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入文件名" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/cancel" android:onClick="startCreate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginBottom="8dp" android:text="取消" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/ok" android:onClick="startCreate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="确认" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </android.support.constraint.ConstraintLayout>
打开文件openFileName.java
package com.example.aimee.aimee_dealfile; import android.content.Intent; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class openFileName extends AppCompatActivity { private EditText openName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.open_file_name); openName=(EditText) findViewById(R.id.openName); } public void startOpen(View view) throws IOException { switch (view.getId()){ case R.id.ok: String path_directory=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"save"; String file_name=openName.getText().toString().trim(); openName.setText(""); fileMethodSet fileMethodSet=new fileMethodSet(); String new_line=fileMethodSet.read_file(path_directory,file_name); fileMethodSet.write_file(file_name,path_directory,"filename.txt"); fileMethodSet.write_file(new_line,path_directory,"temp.txt"); Intent i=new Intent(this,saveFile.class); startActivity(i); break; case R.id.cancel: openName.setText(""); default: break; } } }
layout文件open_file_name.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/openName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入文件名" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/cancel" android:onClick="startOpen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginBottom="8dp" android:text="取消" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/ok" android:onClick="startOpen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="确认" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </android.support.constraint.ConstraintLayout>
保存文件saveFile.java
package com.example.aimee.aimee_dealfile; import android.content.Intent; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class saveFile extends AppCompatActivity { String file_name1; String filename1="temp.txt"; String filename2="filename.txt"; private EditText content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.file_edit); content=(EditText) findViewById(R.id.opename); String path_directory=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"save"; fileMethodSet fileMethodSet=new fileMethodSet(); String new_line= null; try { new_line = fileMethodSet.read_file(path_directory,filename1); file_name1=fileMethodSet.read_file(path_directory,filename2).trim(); File file1=new File(path_directory+"/"+filename2); fileMethodSet.del_file(path_directory,filename2); File file=new File(path_directory+"/"+filename1); fileMethodSet.del_file(path_directory,filename1); } catch (IOException e) { e.printStackTrace(); } content.setText(new_line); } public void startSave(View view) throws IOException { String strcontent=content.getText().toString(); String path_directory=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"save"; fileMethodSet fileMethodSet=new fileMethodSet(); fileMethodSet.write_file(strcontent,path_directory,"temp1.txt"); File oldfile=new File(path_directory+"/"+"temp1.txt"); File newfile=new File(path_directory+"/"+file_name1); oldfile.renameTo(newfile); Toast.makeText(this,"文件保存成功",Toast.LENGTH_LONG).show(); Intent i=new Intent(this,MainActivity.class); startActivity(i); } public void cancelSave(View view) { Intent i=new Intent(this,MainActivity.class); startActivity(i); } }
layout文件file_edit.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/opename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/cancel" android:onClick="cancelSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginBottom="8dp" android:text="取消" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/save" android:onClick="startSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="保存" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </android.support.constraint.ConstraintLayout>
删除文件delFileName.java
package com.example.aimee.aimee_dealfile; import android.content.Intent; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.File; public class delFileName extends AppCompatActivity { private EditText delName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.del_file_name); delName=(EditText) findViewById(R.id.delName); } public void cancelCreate(View view) { Intent i=new Intent(this,MainActivity.class); startActivity(i); } public void startCreate(View view) { String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"save"; String filename=delName.getText().toString().trim(); String final_path=path+"/"+filename; File file=new File(final_path); if(file.exists()){ file.delete(); Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); Intent i=new Intent(this,MainActivity.class); startActivity(i); } } }
layout文件del_file_name.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/delName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入文件名" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/cancel" android:onClick="cancelCreate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginBottom="8dp" android:text="取消" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/ok" android:onClick="startCreate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="确认" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> </android.support.constraint.ConstraintLayout>
权限修改文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aimee.aimee_dealfile"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <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=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <!-- <action android:name="android.intent.action.VIEW" /> --> <!-- <category android:name="android.intent.category.DEFAULT" /> --> <!-- <data android:scheme="file" /> --> <!-- <data android:scheme="content" /> --> <!-- <data android:mimeType="text/plain" /> --> </intent-filter> </activity> <activity android:name=".InputFileName" /> <activity android:name=".openFileName" /> <activity android:name=".saveFile" /> <activity android:name=".delFileName" /> <activity android:name=".fileMethodSet"></activity> </application> </manifest>
方法类文件fileMethodSet.java
package com.example.aimee.aimee_dealfile; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class fileMethodSet{ public String read_file(String path_directory,String file_name) throws IOException { String path=path_directory+"/"+file_name; File file=new File(path); if(!file.exists()){ String new_line="文件不存在"; return new_line; } else { RandomAccessFile raf = new RandomAccessFile(file, "rwd"); int j = 0; while (raf.readLine() != null) { j = j + 1; } raf.seek(0); String new_line = ""; String tep = ""; for (int i = 0; i < j; i++) { String line = new String(raf.readLine().getBytes("ISO-8859-1"), "utf8"); tep = line + " "; new_line = new_line + tep; } raf.close(); return new_line.toString(); } } public void write_file(String strcontent,String path_directory,String file_name) throws IOException { String strFilePath=path_directory+"/"+file_name; String strContent=strcontent; File file=new File(strFilePath); if(!file.exists()){ file.createNewFile(); } RandomAccessFile raf=new RandomAccessFile(file,"rwd"); raf.seek(file.length()); raf.write(strContent.getBytes()); raf.close(); } public void del_file(String path_directory,String file_name){ String path=path_directory+"/"+file_name; File file=new File(path); file.delete(); } public void create_file(String path_directory,String file_name) throws IOException { File file = new File(path_directory); if (!file.exists()) { file.mkdirs(); } String path = path_directory + "/" + file_name; File f = new File(path); if (!f.exists()) { f.createNewFile(); } } }
一个简易的增删查改文件处理器就完成了。