內建SD卡存取
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
//
public class MainActivity extends ActionBarActivity {
private static final String TAG = null;
private String path ;
private String path1;
private String content ;
private String content1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
path = "test.txt";
path1 = "/sdcard/nihao.txt";
try {
//WriteFile(path);
// WriteFile1(path1);
ReadFile(path);
ReadFile1(path1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//覆蓋選單方法
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//寫檔案在 (記憶體)上採用OpenFileOutput方法
public void WriteFile(String path) throws IOException{
int len = 0;
String sentence = "sdfsdf";
byte[] buffer = sentence.getBytes();
len = buffer.length;
FileOutputStream out = openFileOutput(path, MODE_PRIVATE);
//使用openFileOutput方法記憶體地址不能有分割符
try {
out.write(buffer,0 ,len);
Log.i(TAG, "檔案生成");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
}
//寫檔案在(Sdcard)上採用FileOutputStream方法 ,注意加許可權
public void WriteFile1(String path) throws IOException{
FileOutputStream fout = new FileOutputStream(path);
byte[] buffer = "dfgdfgdfg".getBytes();
int len = buffer.length;
fout.write(buffer, 0, len);
fout.close();
Log.i(TAG, "成功");
}
//讀檔案在(記憶體)上採用openFileInput的方法
public void ReadFile(String path) throws IOException
{
String content = null ;
FileInputStream read = openFileInput(path);
byte[] aaa = new byte[read.available()];
read.read(aaa);
content = new String(aaa);
Log.i(TAG, content);
//建立一個記憶體輸出流物件
/* ByteArrayOutputStream outstream = new ByteArrayOutputStream();
int len = 0;
byte [] buffer = new byte[1024];
try {
while((len = read.read(buffer)) != -1){
outstream.write(buffer, 0, len);
}
content = outstream.toByteArray();
Log.i(TAG, new String(content));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
//讀檔案在(Sdcard)上採用FileInputStream
public void ReadFile1(String path){
String contentsd = "";
try {
FileInputStream input = new FileInputStream(path);
byte[] buffer = new byte[input.available()];
input.read(buffer);
contentsd = new String(buffer);
Log.i(TAG, contentsd);
; input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class File extends Activity{
private static final String TAG = null;
private String filepath ; //檔案的路徑
public File(){
}
//寫檔案在 (記憶體)上採用OpenFileOutput
public void WriteFile(String path) throws IOException{
int len = 0;
String sentence = "sdfsdf";
byte[] buffer = sentence.getBytes();
len = buffer.length;
FileOutputStream out = openFileOutput(path, MODE_PRIVATE);
try {
out.write(buffer,0 ,len);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.close();
}
}
}