Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2 第二次实验 #2210

Merged
merged 12 commits into from
May 24, 2018
29 changes: 29 additions & 0 deletions soft1614080902411/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.edu.hzu.myapplication4">

<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" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Main3Activity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

</manifest>
193 changes: 193 additions & 0 deletions soft1614080902411/Java/Main2Activity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package com.example.edu.hzu.myapplication4;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
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 com.example.edu.hzu.myapplication4.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.bt2)).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {
String text = ((EditText) findViewById(R.id.edit1)).getText().toString();
saveTextIntoInternalStorage(text);
}

});
((Button) findViewById(R.id.bt4)).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {
Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
startActivity(intent);
}
});
}
// 将文字保存到内部存储
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.view1)) //
.setText(result.toCharArray(), 0, result.length());
}
}
18 changes: 18 additions & 0 deletions soft1614080902411/Java/Main3Activity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.edu.hzu.myapplication4;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class Main3Activity extends AppCompatActivity {

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

}
25 changes: 25 additions & 0 deletions soft1614080902411/Java/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.edu.hzu.myapplication4;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1=(Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);

}
});
}
}
Binary file added soft1614080902411/Main2Activity.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902411/Main3Activity.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902411/MainActivity.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading