Skip to content

Commit

Permalink
Merge pull request #1970 from zaitao/master
Browse files Browse the repository at this point in the history
#5 #14 第五次实验
  • Loading branch information
zengsn authored May 19, 2018
2 parents bc2c255 + ce3afcc commit 53f0f24
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
Binary file added soft1614080902138/123.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions soft1614080902138/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 第五次实验
## 1.实验目标
+ 掌握在Android App中存储数据
## 2.实验步骤
+ 打开上次实验的项目
+ 点开activity_main.xml文件,给EditText控件添加一个id,添加一个Button控件用于保存数据,添加一个TextView控件用于显示数据保存的路径
+ 参考实验要求五文件操作的代码,选择内部存储的方式,修改MainActivity中的代码,添加监听器以及文件输入输出流
+ 运行程序并截图
+ 使用git将代码提交到自己的库里
+ 在自己的GitHub库上创建和发送Pull Request
+ 在GitHub中使用Markdown文件编写实验报告
## 3.实验结果
![image](https://github.com/zaitao/android-labs-2018/blob/master/soft1614080902138/123.png)
## 4.实验体会
尝试着在手机中搜索存储数据的files_demo文件,想查看一下存储的数据,发现找不到,百度了一下,是因为手机没有root,所以是不能查看内部文件的。
做这几次实验最大的收获是学会了遇到问题尝试着去解决问题,就比如说,运行项目会出现奇怪的错误,有些可以通过百度去解决,有时候百度了也解决不了,
但是删掉原来的项目,重新做一个就没问题了。
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
package edu.hzuapps.androidlabs.Soft1614080902138;
package edu.hzuapps.androidlabs.Soft1614080902138;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;
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 MainActivity extends AppCompatActivity {

public static final String DIRECTORY = "demo";
public static final String FILENAME = "file_demo.txt";

public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -24,5 +44,92 @@ public void onClick(View view) {
//thisActivity.startActivityForResult(intent, 2016);
}
});
((Button) findViewById(R.id.Button11)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = ((EditText) findViewById(R.id.edit_text)).getText().toString();
saveTextIntoInternalStorage(text);
}
});
}
// 将文字保存到内部存储
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 showResult(String result) {
((TextView) findViewById(R.id.view1)) //
.setText(result.toCharArray(), 0, result.length());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@
android:layout_height="38dp"
android:text="播放器" />

<Button
android:id="@+id/Button11"
android:layout_width="match_parent"
android:layout_height="38dp"
android:text="保存" />

<TextView
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="31dp"
android:gravity="center" />




Expand Down

0 comments on commit 53f0f24

Please sign in to comment.