说明:
文件保存路径为安卓默认路径,/data/应用包名(全名)/files
一、activity
package com.example.filerw; import com.example.server.FileServer; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText contentEdit; private String file_name="file.txt"; private FileServer fileServer; private TextView showText; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contentEdit = (EditText)findViewById(R.id.content); showText = (TextView)findViewById(R.id.showText); fileServer = new FileServer(getApplicationContext()); } public void MainClick(View v){ switch (v.getId()) { case R.id.save: String str = contentEdit.getText().toString(); fileServer.save(file_name, str); break; case R.id.read: String show = fileServer.read(file_name); showText.setText(show); break; default: break; } } //菜单 public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
二、主要实现代码
package com.example.server; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import org.apache.http.util.EncodingUtils; import android.content.Context; public class FileServer { private Context context; private FileInputStream in; private FileOutputStream out; public FileServer(Context context){ this.context = context; } /** * 保存 * @param name */ public void save(String name,String content){ try { out = context.openFileOutput(name, Context.MODE_PRIVATE); out.write(content.getBytes()); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { System.out.println(e.toString()); } } /** * 读文件 * @param name * @return */ public String read(String name){ String file_content = null; try { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); in = context.openFileInput(name);//具有缓冲实例的打开文件 byte[] buf = new byte[1024]; while((in.read(buf))!=-1){ byteOut.write(buf);//将读出来的数据写到缓存中 } file_content = EncodingUtils.getString(byteOut.toByteArray(),"GBK");//转码 byteOut.close(); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (Exception e) { System.out.println(e.toString()); } return file_content; } }
三、xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:padding="0dp" > <EditText android:id="@+id/content" android:inputType="text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入内容" android:textSize="16sp" android:background="@drawable/edit_xmlbg" android:minHeight="30dp" /> <TextView android:id="@+id/showText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/content" /> <Button android:id="@+id/save" android:onClick="MainClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="save" android:layout_alignParentBottom="true" /> <Button android:id="@+id/read" android:onClick="MainClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="read" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/save" /> </RelativeLayout>
//
时间: 2024-10-31 00:37:28