Skip to content

Commit

Permalink
hzuapps#5 hzuapps#1336 第五次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
as6296463 committed May 19, 2018
1 parent b97c108 commit a32ba0b
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 0 deletions.
26 changes: 26 additions & 0 deletions com1614080901219/app4/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.com1614080901219">

<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=".com1614080901219activity1"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".com1614080901219activity2" />
<activity android:name=".com1614080901219activity3"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package edu.hzuapps.androidlabs.com1614080901219;

import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;

public class com1614080901219activity1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com1614080901219activity1);

final Activity thisActivity = this;

final Button button1 = (Button) findViewById(R.id.button_1);
final Button button2 = (Button) findViewById(R.id.button_2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.equals(button1)) {
Intent intent = new Intent(thisActivity,com1614080901219activity2.class);
thisActivity.startActivity(intent);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.equals(button2)) {
Intent intent = new Intent(thisActivity,com1614080901219activity3.class);
thisActivity.startActivity(intent);
}
}
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package edu.hzuapps.androidlabs.com1614080901219;

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;


public class com1614080901219activity3 extends AppCompatActivity {
public static final String DIRECTORY = "demo";
public static final String FILENAME = "file_demo.txt";

public static final String TAG = com1614080901219activity3.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com1614080901219activity3);

((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();
saveTextIntoExternalStorage(text1);
saveTextIntoExternalStorage(text2);
}
});
}

// 将文字保存到内部存储
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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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=".com1614080901219activity3">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/p7"
android:orientation="vertical"
tools:layout_editor_absoluteX="62dp"
tools:layout_editor_absoluteY="0dp"
tools:ignore="MissingConstraints">

<EditText
android:id="@+id/text_content1"
android:hint=" 请输入你的昵称"
android:background="@null"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/text_content2"
android:background="@null"
android:hint=" 请输入你的年龄"
android:textCursorDrawable="@null"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button_save_external"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存到外部存储" />

<Button
android:id="@+id/button_save_internal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:text="保存到内部存储" />

<TextView
android:id="@+id/text_path"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

</android.support.constraint.ConstraintLayout>
Binary file added com1614080901219/e1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a32ba0b

Please sign in to comment.