版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android開(kāi)發(fā)中文件操作工具類FileUtil怎么用
package
com.ymerp.android.tools;
import
java.io.BufferedReader;
import
java.io.ByteArrayOutputStream;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.FileReader;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.LineNumberReader;
import
java.io.OutputStream;
import
java.io.Reader;
import
java.text.DecimalFormat;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.Bitmap.Config;
import
android.graphics.BitmapFactory;
import
android.os.Environment;
import
android.util.Log;
import
android.widget.Toast;
/**
*
文件操作工具
*
*
@author
chen.lin
*
*/
public
class
FileUtil
{
private
static
final
String
TAG
=
"FileUtil";
/**
*
從sd卡取文件
*
*
@param
filename
*
@return
*/
public
String
getFileFromSdcard(String
filename)
{
ByteArrayOutputStream
outputStream
=
null;
FileInputStream
fis
=
null;
try
{
outputStream
=
new
ByteArrayOutputStream();
File
file
=
new
File(Environment.getExternalStorageDirectory(),
filename);
if
(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
fis
=
new
FileInputStream(file);
int
len
=
0;
byte[]
data
=
new
byte[1024];
while
((len
=
fis.read(data))
!=
-1)
{
outputStream.write(data,
0,
len);
}
}
}
catch
(Exception
e)
{
e.printStackTrace();
}
finally
{
try
{
outputStream.close();
fis.close();
}
catch
(IOException
e)
{
}
}
return
new
String(outputStream.toByteArray());
}
/**
*
保存文件到sd
*
*
@param
filename
*
@param
content
*
@return
*/
public
static
boolean
saveContentToSdcard(String
filename,
String
content)
{
boolean
flag
=
false;
FileOutputStream
fos
=
null;
try
{
File
file
=
new
File(Environment.getExternalStorageDirectory(),
filename);
if
(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
fos
=
new
FileOutputStream(file);
fos.write(content.getBytes());
flag
=
true;
}
}
catch
(Exception
e)
{
e.printStackTrace();
flag
=
false;
}
finally
{
try
{
fos.close();
}
catch
(IOException
e)
{
}
}
return
flag;
}
/**
*
取得文件大小
*
*
@param
f
*
@return
*
@throws
Exception
*/
@SuppressWarnings("resource")
public
static
long
getFileSizes(File
f)
throws
Exception
{
long
size
=
0;
if
(f.exists())
{
FileInputStream
fis
=
null;
fis
=
new
FileInputStream(f);
size
=
fis.available();
}
else
{
f.createNewFile();
}
return
size;
}
/**
*
遞歸取得文件夾大小
*
*
@param
dir
*
@return
*
@throws
Exception
*/
public
static
long
getFileSize(File
dir)
throws
Exception
{
long
size
=
0;
File
flist[]
=
dir.listFiles();
for
(int
i
=
0;
i
<
flist.length;
i++)
{
if
(flist[i].isDirectory())
{
size
=
size
+
getFileSize(flist[i]);
}
else
{
size
=
size
+
flist[i].length();
}
}
return
size;
}
/**
*
轉(zhuǎn)換文件大小
*
*
@param
fileS
*
@return
*/
public
static
String
FormetFileSize(long
fileS)
{
DecimalFormat
df
=
new
DecimalFormat("#.00");
String
fileSizeString
=
"";
if
(fileS
<
1024)
{
fileSizeString
=
df.format((double)
fileS)
+
"B";
}
else
if
(fileS
<
1048576)
{
fileSizeString
=
df.format((double)
fileS
/
1024)
+
"K";
}
else
if
(fileS
<
1073741824)
{
fileSizeString
=
df.format((double)
fileS
/
1048576)
+
"M";
}
else
{
fileSizeString
=
df.format((double)
fileS
/
1073741824)
+
"G";
}
return
fileSizeString;
}
/**
*
遞歸求取目錄文件個(gè)數(shù)
*
*
@param
f
*
@return
*/
public
static
long
getlist(File
f)
{
long
size
=
0;
File
flist[]
=
f.listFiles();
size
=
flist.length;
for
(int
i
=
0;
i
<
flist.length;
i++)
{
if
(flist[i].isDirectory())
{
size
=
size
+
getlist(flist[i]);
size--;
}
}
return
size;
}
/**
*
在根目錄下搜索文件
*
*
@param
keyword
*
@return
*/
public
static
String
searchFile(String
keyword)
{
String
result
=
"";
File[]
files
=
new
File("/").listFiles();
for
(File
file
:
files)
{
if
(file.getName().indexOf(keyword)
>=
0)
{
result
+=
file.getPath()
+
"\n";
}
}
if
(result.equals(""))
{
result
=
"找不到文件!!";
}
return
result;
}
/**
*
@detail
搜索sdcard文件
*
@param
需要進(jìn)行文件搜索的目錄
*
@param
過(guò)濾搜索文件類型
*
*/
public
static
List<String>
search(File
file,
String[]
ext)
{
List<String>
list
=
new
ArrayList<String>();
if
(file
!=
null)
{
if
(file.isDirectory())
{
File[]
listFile
=
file.listFiles();
if
(listFile
!=
null)
{
for
(int
i
=
0;
i
<
listFile.length;
i++)
{
search(listFile[i],
ext);
}
}
}
else
{
String
filename
=
file.getAbsolutePath();
for
(int
i
=
0;
i
<
ext.length;
i++)
{
if
(filename.endsWith(ext[i]))
{
list.add(filename);
break;
}
}
}
}
return
list;
}
/**
*
查詢文件
*
*
@param
file
*
@param
keyword
*
@return
*/
public
static
List<File>
FindFile(File
file,
String
keyword)
{
List<File>
list
=
new
ArrayList<File>();
if
(file.isDirectory())
{
File[]
files
=
file.listFiles();
if
(files
!=
null)
{
for
(File
tempf
:
files)
{
if
(tempf.isDirectory())
{
if
(tempf.getName().toLowerCase().lastIndexOf(keyword)
>
-1)
{
list.add(tempf);
}
list.addAll(FindFile(tempf,
keyword));
}
else
{
if
(tempf.getName().toLowerCase().lastIndexOf(keyword)
>
-1)
{
list.add(tempf);
}
}
}
}
}
return
list;
}
/**
*
searchFile
查找文件并加入到ArrayList
當(dāng)中去
*
*
@param
context
*
@param
keyword
*
@param
filepath
*
@return
*/
public
static
List<Map<String,
Object>>
searchFile(Context
context,
String
keyword,
File
filepath)
{
List<Map<String,
Object>>
list
=
new
ArrayList<Map<String,
Object>>();
Map<String,
Object>
rowItem
=
null;
int
index
=
0;
//
判斷SD卡是否存在
if
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File[]
files
=
filepath.listFiles();
if
(files.length
>
0)
{
for
(File
file
:
files)
{
if
(file.isDirectory())
{
if
(file.getName().toLowerCase().lastIndexOf(keyword)
>
-1)
{
rowItem
=
new
HashMap<String,
Object>();
rowItem.put("number",
index);
//
加入序列號(hào)
rowItem.put("fileName",
file.getName());//
加入名稱
rowItem.put("path",
file.getPath());
//
加入路徑
rowItem.put("size",
file.length()
+
"");
//
加入文件大小
list.add(rowItem);
}
//
如果目錄可讀就執(zhí)行(一定要加,不然會(huì)掛掉)
if
(file.canRead())
{
list.addAll(searchFile(context,
keyword,
file));
//
如果是目錄,遞歸查找
}
}
else
{
//
判斷是文件,則進(jìn)行文件名判斷
try
{
if
(file.getName().indexOf(keyword)
>
-1
||
file.getName().indexOf(keyword.toUpperCase())
>
-1)
{
rowItem
=
new
HashMap<String,
Object>();
rowItem.put("number",
index);
//
加入序列號(hào)
rowItem.put("fileName",
file.getName());//
加入名稱
rowItem.put("path",
file.getPath());
//
加入路徑
rowItem.put("size",
file.length()
+
"");
//
加入文件大小
list.add(rowItem);
index++;
}
}
catch
(Exception
e)
{
Toast.makeText(context,
"查找發(fā)生錯(cuò)誤!",
Toast.LENGTH_SHORT).show();
}
}
}
}
}
return
list;
}
/**
*
根據(jù)后綴得到文件類型
*
*
@param
fileName
*
@param
pointIndex
*
@return
*/
public
static
String
getFileType(String
fileName,
int
pointIndex)
{
String
type
=
fileName.substring(pointIndex
+
1).toLowerCase();
if
("m4a".equalsIgnoreCase(type)
||
"xmf".equalsIgnoreCase(type)
||
"ogg".equalsIgnoreCase(type)
||
"wav".equalsIgnoreCase(type)
||
"m4a".equalsIgnoreCase(type)
||
"aiff".equalsIgnoreCase(type)
||
"midi".equalsIgnoreCase(type)
||
"vqf".equalsIgnoreCase(type)
||
"aac".equalsIgnoreCase(type)
||
"flac".equalsIgnoreCase(type)
||
"tak".equalsIgnoreCase(type)
||
"wv".equalsIgnoreCase(type))
{
type
=
"ic_file_audio";
}
else
if
("mp3".equalsIgnoreCase(type)
||
"mid".equalsIgnoreCase(type))
{
type
=
"ic_file_mp3";
}
else
if
("avi".equalsIgnoreCase(type)
||
"mp4".equalsIgnoreCase(type)
||
"dvd".equalsIgnoreCase(type)
||
"mid".equalsIgnoreCase(type)
||
"mov".equalsIgnoreCase(type)
||
"mkv".equalsIgnoreCase(type)
||
"mp2v".equalsIgnoreCase(type)
||
"mpe".equalsIgnoreCase(type)
||
"mpeg".equalsIgnoreCase(type)
||
"mpg".equalsIgnoreCase(type)
||
"asx".equalsIgnoreCase(type)
||
"asf".equalsIgnoreCase(type)
||
"flv".equalsIgnoreCase(type)
||
"navi".equalsIgnoreCase(type)
||
"divx".equalsIgnoreCase(type)
||
"rm".equalsIgnoreCase(type)
||
"rmvb".equalsIgnoreCase(type)
||
"dat".equalsIgnoreCase(type)
||
"mpa".equalsIgnoreCase(type)
||
"vob".equalsIgnoreCase(type)
||
"3gp".equalsIgnoreCase(type)
||
"swf".equalsIgnoreCase(type)
||
"wmv".equalsIgnoreCase(type))
{
type
=
"ic_file_video";
}
else
if
("bmp".equalsIgnoreCase(type)
||
"pcx".equalsIgnoreCase(type)
||
"tiff".equalsIgnoreCase(type)
||
"gif".equalsIgnoreCase(type)
||
"jpeg".equalsIgnoreCase(type)
||
"tga".equalsIgnoreCase(type)
||
"exif".equalsIgnoreCase(type)
||
"fpx".equalsIgnoreCase(type)
||
"psd".equalsIgnoreCase(type)
||
"cdr".equalsIgnoreCase(type)
||
"raw".equalsIgnoreCase(type)
||
"eps".equalsIgnoreCase(type)
||
"gif".equalsIgnoreCase(type)
||
"jpg".equalsIgnoreCase(type)
||
"jpeg".equalsIgnoreCase(type)
||
"png".equalsIgnoreCase(type)
||
"hdri".equalsIgnoreCase(type)
||
"ai".equalsIgnoreCase(type))
{
type
=
"ic_file_image";
}
else
if
("ppt".equalsIgnoreCase(type)
||
"doc".equalsIgnoreCase(type)
||
"xls".equalsIgnoreCase(type)
||
"pps".equalsIgnoreCase(type)
||
"xlsx".equalsIgnoreCase(type)
||
"xlsm".equalsIgnoreCase(type)
||
"pptx".equalsIgnoreCase(type)
||
"pptm".equalsIgnoreCase(type)
||
"ppsx".equalsIgnoreCase(type)
||
"maw".equalsIgnoreCase(type)
||
"mdb".equalsIgnoreCase(type)
||
"pot".equalsIgnoreCase(type)
||
"msg".equalsIgnoreCase(type)
||
"oft".equalsIgnoreCase(type)
||
"xlw".equalsIgnoreCase(type)
||
"wps".equalsIgnoreCase(type)
||
"rtf".equalsIgnoreCase(type)
||
"ppsm".equalsIgnoreCase(type)
||
"potx".equalsIgnoreCase(type)
||
"potm".equalsIgnoreCase(type)
||
"ppam".equalsIgnoreCase(type))
{
type
=
"ic_file_office";
}
else
if
("txt".equalsIgnoreCase(type)
||
"text".equalsIgnoreCase(type)
||
"chm".equalsIgnoreCase(type)
||
"hlp".equalsIgnoreCase(type)
||
"pdf".equalsIgnoreCase(type)
||
"doc".equalsIgnoreCase(type)
||
"docx".equalsIgnoreCase(type)
||
"docm".equalsIgnoreCase(type)
||
"dotx".equalsIgnoreCase(type))
{
type
=
"ic_file_text";
}
else
if
("ini".equalsIgnoreCase(type)
||
"sys".equalsIgnoreCase(type)
||
"dll".equalsIgnoreCase(type)
||
"adt".equalsIgnoreCase(type))
{
type
=
"ic_file_system";
}
else
if
("rar".equalsIgnoreCase(type)
||
"zip".equalsIgnoreCase(type)
||
"arj".equalsIgnoreCase(type)
||
"gz".equalsIgnoreCase(type)
||
"z".equalsIgnoreCase(type)
||
"7Z".equalsIgnoreCase(type)
||
"GZ".equalsIgnoreCase(type)
||
"BZ".equalsIgnoreCase(type)
||
"ZPAQ".equalsIgnoreCase(type))
{
type
=
"ic_file_rar";
}
else
if
("html".equalsIgnoreCase(type)
||
"htm".equalsIgnoreCase(type)
||
"java".equalsIgnoreCase(type)
||
"php".equalsIgnoreCase(type)
||
"asp".equalsIgnoreCase(type)
||
"aspx".equalsIgnoreCase(type)
||
"jsp".equalsIgnoreCase(type)
||
"shtml".equalsIgnoreCase(type)
||
"xml".equalsIgnoreCase(type))
{
type
=
"ic_file_web";
}
else
if
("exe".equalsIgnoreCase(type)
||
"com".equalsIgnoreCase(type)
||
"bat".equalsIgnoreCase(type)
||
"iso".equalsIgnoreCase(type)
||
"msi".equalsIgnoreCase(type))
{
type
=
"ic_file_exe";
}
else
if
("apk".equalsIgnoreCase(type))
{
type
=
"ic_file_apk";
}
else
{
type
=
"ic_file_normal";
}
return
type;
}
/**
*
改變文件大小顯示的內(nèi)容
*
*
@param
size
*
@return
*/
public
static
String
changeFileSize(String
size)
{
if
(Integer.parseInt(size)
>
1024)
{
size
=
Integer.parseInt(size)
/
1024
+
"K";
}
else
if
(Integer.parseInt(size)
>
(1024
*
1024))
{
size
=
Integer.parseInt(size)
/
(1024
*
1024)
+
"M";
}
else
if
(Integer.parseInt(size)
>
(1024
*
1024
*
1024))
{
size
=
Integer.parseInt(size)
/
(1024
*
1024
*
1024)
+
"G";
}
else
{
size
+=
"B";
}
return
size;
}
/**
*
得到所有文件
*
*
@param
dir
*
@return
*/
public
static
ArrayList<File>
getAllFiles(File
dir)
{
ArrayList<File>
allFiles
=
new
ArrayList<File>();
//
遞歸取得目錄下的所有文件及文件夾
File[]
files
=
dir.listFiles();
for
(int
i
=
0;
i
<
files.length;
i++)
{
File
file
=
files[i];
allFiles.add(file);
if
(file.isDirectory())
{
getAllFiles(file);
}
}
Logger.i("test",
allFiles.size()
+
"");
return
allFiles;
}
/**
*
判斷文件MimeType
類型
*
*
@param
f
*
@return
*/
public
static
String
getMIMEType(File
f)
{
String
type
=
"";
String
fName
=
f.getName();
/*
取得擴(kuò)展名
*/
String
end
=
fName.substring(fName.lastIndexOf(".")
+
1,
fName.length()).toLowerCase();
/*
依擴(kuò)展名的類型決定MimeType
*/
if
(end.equalsIgnoreCase("m4a")
||
end.equalsIgnoreCase("mp3")
||
end.equalsIgnoreCase("mid")
||
end.equalsIgnoreCase("xmf")
||
end.equalsIgnoreCase("ogg")
||
end.equalsIgnoreCase("wav"))
{
type
=
"audio";
}
else
if
(end.equalsIgnoreCase("3gp")
||
end.equalsIgnoreCase("mp4"))
{
type
=
"video";
}
else
if
(end.equalsIgnoreCase("jpg")
||
end.equalsIgnoreCase("gif")
||
end.equalsIgnoreCase("png")
||
end.equalsIgnoreCase("jpeg")
||
end.equalsIgnoreCase("bmp"))
{
type
=
"image";
}
else
if
(end.equalsIgnoreCase("apk"))
{
/*
android.permission.INSTALL_PACKAGES
*/
type
=
"application/vnd.android.package-archive";
}
else
if
(end.equalsIgnoreCase("txt")
||
end.equalsIgnoreCase("java"))
{
/*
android.permission.INSTALL_PACKAGES
*/
type
=
"text";
}
else
{
type
=
"*";
}
/*
如果無(wú)法直接打開(kāi),就跳出軟件列表給用戶選擇
*/
if
(end.equalsIgnoreCase("apk"))
{
}
else
{
type
+=
"/*";
}
return
type;
}
/**
*
拷貝文件
*
*
@param
fromFile
*
@param
toFile
*
@throws
IOException
*/
public
static
void
copyFile(File
fromFile,
String
toFile)
throws
IOException
{
FileInputStream
from
=
null;
FileOutputStream
to
=
null;
try
{
from
=
new
FileInputStream(fromFile);
to
=
new
FileOutputStream(toFile);
byte[]
buffer
=
new
byte[1024];
int
bytesRead;
while
((bytesRead
=
from.read(buffer))
!=
-1)
to.write(buffer,
0,
bytesRead);
//
write
}
finally
{
if
(from
!=
null)
try
{
from.close();
}
catch
(IOException
e)
{
Log.e(TAG,
"",
e);
}
if
(to
!=
null)
try
{
to.close();
}
catch
(IOException
e)
{
Log.e(TAG,
"",
e);
}
}
}
/**
*
創(chuàng)建文件
*
*
@param
file
*
@return
*/
public
static
File
createNewFile(File
file)
{
try
{
if
(file.exists())
{
return
file;
}
File
dir
=
file.getParentFile();
if
(!dir.exists())
{
dir.mkdirs();
}
if
(!file.exists())
{
file.createNewFile();
}
}
catch
(IOException
e)
{
Log.e(TAG,
"",
e);
return
null;
}
return
file;
}
/**
*
創(chuàng)建文件
*
*
@param
path
*/
public
static
File
createNewFile(String
path)
{
File
file
=
new
File(path);
return
createNewFile(file);
}//
end
method
createText()
/**
*
刪除文件
*
*
@param
path
*/
public
static
void
deleteFile(String
path)
{
File
file
=
new
File(path);
deleteFile(file);
}
/**
*
刪除文件
*
*
@param
file
*/
public
static
void
deleteFile(File
file)
{
if
(!file.exists())
{
return;
}
if
(file.isFile())
{
file.delete();
}
else
if
(file.isDirectory())
{
File
files[]
=
file.listFiles();
for
(int
i
=
0;
i
<
files.length;
i++)
{
deleteFile(files[i]);
}
}
file.delete();
}
/**
*
向Text文件中寫入內(nèi)容
*
*
@param
file
*
@param
content
*
@return
*/
public
static
boolean
write(String
path,
String
content)
{
return
write(path,
content,
false);
}
public
static
boolean
write(String
path,
String
content,
boolean
append)
{
return
write(new
File(path),
content,
append);
}
public
static
boolean
write(File
file,
String
content)
{
return
write(file,
content,
false);
}
/**
*
寫入文件
*
*
@param
file
*
@param
content
*
@param
append
*
@return
*/
public
static
boolean
write(File
file,
String
content,
boolean
append)
{
if
(file
==
null
||
StringUtil.empty(content))
{
return
false;
}
if
(!file.exists())
{
file
=
createNewFile(file);
}
FileOutputStream
fos
=
null;
try
{
fos
=
new
FileOutputStream(file,
append);
fos.write(content.getBytes());
}
catch
(Exception
e)
{
Log.e(TAG,
"",
e);
return
false;
}
finally
{
try
{
fos.close();
}
catch
(IOException
e)
{
Log.e(TAG,
"",
e);
}
fos
=
null;
}
return
true;
}
/**
*
獲得文件名
*
*
@param
path
*
@return
*/
public
static
String
getFileName(String
path)
{
if
(StringUtil.empty(path))
{
return
null;
}
File
f
=
new
File(path);
String
name
=
f.getName();
f
=
null;
return
name;
}
/**
*
讀取文件內(nèi)容,從第startLine行開(kāi)始,讀取lineCount行
*
*
@param
file
*
@param
startLine
*
@param
lineCount
*
@return
讀到文字的list,如果list.size<lineCount則說(shuō)明讀到文件末尾了
*/
public
static
List<String>
readFile(File
file,
int
startLine,
int
lineCount)
{
if
(file
==
null
||
startLine
<
1
||
lineCount
<
1)
{
return
null;
}
if
(!file.exists())
{
return
null;
}
FileReader
fileReader
=
null;
List<String>
list
=
null;
try
{
list
=
new
ArrayList<String>();
fileReader
=
new
FileReader(file);
LineNumberReader
lineReader
=
new
LineNumberReader(fileReader);
boolean
end
=
false;
for
(int
i
=
1;
i
<
startLine;
i++)
{
if
(lineReader.readLine()
==
null)
{
end
=
true;
break;
}
}
if
(end
==
false)
{
for
(int
i
=
startLine;
i
<
startLine
+
lineCount;
i++)
{
String
line
=
lineReader.readLine();
if
(line
==
null)
{
break;
}
list.add(line);
}
}
}
catch
(Exception
e)
{
Log.e(TAG,
"read
log
error!",
e);
}
finally
{
if
(fileReader
!=
null)
{
try
{
fileReader.close();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
}
return
list;
}
/**
*
創(chuàng)建文件夾
*
*
@param
dir
*
@return
*/
public
static
boolean
createDir(File
dir)
{
try
{
if
(!dir.exists())
{
dir.mkdirs();
}
return
true;
}
catch
(Exception
e)
{
Log.e(TAG,
"create
dir
error",
e);
return
false;
}
}
/**
*
在SD卡上創(chuàng)建目錄
*
*
@param
dirName
*/
public
static
File
creatSDDir(String
dirName)
{
File
dir
=
new
File(dirName);
dir.mkdir();
return
dir;
}
/**
*
判斷SD卡上的文件是否存在
*/
public
static
boolean
isFileExist(String
fileName)
{
File
file
=
new
File(fileName);
return
file.exists();
}
/**
*
將一個(gè)InputStream里面的數(shù)據(jù)寫入到SD卡中
*/
public
static
File
write2SDFromInput(String
path,
String
fileName,
InputStream
input)
{
File
file
=
null;
OutputStream
output
=
null;
try
{
creatSDDir(path);
file
=
createNewFile(path
+
"/"
+
fileName);
output
=
new
FileOutputStream(file);
byte
buffer[]
=
new
byte[1024];
int
len
=
-1;
while
((len
=
input.read(buffer))
!=
-1)
{
output.write(buffer,
0,
len);
}
output.flush();
}
catch
(Exception
e)
{
e.printStackTrace();
}
finally
{
try
{
output.close();
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
return
file;
}
/**
*
讀取文件內(nèi)容
從文件中一行一行的讀取文件
*
*
@param
file
*
@return
*/
public
static
String
readFile(File
file)
{
Reader
read
=
null;
String
content
=
"";
String
result
=
"";
BufferedReader
br
=
null;
try
{
read
=
new
FileReader(file);
br
=
new
BufferedReader(read);
while
((content
=
br.readLine().toString().trim())
!=
null)
{
result
+=
content
+
"\r\n";
}
}
catch
(Exception
e)
{
e.printStackTrace();
}
finally
{
try
{
read.close();
br.close();
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
return
result;
}
/**
*
將圖片保存到本地時(shí)進(jìn)行壓縮,
即將圖片從Bitmap形式變?yōu)镕ile形式時(shí)進(jìn)行壓縮,
*
特點(diǎn)是:
File形式的圖片確實(shí)被壓縮了,
但是當(dāng)你重新讀取壓縮后的file為
Bitmap是,它占用的內(nèi)存并沒(méi)有改變
*
*
@param
bmp
*
@param
file
*/
public
static
void
compressBmpToFile(Bitmap
bmp,
File
file)
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream();
int
options
=
80;//
個(gè)人喜歡從80開(kāi)始,
press(Bitmap.CompressFormat.JPEG,
options,
baos);
while
(baos.toByteArray().length
/
1024
>
100)
{
baos.reset();
options
-=
10;
press(Bitmap.CompressFormat.JPEG,
options,
baos);
}
try
{
FileOutputStream
fos
=
new
FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
f
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 長(zhǎng)沙航空職業(yè)技術(shù)學(xué)院《醫(yī)學(xué)細(xì)胞生物學(xué)B》2023-2024學(xué)年第一學(xué)期期末試卷
- 2024年船舶信息安全管理系統(tǒng)合同
- 2025版二手挖掘機(jī)轉(zhuǎn)讓合同范本3篇
- 逾期債款行業(yè)客服工作總結(jié)
- 2024年餐飲企業(yè)廚師勞動(dòng)合同3篇
- 生物醫(yī)療商務(wù)交流技巧模板
- 2024房屋認(rèn)購(gòu)合同
- 娛樂(lè)休閑銷售人員工作總結(jié)
- 2025年度石材行業(yè)綠色物流與運(yùn)輸服務(wù)合同3篇
- 行業(yè)競(jìng)爭(zhēng)分析與市場(chǎng)定位計(jì)劃
- (八省聯(lián)考)2025年高考綜合改革適應(yīng)性演練 物理試卷合集(含答案逐題解析)
- 2025年安徽銅陵市公安局第二批輔警招聘158人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 車間修繕合同模板
- 商會(huì)年會(huì)策劃方案范例(3篇)
- SQE年終總結(jié)報(bào)告
- 《化學(xué)實(shí)驗(yàn)室安全》課程教學(xué)大綱
- 2024年人教版初二地理上冊(cè)期末考試卷(附答案)
- 2024文旅景區(qū)秋季稻田豐收節(jié)稻花香里 說(shuō)豐年主題活動(dòng)策劃方案
- 高低壓供配電設(shè)備檢查和檢修保養(yǎng)合同3篇
- 2023-2024學(xué)年福建省廈門市八年級(jí)(上)期末物理試卷
- (正式版)JBT 9229-2024 剪叉式升降工作平臺(tái)
評(píng)論
0/150
提交評(píng)論