关于非Activity类中读取文件操作
Android新手,请教问题。
现在正在学习Android,安装和配置了环境,测试了helloworld,一切正常。
下面想学习对文件的操作,参考了网上的例子,可以读取assets和sdcard中的文件。
源文件请看下边:
package com.example.helloworld;
import java.io.FileInputStream;
import java.io.InputStream;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
String str;
String filename;
//private static Context context=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//另一种显示文本的方法
// str="This is a test.";
// tv.setText(str);
filename="phone_bj.txt";
str=readfile(filename);
// filename="/sdcard/phone.txt";
// str=readfile_sd(filename);
TextView tv=new TextView(this);
tv.setText(str);
setContentView(tv);
}
@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;
}
/*
public static Context getContext(){
return context;
}
*/
//读取assets中文件
private String readfile(String fileName) {
String filecontent = null;
try{
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte [length];
in.read(buffer);
filecontent = new String(buffer, "GBK");
}catch(Exception e){
e.printStackTrace();
}
return filecontent ;
}
//读取sdcard中文件
private String readfile_sd(String fileName) {
String filecontent = null;
try{
FileInputStream fis = new FileInputStream(fileName);
int length = fis.available();
byte [] buffer = new byte [length];
fis.read(buffer);
filecontent = new String(buffer, "GBK");
fis.close();
}catch(Exception e){
e.printStackTrace();
}
return filecontent ;
}
}
我现在的问题是想把有关文件读取操作放到另一个普通class中,类似下面的样子:
package com.example.helloworld;
import java.io.FileInputStream;
import java.io.InputStream;
import android.content.res.Resources;
public class read_file {
/*
private Resources getResources() {
// TODO Auto-generated method stub
Resources mResources=null;
mResources = getResources();
return mResources;
}
*/
//读取assets中文件
private String readfile(String fileName) {
String filecontent = null;
try{
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte [length];
in.read(buffer);
filecontent = new String(buffer, "GBK");
}catch(Exception e){
e.printStackTrace();
}
return filecontent ;
}
//读取sdcard中文件
private String readfile_sd(String fileName) {
String filecontent = null;
try{
FileInputStream fis = new FileInputStream(fileName);
int length = fis.available();
byte [] buffer = new byte [length];
fis.read(buffer);
filecontent = new String(buffer, "GBK");
fis.close();
}catch(Exception e){
e.printStackTrace();
}
return filecontent ;
}
}
不知如何操作?谢谢大家!
Android Activity getResources()
[解决办法]
getResources()其实activity是父类的一个方法,把activity对象 或者this 或者context作为参数传递给你要的类作为参数就行了,甚至你可以直接用appContext作为上下文调用getResources()
[解决办法]
package com.example.helloworld;
import java.io.FileInputStream;
import java.io.InputStream;
import android.content.res.Resources;
import android.content.Context;
public class read_file {
private Context mContext;
read_file(Context context)
{
mContext = context;
}
/*
private Resources getResources() {
// TODO Auto-generated method stub
Resources mResources=null;
mResources = mContext.getResources();
return mResources;
}
*/
//??assets???
private String readfile(String fileName) {
String filecontent = null;
try{
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte [length];
in.read(buffer);
filecontent = new String(buffer, "GBK");
}catch(Exception e){
e.printStackTrace();
}
return filecontent ;
}
//??sdcard???
private String readfile_sd(String fileName) {
String filecontent = null;
try{
FileInputStream fis = new FileInputStream(fileName);
int length = fis.available();
byte [] buffer = new byte [length];
fis.read(buffer);
filecontent = new String(buffer, "GBK");
fis.close();
}catch(Exception e){
e.printStackTrace();
}
return filecontent ;
}
}