package com.example.androidfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.xml.sax.InputSource;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText e1,e2;
private TextView t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editText1);
e2=(EditText) findViewById(R.id.editText2);
t1=(TextView) findViewById(R.id.textView3);
t2=(TextView) findViewById(R.id.textView4);
}
public void store(View view) {
// TODO Auto-generated method stub
FileOutputStream fos=null;
try {
fos=this.openFileOutput(e1.getText().toString(),Context.MODE_APPEND);
fos.write(e2.getText().toString().getBytes());
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}finally{
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void show(View view) {
// TODO Auto-generated method stub
FileInputStream fis=null;
String s="";
try {
fis=this.openFileInput(e1.getText().toString());
byte[] buffer=new byte[1024];
int len=-1;
while ((len=fis.read(buffer))!=-1) {
s+=new String(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
t2.setText(s);
}
public void storeSD(View view) {
// TODO Auto-generated method stub
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File sdDir=Environment.getExternalStorageDirectory();
File file=new File(sdDir,e1.getText().toString());
FileOutputStream fos=null;
try {
fos=new FileOutputStream(file,true);
fos.write(e2.getText().toString().getBytes());
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}finally{
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public void showSD(View view) {
// TODO Auto-generated method stub
String s="SD卡不可用";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File sdCardDir=Environment.getExternalStorageDirectory();
File file=new File(sdCardDir,e1.getText().toString());
FileInputStream fis=null;
try {
fis=new FileInputStream(file);
byte[] buffer=new byte[1024];
int len=-1;
s="";
while ((len=fis.read(buffer))!=-1) {
s+=new String(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// public void domnImage(View view) {
// try {
// URL url=new URL("");
// if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
// URLConnection con=url.openConnection();
// InputStream is=con.getInputStream();
// File sd=Environment.getExternalStorageDirectory();
// File file=new File(sd,System.currentTimeMillis()+".jpg");
// FileInputStream fis=null;
// fis=new FileInputStream(file);
// byte[] buffer=new byte[1024];
// int len=-1;
//
// while (((len=fis.read())!=-1)) {
//
// }
// }
// } catch (MalformedURLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
// }
}
<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" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题:" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="15dp"
android:text="内容:" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView2"
android:layout_marginLeft="67dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/textView1"
android:lines="5"
android:ems="10" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText2"
android:layout_marginRight="26dp"
android:onClick="show"
android:text="显示" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignRight="@+id/button2"
android:onClick="showSD"
android:text="显示SD" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:onClick="storeSD"
android:text="保存SD" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText2"
android:onClick="store"
android:text="保存" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button3"
android:text="TextView" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/textView4"
android:onClick="domnImage"
android:text="下载图片" />
</RelativeLayout>