android java文件_Android---Java文件存取

android需声明文件权限:

java二进制文件的存储:

try{

File file = new File(fileName);

if(!file.exists()) file.createNewFile();

FileOutputStream outputStream = new FileOutputStream(file);

outputStream.write("hello world");

outputStream.flush();

outputStream.close();

}catch (FileNotFoundException e){

e.printStackTrace();

}catch (IOException e){

e.printStackTrace();

}

注意:输入输出流用完后及时关闭。

java二进制文件的读取:

String fileName = "/sdcard/feature.db";

File file = new File(fileName);

if(!file.exists()) return;

try{

FileInputStream fis = new FileInputStream(file);

byte[] buffer = new byte[size];

while(fis.read(buffer) > 0){

Sysout(buffer.toString());

}

fis.close();

}catch (Exception e){

e.printStackTrace();

}

按行写入数据

file = new File(ind);

if (!file.exists()) file.createNewFile();

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));

out.println(filePath + "---" + faceimg.width + "---" + faceimg.height);

out.close();

从文件中按行读取数据:

File file = new File(path);

try {

FileInputStream instream = new FileInputStream(fileName);

if (instream != null)

{

InputStreamReader inputreader = new InputStreamReader(instream);

BufferedReader buffreader = new BufferedReader(inputreader);

String line;

//分行读取

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

///

}

buffreader.close();

inputreader.close();

instream.close();

//注意关闭的顺序,先打开后关闭

}

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

使用BufferedWriter写入文件,可以直接写字符串,数组,字符数据保存到文件中:

try {

String str = "hello";

File file = new File(FileName);

if (!file.exists()) {

file.createNewFile();

}

FileWriter fw = new FileWriter(file);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(str);

bw.close();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

将NV21格式图片转成jpg压缩并存储:

//face为NV21格式

public Bitmap pictureTrans(byte[] face, int width, int height){

if(face == null) return null;

YuvImage image = new YuvImage(face, ImageFormat.NV21, width, height, null);

Rect rect = new Rect(0, 0, width, height);

ByteArrayOutputStream os = new ByteArrayOutputStream(face.length);

image.compressToJpeg(rect, 80, os);

byte[] tmp = os.toByteArray();

Bitmap bmp = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);

return bmp;

}

---------------------------------- //文件操作

File file = new File("/sdcard/myFolder");

if(!file.exists())

file.mkdir();

String filePath = "/sdcard/myFolder/" + "hello.jpg";

file = new File(filePath);

try{

file.createNewFile();

FileOutputStream fos = new FileOutputStream(file);

bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);

fos.flush();

fos.close();

}catch (IOException e){

e.printStackTrace();

}

将图片读取出来

//android中直接使用BitmapFactory即可

public Bitmap readImage(String filePath){

Bitmap bitmap = BitmapFactory.decodeFile(filePath);

return bitmap;

}

获取文件的大小:

//文件大小小于2GB时可用,大文件需用其它方法

public long getFileSize(File file){

int size = 0;

if(file.exists()){

try{

FileInputStream fis = new FileInputStream(file);

size = fis.available();

fis.close();

}catch (Exception e){

e.printStackTrace();

}

}

return size;

}

判断sd是否存在

public boolean isSDCardExist() {

return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

}

获取sd卡根目录:

public String getSDCardPath() {

boolean exist = isSDCardExist();

String path = "";

if (exist) {

path = Environment.getExternalStorageDirectory().getAbsolutePath();

} else {

path = "error";

}

return path;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值