android开发学习_文件操作

本人开始学习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);
 }}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值