forked from hzuapps/android-labs-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
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
169 additions
and
1 deletion.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...p/src/main/java/edu/hzuapps/androidlabs/Soft1714080902240/Soft1714080902240Activity3.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,105 @@ | ||
package edu.hzuapps.androidlabs.soft1714080902240; | ||
|
||
import android.content.Intent; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
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.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class Soft1714080902240Activity3 extends AppCompatActivity { | ||
private TextView edittext; | ||
|
||
public static final String DIRECTORY = "demo"; | ||
public static final String FILENAME = "file_demo.txt"; | ||
public static final String TAG = Soft1714080902240Activity3.class.getSimpleName(); | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
|
||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_soft17140809022403); | ||
|
||
|
||
edittext = (TextView) findViewById(R.id.danci); | ||
Button button = (Button) findViewById(R.id.btn_save); | ||
|
||
button.setOnClickListener(new MyonclickListener()); | ||
} | ||
|
||
private class MyonclickListener implements View.OnClickListener { | ||
|
||
@Override | ||
public void onClick(View v) { | ||
String string = edittext.getText().toString(); | ||
saveTextIntoInternalStorage(string); | ||
} | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
|
||
Toast.makeText(Soft1714080902240Activity3.this,"保存成功", 0).show(); | ||
} | ||
|
||
|
||
} |
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
41 changes: 41 additions & 0 deletions
41
students/soft1714080902240/app/src/main/res/layout/activity_soft17140809022403.xml
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,41 @@ | ||
<?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="edu.hzuapps.androidlabs.soft1714080902240.Soft1714080902240Activity3"> | ||
|
||
<RelativeLayout | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content"> | ||
|
||
<TextView | ||
android:id="@+id/text1" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="请输入您学习的单词" | ||
android:layout_marginTop="20dp" | ||
/> | ||
|
||
<EditText | ||
android:id="@+id/danci" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_below="@+id/text1" | ||
android:layout_marginTop="20dp" | ||
android:ems="15"> | ||
|
||
<requestFocus /> | ||
</EditText> | ||
|
||
<Button | ||
android:id="@+id/btn_save" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="保存单词" | ||
android:layout_marginTop="130dp"/> | ||
</RelativeLayout> | ||
|
||
|
||
</android.support.constraint.ConstraintLayout> |