Skip to content

Commit

Permalink
hzuapps#5 #YZT6 第五次实验
Browse files Browse the repository at this point in the history
  • Loading branch information
YZT6 committed Apr 2, 2019
1 parent 2065665 commit ad941c7
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,26 @@
android:layout_height="match_parent"
tools:context="edu.hzuapps.androidlabs.soft1714080902240.Soft1714080902240Activity2">

</android.support.constraint.ConstraintLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/firstread"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="0dp"></LinearLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击输入今天练习的单词"
tools:layout_editor_absoluteX="116dp"
tools:layout_editor_absoluteY="590dp"
android:layout_marginTop="550dp"
/>
</RelativeLayout>


</android.support.constraint.ConstraintLayout>
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>

0 comments on commit ad941c7

Please sign in to comment.