forked from hzuapps/android-labs-2018
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
222 additions
and
8 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,14 +1,158 @@ | ||
package com.hzu.edu.myapplication; | ||
|
||
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.Button; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import android.app.Activity; | ||
import android.app.ListActivity; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ListView; | ||
import android.widget.SimpleAdapter; | ||
import android.widget.Toast; | ||
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 chakan extends AppCompatActivity { | ||
|
||
public static final String DIRECTORY = "demo"; | ||
public static final String FILENAME = "file_demo.txt"; | ||
public static final String TAG = chakan.class.getSimpleName(); | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_chakan); | ||
final Activity thisActivity = this; | ||
((Button) findViewById(R.id.Button5)).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()); | ||
} | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -1,19 +1,89 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<LinearLayout 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:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".chakan" | ||
android:background="@drawable/c"> | ||
android:background="@drawable/c" | ||
tools:context=".chakan"> | ||
|
||
<TextView | ||
android:layout_width="384dp" | ||
android:layout_height="50dp" | ||
android:layout_height="40dp" | ||
android:gravity="center" | ||
android:text="事件查看" | ||
android:textColor="#3232CD" | ||
android:textSize="30dp" | ||
/> | ||
android:textSize="20dp" | ||
android:textStyle="bold"/> | ||
<EditText | ||
android:id="@+id/edit_text" | ||
android:layout_width="match_parent" | ||
android:layout_height="60dp" /> | ||
|
||
</android.support.constraint.ConstraintLayout> | ||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:text="搜索" | ||
android:textSize="20dp" | ||
android:textColor="#000000" | ||
android:textStyle="bold"/> | ||
|
||
<Button | ||
android:id="@+id/Button1" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="1" /> | ||
|
||
<Button | ||
android:id="@+id/Button2" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="星期三去……" /> | ||
|
||
<Button | ||
android:id="@+id/Button3" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="等一下得去……" /> | ||
|
||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:text=" " /> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:text=" " /> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:text=" " /> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:text=" " /> | ||
|
||
<Button | ||
android:id="@+id/Button5" | ||
android:layout_width="match_parent" | ||
android:layout_height="38dp" | ||
android:text="打开" | ||
android:textStyle="bold"/> | ||
|
||
<TextView | ||
android:id="@+id/view1" | ||
android:layout_width="match_parent" | ||
android:layout_height="33dp" | ||
android:gravity="center" /> | ||
|
||
</LinearLayout> |