您的位置:首页 > 移动开发 > Android开发

Android学习犯过的错

2015-12-30 22:43 429 查看
1.文件操作:

感谢: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();
}
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: