关于非Activity类中读取文件操作

luotuocao9 2013-08-08 12:20:19
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 ;
}

}



不知如何操作?谢谢大家!
...全文
219 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
luotuocao9 2013-08-09
  • 打赏
  • 举报
回复
按照guoyoulei520的方法解决了,是在Activity中调用读文件方法有误。注释掉的方法,还没太搞懂,过一阵在说。 谢谢大家!
homlee ning 2013-08-08
  • 打赏
  • 举报
回复
getResources()其实activity是父类的一个方法,把activity对象 或者this 或者context作为参数传递给你要的类作为参数就行了,甚至你可以直接用appContext作为上下文调用getResources()
luotuocao9 2013-08-08
  • 打赏
  • 举报
回复
引用 4 楼 QQ718225250 的回复:
Context 可以作为参数传递,就可以使用Context 的public 方法了。 你注释掉的getResources就是其中一种调用。注释掉是什么意思呢,留着会有错误吗 ?有错误贴出来 private Resources getResources() { // TODO Auto-generated method stub Resources mResources=null; mResources = getResources(); return mResources; } 注释掉的是网上查到的一种方法,编译没有显示错误。但运行时没有结果显示,不知问题出在哪里? 在Activity中调用有什么格式吗?不是实例化后直接调用方法吗? 谢谢大家了!
luotuocao9 2013-08-08
  • 打赏
  • 举报
回复
新手,太笨了。 要将下句 InputStream in = getResources().getAssets().open(fileName); 改为: InputStream in = mContext.getResources().getAssets().open(fileName); 编译通过了,可运行显示没结果呀!
homlee ning 2013-08-08
  • 打赏
  • 举报
回复
Context 可以作为参数传递,就可以使用Context 的public 方法了。 你注释掉的getResources就是其中一种调用。注释掉是什么意思呢,留着会有错误吗 ?有错误贴出来 private Resources getResources() { // TODO Auto-generated method stub Resources mResources=null; mResources = getResources(); return mResources; }
凉凉二点凉 2013-08-08
  • 打赏
  • 举报
回复
引用 2 楼 luotuocao9 的回复:
[quote=引用 1 楼 QQ718225250 的回复:] getResources()其实activity是父类的一个方法,把activity对象 或者this 或者context作为参数传递给你要的类作为参数就行了,甚至你可以直接用appContext作为上下文调用getResources()
没太明白,请详细说一说。 或直接改read_file()。谢谢! [/quote]


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 ;
    }
	
}

luotuocao9 2013-08-08
  • 打赏
  • 举报
回复
引用 1 楼 QQ718225250 的回复:
getResources()其实activity是父类的一个方法,把activity对象 或者this 或者context作为参数传递给你要的类作为参数就行了,甚至你可以直接用appContext作为上下文调用getResources()
没太明白,请详细说一说。 或直接改read_file()。谢谢!

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧