80,362
社区成员
发帖
与我相关
我的任务
分享
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 ;
}
}
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 ;
}
}
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 ;
}
}