/**
*文件管理
*/
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字节输入流
*@paramappend为true时,在文件开始处重新写入数据;
*为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());
}
}
}