Android-文件存储-资源文件

需要把文件保存成UTF-8编码(txt默认的是ASNI编码),或者在代码中用EncodingUtil类进行转码

放在res/raw

示例如下:

package com.example.testtext;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		
		Resources resources=getResources();
		InputStream inputStream=resources.openRawResource(R.raw.potato);
		Scanner scanner=new Scanner(inputStream);
		while(scanner.hasNext()){
			Log.e("Potato", scanner.next());
		}
		scanner.close();
		try {
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

貌似只能读不能写


版权声明:本文为u010021900原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。