/无论是本地的文本,还是assets或raw中的文本,还是网络的文本。。。是不能直接用的。。。因此归纳了一下,贴出该码,实现各种文本对象的转换-----希望对大家有用
///<-------------工程里的resource-------------------->
package com.zhongxicai.ReadLocalFile;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
public class ReadSourceText {
private Context context;
public ReadSourceText(){
super();
}
public ReadSourceText(Context context){
this.context=context;
}
/**
* 读取String对象,返回InputStream
* */
public InputStream readStringToInputStream(String string){
InputStream inputstream=null;
byte b[]= string.getBytes();
inputstream=new ByteArrayInputStream(b);
return inputstream;
}
/**
* 读取raw中的文本文件,返回InputStream
*/
public InputStream readRawToInputStream(int Id)
{
InputStream inputstream=null;
inputstream=context.getResources().openRawResource(Id);
return inputstream;
}
/**
* 读取assets中的文本文件,返回InputStream
*/
public InputStream readAssetsToInputStream(String name){
InputStream inputstream = null;//输入流
try {
inputstream= context.getAssets().open(name);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//注意编码格式
return inputstream;
}
/**
* 读取assets中的文本文件,返回String
* */
public String readAssestToString(String name){
InputStream in = null;//输入流
BufferedReader reader = null;//缓存
StringBuilder sb = new StringBuilder();//用于存放读取的数据
try{
in = readAssetsToInputStream(name);
reader = new BufferedReader(new InputStreamReader(in));//将读取到的数据保存在缓存中
String line = null;
while((line = reader.readLine())!=null){//如果读取没结束
sb.append(line);//在保持的数据结尾追加新读取的一行数据
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
///////<--------------本地文件------------------》
package com.zhongxicai.ReadLocalFile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class ReadLocalText {
/**
* 读取本地文本,返回InputStream
*/
public InputStream readFileToInputstream(String filepath)
{
InputStream inputStream = null;
try
{
File file=new File(filepath);
inputStream = new FileInputStream(file);
}
catch (IOException e)
{
e.printStackTrace();
}
return inputStream;
}
/**
* 读取本地文本,返回String
* */
public String readFileToString(String filepath){
StringBuffer buffer=new StringBuffer();
try{
FileInputStream fis=new FileInputStream(filepath);
//或者InputStream fis=new readFileToInputstream(filepath)
InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
Reader reader=new BufferedReader(isr);
int ch;
while((ch=reader.read())>-1){
buffer.append((char)ch);
}
reader.close();
}
catch(IOException e){
e.printStackTrace();
}
return buffer.toString();
}
}
/////《--------------网络------------------》
package com.zhongxicai.UrlDownLoad;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.Log;
public class BookHttpDownload {
/**
* 由url-----》获得InputStream----可行
* */
public InputStream getInputStream(String xmlUrlPath)
{
URL infoUrl = null;
InputStream inStream = null;
try
{
infoUrl = new URL(xmlUrlPath);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
{
inStream = httpConnection.getInputStream();
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return inStream;
}
/**
* 由inputstream-----获得 byte[]
* */
public byte[] getBytesFromStream(InputStream inputStream) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
while (len != -1) {
try {
len = inputStream.read(b);
} catch (IOException e) {
e.printStackTrace();
}
if (len != -1) {
baos.write(b, 0, len);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return baos.toByteArray();
}
/**
* 由url----》获得byte[]
* */
public byte[] getBytesFromUrl(String urlstr) {
BookHttpDownload httpload=new BookHttpDownload();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
InputStream inputStream = null;
int len = 0;
while (len != -1) {
inputStream=httpload.getInputStream(urlstr);
try {
len = inputStream.read(b);
} catch (IOException e) {
e.printStackTrace();
}
if (len != -1) {
baos.write(b, 0, len);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return baos.toByteArray();
}
/**
* 获取URL输入流,转为String----可行
* */
public String getUrlString(String urlstr){
StringBuilder sb=new StringBuilder(512);
URL url = null;
try {
url = new URL(urlstr);
} catch (MalformedURLException e) {
e.printStackTrace();
}
InputStreamReader urlStream = null;
try {
urlStream = new InputStreamReader(url.openStream());
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedReader in=new BufferedReader(urlStream);
String str;
try {
while((str=in.readLine())!=null){
sb.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
urlStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
转载于:http://www.eoeandroid.com/thread-164769-1-1.html