本人开始学习android应用开发,写一点小笔记.希望大家批评指正
package com.xiaoming.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.util.EncodingUtils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.OpenableColumns;
import android.util.Log;
public class FileService {
private Context context;
private static final String TAG = "FileService";
public FileService(Context contex) {
this.context = contex;
}
public void WriteToData(String fileName,String content) throws IOException
{
FileOutputStream fout = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fout.write(content.getBytes());
fout.close();
}
public String ReadFromData(String fileName) throws IOException
{
/*
FileInputStream inStream = context.openFileInput(fileName);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) != -1)
{
outStream.write(buffer,0,len);
}
byte[] data = outStream.toByteArray();
return new String(data);
*/
String res ="";
try{
FileInputStream fin = context.openFileInput(fileName);
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch( IOException e)
{
e.printStackTrace();
}
Log.i(TAG,res);
return res;
}
public void WriteToSDcard(String fileName,String content) throws Exception
{
String sdcardPath = Environment.getExternalStorageDirectory().getPath();
File tempFile = new File(sdcardPath+"/"+fileName);
if( !tempFile.exists() )
{
tempFile.createNewFile();
}
FileOutputStream fout = new FileOutputStream(tempFile);
byte[] buffer = content.getBytes();
fout.write(buffer);
fout.close();
}
public String ReadFromSDcard(String fileName) throws Exception
{
String res = "";
String sdcardPath = Environment.getExternalStorageDirectory().getPath();
FileInputStream fin = new FileInputStream(sdcardPath+"/"+fileName);
int lenth = fin.available();
byte[] buffer = new byte[lenth];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
return res;
}
}
可以写一个测试类,代码如下:
package com.xiaoming.test;
import com.xiaoming.service.FileService;
import android.test.AndroidTestCase;
import android.util.Log;
public class FileServiceTest extends AndroidTestCase {
private static final String TAG = "FileServiceTest";
public void testWriteToSDcard() throws Exception
{
FileService ftest = new FileService(this.getContext());
//这里必须要路径,不然权限不够
String path = this.getContext().getFilesDir().getPath().toString();
String fileName = "text3.txt";
ftest.WriteToSDcard(fileName, "测试");
Log.i(TAG,"写入成功");
}
public void testReadFromData() throws Throwable
{
FileService ftest = new FileService(this.getContext());
String res = "";
String path = this.getContext().getFilesDir().getPath().toString();
String fileName ="text3.txt";
res = ftest.ReadFromSDcard(fileName);
Log.i(TAG,"成功:"+res);
}}