Android 文件读取

/**

*文件管理

*/

publicclassFileUtils{

privatestaticfinalStringTAG="FileUtils";

 

/**

*判断SD卡是否可用

*

*@returnSD卡可用返回true

*/

publicstaticbooleanhasSdcard(){

Stringstatus=Environment.getExternalStorageState();

returnEnvironment.MEDIA_MOUNTED.equals(status);

}

 

/**

*读取文件的内容

*<br>

*默认utf-8编码

*

*@paramfilePath文件路径

*@return字符串

*@throwsIOException

*/

publicstaticStringreadFile(StringfilePath)throwsIOException{

returnreadFile(filePath,"utf-8");

}

 

/**

*读取文件的内容

*

*@paramfilePath文件目录

*@paramcharsetName字符编码

*@returnString字符串

*/

publicstaticStringreadFile(StringfilePath,StringcharsetName)

throwsIOException{

if(TextUtils.isEmpty(filePath))

returnnull;

if(TextUtils.isEmpty(charsetName))

charsetName="utf-8";

Filefile=newFile(filePath);

StringBuilderfileContent=newStringBuilder("");

if(file==null||!file.isFile())

returnnull;

BufferedReaderreader=null;

try{

InputStreamReaderis=newInputStreamReader(newFileInputStream(

file),charsetName);

reader=newBufferedReader(is);

Stringline=null;

while((line=reader.readLine())!=null){

if(!fileContent.toString().equals("")){

fileContent.append("\r\n");

}

fileContent.append(line);

}

returnfileContent.toString();

}finally{

if(reader!=null){

try{

reader.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

 

/**

*读取文本文件到List字符串集合中(默认utf-8编码)

*

*@paramfilePath文件目录

*@return文件不存在返回null,否则返回字符串集合

*@throwsIOException

*/

publicstaticList<String>readFileToList(StringfilePath)

throwsIOException{

returnreadFileToList(filePath,"utf-8");

}

 

/**

*读取文本文件到List字符串集合中

*

*@paramfilePath文件目录

*@paramcharsetName字符编码

*@return文件不存在返回null,否则返回字符串集合

*/

publicstaticList<String>readFileToList(StringfilePath,

StringcharsetName)throwsIOException{

if(TextUtils.isEmpty(filePath))

returnnull;

if(TextUtils.isEmpty(charsetName))

charsetName="utf-8";

Filefile=newFile(filePath);

List<String>fileContent=newArrayList<>();

if(file==null||!file.isFile()){

returnnull;

}

BufferedReaderreader=null;

try{

InputStreamReaderis=newInputStreamReader(newFileInputStream(

file),charsetName);

reader=newBufferedReader(is);

Stringline=null;

while((line=reader.readLine())!=null){

fileContent.add(line);

}

returnfileContent;

}finally{

if(reader!=null){

try{

reader.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

 

 

/**

*向文件中写入数据

*

*@paramfilePath文件目录

*@paramcontent要写入的内容

*@paramappend如果为true,则将数据写入文件末尾处,而不是写入文件开始处

*@return写入成功返回true,写入失败返回false

*@throwsIOException

*/

publicstaticbooleanwriteFile(StringfilePath,Stringcontent,

booleanappend)throwsIOException{

if(TextUtils.isEmpty(filePath))

returnfalse;

if(TextUtils.isEmpty(content))

returnfalse;

FileWriterfileWriter=null;

try{

createFile(filePath);

fileWriter=newFileWriter(filePath,append);

fileWriter.write(content);

fileWriter.flush();

returntrue;

}finally{

if(fileWriter!=null){

try{

fileWriter.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

 

 

/**

*向文件中写入数据<br>

*默认在文件开始处重新写入数据

*

*@paramfilePath文件目录

*@paramstream字节输入流

*@return写入成功返回true,否则返回false

*@throwsIOException

*/

publicstaticbooleanwriteFile(StringfilePath,InputStreamstream)

throwsIOException{

returnwriteFile(filePath,stream,false);

}

 

/**

*向文件中写入数据

*

*@paramfilePath文件目录

*@paramstream字节输入流

*@paramappend如果为true,则将数据写入文件末尾处;

*false时,清空原来的数据,从头开始写

*@return写入成功返回true,否则返回false

*@throwsIOException

*/

publicstaticbooleanwriteFile(StringfilePath,InputStreamstream,

booleanappend)throwsIOException{

if(TextUtils.isEmpty(filePath))

thrownewNullPointerException("filePathisEmpty");

if(stream==null)

thrownewNullPointerException("InputStreamisnull");

returnwriteFile(newFile(filePath),stream,

append);

}

 

/**

*向文件中写入数据

*默认在文件开始处重新写入数据

*

*@paramfile指定文件

*@paramstream字节输入流

*@return写入成功返回true,否则返回false

*@throwsIOException

*/

publicstaticbooleanwriteFile(Filefile,InputStreamstream)

throwsIOException{

returnwriteFile(file,stream,false);

}

 

/**

*向文件中写入数据

*

*@paramfile指定文件

*@paramstream字节输入流

*@paramappendtrue时,在文件开始处重新写入数据;

*false时,清空原来的数据,从头开始写

*@return写入成功返回true,否则返回false

*@throwsIOException

*/

publicstaticbooleanwriteFile(Filefile,InputStreamstream,

booleanappend)throwsIOException{

if(file==null)

thrownewNullPointerException("file=null");

OutputStreamout=null;

try{

createFile(file.getAbsolutePath());

out=newFileOutputStream(file,append);

bytedata[]=newbyte[1024];

intlength=-1;

while((length=stream.read(data))!=-1){

out.write(data,0,length);

}

out.flush();

returntrue;

}finally{

if(out!=null){

try{

out.close();

stream.close();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

}

 

 

/**

*创建文件

*@paramabsolutePath

*/

privatestaticvoidcreateFile(StringabsolutePath){

Filefile=null;

try{

file=newFile(absolutePath);

if(!file.exists()){

file.createNewFile();

}

}catch(IOExceptione){

e.printStackTrace();

Log.e(TAG,"文件创建失败:"+e.toString());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值