Android学习犯过的错
2015-12-30 22:43
429 查看
1.文件操作:
感谢:http://www.cnblogs.com/rayray/p/3408097.html
参考别人博文写了一个简单的读写操作栗子:
效果我就用文字简单描述一下:
一个输入框,一个save按钮。一个show按钮,还有一个展示读取内容的文本框。
代码如下:
file_main.xml:
MainActivity.java:
感谢:http://www.cnblogs.com/rayray/p/3408097.html
参考别人博文写了一个简单的读写操作栗子:
效果我就用文字简单描述一下:
一个输入框,一个save按钮。一个show按钮,还有一个展示读取内容的文本框。
代码如下:
file_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="请输入内容:"/> <EditText android:id="@+id/edt_context" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="save"/> <Button android:id="@+id/btn_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show"/> <TextView android:id="@+id/txt_show" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java:
<span style="white-space:pre"> </span><pre name="code" class="html">package com.zwg.asus.appone1; import android.app.Activity; 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.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.zwg.asus.appone1.fileTest.FileTest; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity implements View.OnClickListener { public static String TAG = "MainActivity"; private EditText edt_context; private TextView txt_show; private String fileName = "text.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.file_main); edt_context = (EditText) findViewById(R.id.edt_context); txt_show = (TextView) findViewById(R.id.txt_show); Button btn_save = (Button) findViewById(R.id.btn_save); Button btn_show = (Button) findViewById(R.id.btn_show); btn_save.setOnClickListener(this); btn_show.setOnClickListener(this); } @Override public void onClick(View v) { FileTest fileTest = null; switch (v.getId()){ case R.id.btn_save: Log.i(TAG,"进入保存方法"); // fileTest = new FileTest(this); // fileTest.save(edt_context.getText().toString(),fileName); this.save(); break; case R.id.btn_show: Log.i(TAG,"进入show方法"); // fileTest = new FileTest(this); // fileTest.show(fileName); this.show(); break; } } //保存 private void save(){ String content = edt_context.getText().toString(); /* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的, * 至于为什么这个地方还有FileNotFoundException抛出,我也比较纳闷。在Context中是这样定义的 * public abstract FileOutputStream openFileOutput(String name, int mode) * throws FileNotFoundException; * openFileOutput(String name, int mode); * 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名 * 该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt * 第二个参数,代表文件的操作模式 * MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖 * MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 * MODE_WORLD_READABLE 公用 可读 * MODE_WORLD_WRITEABLE 公用 可读写 * */ try { FileOutputStream fileOut = openFileOutput(fileName, Activity.MODE_PRIVATE); fileOut.write(content.getBytes()); fileOut.flush(); fileOut.close(); Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //读取 private void show(){ try { FileInputStream inputStream = this.openFileInput(fileName); byte[] bytes = new byte[1024]; StringBuffer sb = new StringBuffer(); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); int len = -1; while ((len=inputStream.read(bytes)) != -1) { sb.append(inputStream.read(bytes)); Log.i(TAG,sb.toString()); arrayOutputStream.write(bytes, 0, len); } inputStream.close(); arrayOutputStream.close(); // String content = arrayOutputStream.toByteArray().toString(); <span style="color:#ff0000;">这里是错误的写法就是因为这里所有我决定记录下来</span> String content = new String(arrayOutputStream.toByteArray()); //<span style="color:#ff0000;">查了new String()的方法后,发现String(byte [] data)是返回的String.所以用这个方法而不是上面的方法。</span> txt_show.setText(content); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- android调用系统相机,获得原图
- soot学习笔记-2.使用soot解析Android apk.
- android之sqlite增删改查
- LedPedometer-3.注册界面和Android随机生成图片验证码
- Android 简单的账号密码登陆界面(IO流)
- android学习日记(一):Fragment详解
- Android 中模仿 Twitter 实现 Toolbar Indicator
- Android图片倒影
- Android逆向反编译之工具介绍
- Android 上实现非root的 Traceroute -- 非Root权限下移植可执行二进制文件 脚本文件
- RXAndroid Single的使用
- android---xml---include用法(导入另一个XML文件到本布局中
- android studio git设置忽略
- Android 绑定数据到界面控件
- Android 自定义控件3
- Android自定义控件2
- android启动界面和引导界面的动画
- android中引入外部字体
- Android 自定义控件1
- Android 反射获得控件对象