`

读写文件

 
阅读更多

 

http://blog.csdn.net/feilong1105/article/details/6590759

 

1、写文件到SD卡

 

 

private void log(String log) { File file = new File(android.os.Environment .getExternalStorageDirectory().getPath() + "/testcn.txt"); try { if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file); fw.write(log); fw.close(); Log.d("test", "fw.close()"); } catch (Exception e) { e.printStackTrace(); } }


 

权限

 

 

 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>


 

 2、resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)

 

String res = ""; try{ InputStream in = getResources().openRawResource(R.raw.bbi); //在\Test\res\raw\bbi.txt, int length = in.available(); byte [] buffer = new byte[length]; in.read(buffer); //res = EncodingUtils.getString(buffer, "UTF-8"); //res = EncodingUtils.getString(buffer, "UNICODE"); res = EncodingUtils.getString(buffer, "BIG5"); //依bbi.txt的编码类型选择合适的编码,如果不调整会乱码 in.close(); }catch(Exception e){ e.printStackTrace(); } myTextView.setText(res);//把得到的内容显示在TextView上

 

 

 


 

 

 

3、asset中获取文件并读取数据(资源文件只能读不能写)

 

String fileName = "yan.txt"; //文件名字 String res=""; try{ InputStream in = getResources().getAssets().open(fileName); // \Test\assets\yan.txt这里有这样的文件存在 int length = in.available(); byte [] buffer = new byte[length]; in.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); }catch(Exception e){ e.printStackTrace(); }

 

 

 


 

 

 

 3、

  sdcard中去读文件,首先要把文件通过\android-sdk-     windows\tools\adb.exe把本地计算机上的文件copysdcard上去,adb.exe  push e:/Y.txt /sdcard/, 不可以用adb.exe push e:\Y.txt \sdcard\ 同样: 把仿真器上的文件copy到本地计算机上用: adb pull ./data/data/com.tt/files/Test.txt e:/

String fileName = "/sdcard/Y.txt"; //也可以用String fileName = "mnt/sdcard/Y.txt"; String res=""; try{ FileInputStream fin = new FileInputStream(fileName); //FileInputStream fin = openFileInput(fileName); //用这个就不行了,必须用FileInputStream int length = fin.available(); byte [] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); }catch(Exception e){ e.printStackTrace(); } myTextView.setText(res);


 

 

 转载:

 

 http://www.cnblogs.com/freeliver54/archive/2011/09/16/2178910.html

 

 

Android中的多种文件读写操作方法
这里补充一下权限:<!-- 往SDCard写入数据权限 -->  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
<!-- 在SDCard中创建与删除文件权限 -->  
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  package com.ppmeet;  
  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
  
import org.apache.http.util.EncodingUtils;  
  
import android.content.Context;  
  
/** 
* class name:FileService<BR> 
* class description:android文件的一些读取操作<BR> 
* PS: <BR> 
*  
* @version 1.00 2010/10/21 
* @author CODYY)peijiangping 
*/  
public class FileService {  
    private Context context;  
  
    public FileService(Context c) {  
        this.context = c;  
    }  
  
    // 读取sd中的文件  
    public String readSDCardFile(String path) throws IOException {  
        File file = new File(path);  
        FileInputStream fis = new FileInputStream(file);  
        String result = streamRead(fis);  
        return result;  
    }  
  
    // 在res目录下建立一个raw资源文件夹,这里的文件只能读不能写入。。。  
    public String readRawFile(int fileId) throws IOException {  
        // 取得输入流  
        InputStream is = context.getResources().openRawResource(fileId);  
        String result = streamRead(is);// 返回一个字符串  
        return result;  
    }  
  
    private String streamRead(InputStream is) throws IOException {  
        int buffersize = is.available();// 取得输入流的字节长度  
        byte buffer[] = new byte;  
        is.read(buffer);// 将数据读入数组  
        is.close();// 读取完毕后要关闭流。  
        String result = EncodingUtils.getString(buffer, "UTF-8");// 设置取得的数据编码,防止乱码  
        return result;  
    }  
  
    // 在assets文件夹下的文件,同样是只能读取不能写入  
    public String readAssetsFile(String filename) throws IOException {  
        // 取得输入流  
        InputStream is = context.getResources().getAssets().open(filename);  
        String result = streamRead(is);// 返回一个字符串  
        return result;  
    }  
  
    // 往sd卡中写入文件  
    public void writeSDCardFile(String path, byte[] buffer) throws IOException {  
        File file = new File(path);  
        FileOutputStream fos = new FileOutputStream(file);  
        fos.write(buffer);// 写入buffer数组。如果想写入一些简单的字符,可以将String.getBytes()再写入文件;  
        fos.close();  
    }  
  
    // 将文件写入应用的data/data的files目录下  
    public void writeDateFile(String fileName, byte[] buffer) throws Exception {  
        byte[] buf = fileName.getBytes("iso8859-1");  
        fileName = new String(buf, "utf-8");  
        // Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND  
        // Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。  
        // Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。  
        // MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。  
        // 如果希望文件被其他应用读和写,可以传入:  
        // openFileOutput("output.txt", Context.MODE_WORLD_READABLE +  
        // Context.MODE_WORLD_WRITEABLE);  
        FileOutputStream fos = context.openFileOutput(fileName,  
                Context.MODE_APPEND);// 添加在文件后面  
        fos.write(buffer);  
        fos.close();  
    }  
  
    // 读取应用的data/data的files目录下文件数据  
    public String readDateFile(String fileName) throws Exception {  
        FileInputStream fis = context.openFileInput(fileName);  
        String result = streamRead(fis);// 返回一个字符串  
        return result;  
    }  
}  这里提供一个我写好的封装类。
解释都放注释里面了。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics