-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2041 from heyingsen/master
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
...02302/Fifth/java/edu/hzuapps/androidlabs/soft1614080902302/Soft1614080902302Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902302; | ||
|
||
import android.app.Activity; | ||
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.EditText; | ||
import android.widget.TextView; | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import android.util.Log; | ||
import android.widget.Button; | ||
|
||
|
||
|
||
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); | ||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | ||
setSupportActionBar(toolbar); | ||
final Activity thisActivity = this; | ||
Button chaxun = (Button) findViewById(R.id.chaxun); | ||
chaxun.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
Intent intent = new Intent(Main2Activity.this, Main3Activity.class); | ||
thisActivity.startActivity(intent); | ||
} | ||
}); | ||
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | ||
fab.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | ||
.setAction("Action", null).show(); | ||
} | ||
}); | ||
((Button) findViewById(R.id.chaxun)).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
String text = ((EditText) findViewById(R.id.editText)).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()); | ||
|
||
} | ||
private void showResult(String result) { | ||
((TextView) findViewById(R.id.textView)) // | ||
.setText(result.toCharArray(), 0, result.length()); | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
实验五 | ||
|
||
一.实验目的 | ||
掌握在Android App中存储数据 | ||
|
||
二.实验内容 | ||
根据选题要求使用文件存储 | ||
|
||
1.将应用产生的数据保存到文件存储中; | ||
|
||
2.说明使用的文件存储方式:内部 or 外部; | ||
|
||
3.将运行结果截图。 | ||
|
||
三.实验步骤 | ||
|
||
1.建立一个查询按钮 | ||
|
||
2.在按钮上添加监听器 | ||
|
||
3.创建save存储文件的函数,我使用的是内部存储 | ||
|
||
4.调用save函数. | ||
|
||
|
||
四.实验截图 | ||
|
||
![image](https://github.com/heyingsen/android-labs-2018/blob/master/soft1614080902302/Fifth/tupian1.png) | ||
|
||
![image](https://github.com/heyingsen/android-labs-2018/blob/master/soft1614080902302/Fifth/tupian2.png) | ||
|
||
五.实验体会 | ||
|
||
这次实验我学会了简单的数据存储,利用java.io类提供的方法简单读写数据来存储,将文字保存到内部存储,获取内部存储,创建事件监听器对button控件进行处理 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.