科技前沿
Android对文件的操作(简单文件读写)顺水鱼财经
2019-05-17 10:24  浏览:11
外汇期货股票比特币交易【IT168 技术】环境:1.系统:window7

2.Android版本:2.2

3.eclipse3.5.2(伽利略)

一、基本的流程图(写入文件)


二、模拟器运行界面

1.主界面


2.保存(不符合条件)


3.正常保存


4.读取文件


5.关于编者


三、代码如下:

1.保存文件的主要代码

保存的主代码

1 bSave.setonClickListener(new View.onClickListener() { 2 3 @Override 4 public void onClick(View v) { 5 // TODO Auto-generated method stub 6 resId=R.string.success; 7 file_name=filename.getText().toString(); 8 file_content=filecontent.getText().toString(); 9 13 public static void save(OutputStream outStream, String content) throws Exception { // 写入数据 outStream.write(content.getBytes()); outStream.close(); } public static String read(InputStream inStream) throws Exception { // 字节流 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; //获取字节数据 while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } //得到字节数据 byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return new String(data); }

四、小结

这个例子只是简单的操作文件的读写,很多地方都不够完善,希望有兴趣的网友们可以互相交流下~~~

发表评论
0评