From ad047bdbb974655c2ad45d1a0a8053d3182c4fac Mon Sep 17 00:00:00 2001 From: 1614080902102 <949848655@qq.com> Date: Tue, 15 May 2018 21:16:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?#5=20#888=20=E7=AC=AC=E4=BA=94=E6=AC=A1?= =?UTF-8?q?=E5=AE=9E=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/src/main/AndroidManifest.xml | 11 +- .../soft1614080902102/Main1Activity.java | 19 ++ .../soft1614080902102/Main2Activity.java | 219 ++++++++++++++++++ .../src/main/res/layout/activity_main2.xml | 50 ++++ soft1614080902102/shiyan5.jpg | Bin 0 -> 16969 bytes 5 files changed, 294 insertions(+), 5 deletions(-) create mode 100644 soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main2Activity.java create mode 100644 soft1614080902102/app/src/main/res/layout/activity_main2.xml create mode 100644 soft1614080902102/shiyan5.jpg diff --git a/soft1614080902102/app/src/main/AndroidManifest.xml b/soft1614080902102/app/src/main/AndroidManifest.xml index 6faa5369b..bb56c272b 100644 --- a/soft1614080902102/app/src/main/AndroidManifest.xml +++ b/soft1614080902102/app/src/main/AndroidManifest.xml @@ -1,6 +1,10 @@ + + - - + + \ No newline at end of file diff --git a/soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main1Activity.java b/soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main1Activity.java index cc1843a2c..f94509061 100644 --- a/soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main1Activity.java +++ b/soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main1Activity.java @@ -1,13 +1,32 @@ package edu.hzuapps.androidlabs.soft1614080902102; +import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.TextView; public class Main1Activity extends AppCompatActivity { + TextView textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); + textView2= (TextView) findViewById(R.id.textView1); + + textView2.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent=new Intent(Main1Activity.this,Main2Activity.class); + startActivity(intent); + } + }); } } + + + + + + diff --git a/soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main2Activity.java b/soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main2Activity.java new file mode 100644 index 000000000..9d1d622f9 --- /dev/null +++ b/soft1614080902102/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902102/Main2Activity.java @@ -0,0 +1,219 @@ +package edu.hzuapps.androidlabs.soft1614080902102; + +/*public class FileStorageActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_file_storage); + } +}*/ + +import android.content.Context; +import android.os.Bundle; +import android.os.Environment; +import android.support.v7.app.AppCompatActivity; +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.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; + +//import edu.hzuapps.androidlabs.R; + +public class Main2Activity extends AppCompatActivity{ + + public static final String DIRECTORY = "demo"; + public static final String FILENAME = "file_demo.txt"; + + public static final String TAG = Main2Activity.class.getSimpleName(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main2); + + ((Button) findViewById(R.id.button_save_external)).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String text1 = ((EditText) findViewById(R.id.text_content1)).getText().toString(); + String text2 = ((EditText) findViewById(R.id.text_content2)).getText().toString(); + String text3 = ((EditText) findViewById(R.id.text_content3)).getText().toString(); + String text4 = ((EditText) findViewById(R.id.text_content4)).getText().toString(); + saveTextIntoExternalStorage(text1); + saveTextIntoExternalStorage(text2); + saveTextIntoExternalStorage(text3); + saveTextIntoExternalStorage(text4); + } + }); + } + + // 将文字保存到内部存储 + private void saveTextIntoInternalStorage(String text) { + // 获取内部存储目录 + File dir = this.getFilesDir(); + //File dir = getCacheDir(); + + if (dir.isDirectory()) { +// dir.mkdir(); +// dir.mkdirs(); + } + + if (dir.isFile()) { + // D:/Abc.txt , -> D:/Abc1.txt ->D:/abc.txt + } + + File file = new File(dir, FILENAME); + +// try { +// File = File.createTempFile(FILENAME, null, dir); +// } catch (IOException e) { +// e.printStackTrace(); +// } + + if (file.exists()) { // 判断文件是否存在 + Log.i(TAG, file.getAbsolutePath()); + Log.i(TAG, file.length() + ""); // bytes*1024=kb *1024 MB + Log.i(TAG, file.isFile() + ""); + file.canRead(); + file.canWrite(); + file.canExecute(); + + file.getFreeSpace(); + file.getTotalSpace(); + } + + FileOutputStream fos = null; // 字节流 | char | cn : gbk 2 bytes, utf8 3 bytes + + try { // 使用API打开输出流 + fos = openFileOutput(FILENAME, MODE_PRIVATE); + //FileOutputStream fos = new FileOutputStream(file); + fos.write(text.getBytes()); // 写入内容 + fos.close(); // 关闭流 + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + FileReader reader = null; // char + + try { + reader = new FileReader(file.getAbsoluteFile()); + BufferedReader bReader = new BufferedReader(reader); + String line = bReader.readLine(); + Log.i(TAG, "从文件读取的内容: " + line); + bReader.close(); + reader.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + // 显示结果 + showResult(file.getAbsolutePath()); + + // 删除文件 + // file.delete(); + // deleteFile(FILENAME); + } + + // 将文字保存到外部存储 + private void saveTextIntoExternalStorage(String text) { + if (!isExternalStorageWritable()) { + Log.e(TAG, "外部存储不可写!"); + return; + } + + File dir = getPublicExtStorageDir(DIRECTORY, Environment.DIRECTORY_DOWNLOADS); + File file = new File(dir, FILENAME); + + try { + FileOutputStream fos = new FileOutputStream(file); + fos.write(text.getBytes()); + fos.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + // 显示结果 + showResult(file.getAbsolutePath()); + } + + /* Checks if external storage is available for read and write */ + private boolean isExternalStorageWritable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state)) { + return true; + } + return false; + } + + /* Checks if external storage is available to at least read */ + private boolean isExternalStorageReadable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state) || + Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { + return true; + } + return false; + } + + // 创建公开的外部存储目录(App卸载时不会删除) + private File getPublicExtStorageDir(String dirName, String type) { + if (type == null) { // 指定文件类型 + type = Environment.DIRECTORY_PICTURES; + } + File dir = new File(Environment // + .getExternalStoragePublicDirectory(type), dirName); + if (!dir.mkdirs()) { + Log.e(TAG, "目录无法创建!"); + } + + long freeSpace = dir.getFreeSpace(); + Log.i(TAG, "剩余空间大小: " + (freeSpace / 1024 / 1024) + "MB"); + long totalSpace = dir.getTotalSpace(); + Log.i(TAG, "总空间大小: " + (totalSpace / 1024 / 1024) + "MB"); + + return dir; + } + + // 创建私有的外部存储目录(App卸载时会一同删除) + private File getPrivateExtStorageDir(Context context, String dirName, String type) { + if (type == null) { // 指定文件类型 + type = Environment.DIRECTORY_PICTURES; + } + File file = new File(context // + .getExternalFilesDir(type), dirName); + if (!file.mkdirs()) { + Log.e(TAG, "目录无法创建!"); + } + return file; + } + + private void showResult(String result) { + ((TextView) findViewById(R.id.text_path)) // + .setText(result.toCharArray(), 0, result.length()); + } + +} + diff --git a/soft1614080902102/app/src/main/res/layout/activity_main2.xml b/soft1614080902102/app/src/main/res/layout/activity_main2.xml new file mode 100644 index 000000000..557d3d329 --- /dev/null +++ b/soft1614080902102/app/src/main/res/layout/activity_main2.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + +