您的位置:首页 > 运维架构

文件存储openFileOutput和openFileInput 和String.getBytes()

2014-03-25 14:50 417 查看
android中提供openFileOutput和openFileInput进行读写,两个方法和SharedPreference相类似,但是要注意的是Sharedpreference在读取数据的时候如果为空,则读取默认值,而文件操作中则没有此内容,

在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组。这个表示在不通OS下,返回的东西不一样!

String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,如

byte[] b_gbk = "中".getBytes("GBK");

byte[] b_utf8 = "中".getBytes("UTF-8");

byte[] b_iso88591 = "中".getBytes("ISO8859-1");

将分别返回“中”这个汉字在GBK、UTF-8和ISO8859-1编码下的byte数组表示,此时b_gbk的长度为2,b_utf8的长度为3,b_iso88591的长度为1。

而与getBytes相对的,可以通过new String(byte[], decode)的方式来还原这个“中”字时,这个new String(byte[], decode)实际是使用decode指定的编码来将byte[]解析成字符串。

String s_gbk = new String(b_gbk,"GBK");

String s_utf8 = new String(b_utf8,"UTF-8");

String s_iso88591 = new String(b_iso88591,"ISO8859-1");

通过打印s_gbk、s_utf8和s_iso88591,会发现,s_gbk和s_utf8都是“中”,而只有s_iso88591是一个不认识的字符,为什么使用ISO8859-1编码再组合之后,无法还原“中”字呢,其实原因很简单,因为ISO8859-1编码的编码表中,根本就没有包含汉字字符,当然也就无法通过"中".getBytes("ISO8859-1");来得到正确的“中”字在ISO8859-1中的编码值了,所以再通过new
String()来还原就无从谈起了。

因此,通过String.getBytes(String decode)方法来得到byte[]时,一定要确定decode的编码表中确实存在String表示的码值,这样得到的byte[]数组才能正确被还原。

此示例,可以将用户输入的内容存储到默认位置和SD卡中,并且可以读出数据

public class MyFile extends Activity {

/*声明控件对象*/

private Button bt_save, bt_saveto, bt_read;

private TextView tv;

private EditText et;

private String et_str;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

/*得到控件对象*/

bt_save = (Button) findViewById(R.id.bt_save);

bt_saveto = (Button) findViewById(R.id.bt_saveto);

bt_read = (Button) findViewById(R.id.bt_read);

tv = (TextView) findViewById(R.id.tv);

et = (EditText) findViewById(R.id.et);

/*监听点击事件*/

bt_save.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

et_str = et.getText().toString();//取得EidtText的内容

write(et_str);//写入

}

});

bt_read.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

read();//读取

}

});

bt_saveto.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

et_str = et.getText().toString();//取得EidtText的内容

writeToSd(et_str);//写入到sd卡

}

});

}

/**将数据写入系统默认位置

* @param text

* 要保存的字符

*/

public void write(String text)

{

try {

//通过openFileOutput方法得到一个输出流,方法参数为创建的文件名(不能有斜杠),操作模式

FileOutputStream fos=this.openFileOutput("myfirst.txt",Context.MODE_WORLD_READABLE);

fos.write(text.getBytes());//写入

fos.close(); // 关闭输出流

//弹出Toast消息

Toast.makeText(MyFile.this,"保存成功",Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

catch (IOException e){

e.printStackTrace();

}

}

/**将数据写入到sd卡中

*

* @param text

* 要保存的字符

*/

public void writeToSd(String text)

{

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录

File sdFile = new File(sdCardDir, "myfirstsd.txt");

try {

FileOutputStream fos = new FileOutputStream(sdFile);

fos.write(text.getBytes());

fos.close();

Toast.makeText(MyFile.this, "成功保存到sd卡", Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**读取文件*/

public void read()

{

try {

FileInputStream fis=this.openFileInput("myfirst.txt"); //获得输入流

//用来获得内存缓冲区的数据,转换成字节数组

ByteArrayOutputStream stream=new ByteArrayOutputStream();

byte[] buffer=new byte[1024];

int length=-1;

while((length=fis.read(buffer))!=-1) {

stream.write(buffer,0,length);//获取内存缓冲区中的数据

}

stream.close(); //关闭

fis.close();

tv.setText(stream.toString()); //设置文本控件显示内容

Toast.makeText(MyFile.this,"读取成功",Toast.LENGTH_LONG).show();//弹出Toast消息

} catch (FileNotFoundException e) {

Toast.makeText(MyFile.this, "文件不存在", Toast.LENGTH_SHORT).show();

e.printStackTrace();

}

catch (IOException e){

e.printStackTrace();

}

}

<!-- 在SDCard中创建与删除文件的权限 -->

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!-- 往SDCard写入数据的权限 -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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