download
download
download
download
download
public class AssetsTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
AssetManager assetManager = getAssets();
InputStream is = null;
try{
is = assetManager.open("texts/mytext.txt");
String text = loadTextFile(is);
textView.setText(text);
}catch(IOException e){
textView.setText("파일을 가져올 수 없습니다.");
}finally{
if(is != null){
try{
is.close();
} catch(IOException e){
textView.setText("파일을 닫을 수 없습니다.");
}
}
}
}
public String loadTextFile(InputStream is)throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int len = 0;
while((len=is.read(bytes))>0)
byteStream.write(bytes,0,len);
return new String(byteStream.toByteArray(),"UTF8");
}
}