java 文件操作

[color=red][b]android 文件工具[/b][/color]

package com.example.asyncimageloader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;

public class FileUtils {
/**
* sd卡的根目录
*/
private static String mSdRootPath = Environment.getExternalStorageDirectory().getPath();
/**
* 手机的缓存根目录
*/
private static String mDataRootPath = null;
/**
* 保存Image的目录名
*/
private final static String FOLDER_NAME = "/AndroidImage";


public FileUtils(Context context){
mDataRootPath = context.getCacheDir().getPath();
}


/**
* 获取储存Image的目录
* @return
*/
private String getStorageDirectory(){
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ?
mSdRootPath + FOLDER_NAME : mDataRootPath + FOLDER_NAME;
}

/**
* 保存Image的方法,有sd卡存储到sd卡,没有就存储到手机目录
* @param fileName
* @param bitmap
* @throws IOException
*/
public void savaBitmap(String fileName, Bitmap bitmap) throws IOException{
if(bitmap == null){
return;
}
String path = getStorageDirectory();
File folderFile = new File(path);
if(!folderFile.exists()){
folderFile.mkdir();
}
File file = new File(path + File.separator + fileName);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}

/**
* 从手机或者sd卡获取Bitmap
* @param fileName
* @return
*/
public Bitmap getBitmap(String fileName){
return BitmapFactory.decodeFile(getStorageDirectory() + File.separator + fileName);
}

/**
* 判断文件是否存在
* @param fileName
* @return
*/
public boolean isFileExists(String fileName){
return new File(getStorageDirectory() + File.separator + fileName).exists();
}

/**
* 获取文件的大小
* @param fileName
* @return
*/
public long getFileSize(String fileName) {
return new File(getStorageDirectory() + File.separator + fileName).length();
}


/**
* 删除SD卡或者手机的缓存图片和目录
*/
public void deleteFile() {
File dirFile = new File(getStorageDirectory());
if(! dirFile.exists()){
return;
}
if (dirFile.isDirectory()) {
String[] children = dirFile.list();
for (int i = 0; i < children.length; i++) {
new File(dirFile, children[i]).delete();
}
}

dirFile.delete();
}
}




1.拷贝文件可以用byte[]来实现,这种实现也比较简单。

[size=medium][color=red]下载网络文件[/color][/size]


/**
* @author:
* @TODO:下载文件
*/
private static boolean downloadFile(String urlStr,String dir,String fileName){
boolean resultFlag = false ;
URL url = null ;
InputStream input = null ;
OutputStream output = null ;
try {
url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(5*1000);
input = urlConnection.getInputStream();
File dirFile = new File(dir);
if(!dirFile.exists()){
dirFile.mkdirs();
}
File file = new File(dir+"\\"+fileName);
output = new FileOutputStream(file) ;
byte[] byt = new byte[1024];
int length = 0;
// 开始读取
while ((length = input.read(byt)) != -1) {
output.write(byt, 0, length);
}
input.close();
output.close();
resultFlag = true ;
System.out.println("下载完成!");
} catch (Exception e) {
e.printStackTrace();
}
return resultFlag ;
}

调用:downloadFile("http://s0.hao123img.com/res/img/logo/logo201509091.png","D:\\x","x.jpg");




InputStream fin = null ;
OutputStream output = null ;
byte[] byt = null ;
try {
//读取文件字节
fin = new FileInputStream(new File("d:\\123.txt"));
byt = new byte[fin.available()];
//把文件内容读到byte数组中
fin.read(byt);

//拷贝文件
File file = new File("d:\\c");
file.mkdirs() ;
File f2 = new File("d:\\c\\123.txt");
output = new FileOutputStream(f2);
//复制文件把byte数组转换成File文件
output.write(byt);

} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
catch (IOException e2) {
e2.printStackTrace();
}


2.创建文件夹File类
mkdir()
[color=red]只能在已经存在的目录中创建创建文件夹。[/color]
mkdirs()
[color=red]可以在不存在的目录中创建文件夹。诸如:a\\b,既可以创建多级目录。[/color]

所以在平时创建文件夹时多使用mkdirs()方法

3.文件夹下所有的文件


import java.io.File;
import java.util.ArrayList;

public class FileTest {

//用于存储文件夹下所有的文件名
private static ArrayList<String> filelist = new ArrayList<String>();

public static void main(String[] args) throws Exception {

String filePath = "E://Test";
getFiles(filePath);
}
/*
* 通过递归得到某一路径下所有的目录及其文件
*/
static void getFiles(String filePath){
File root = new File(filePath);
//得到所有文件
File[] files = root.listFiles();
for(File file:files){
if(file.isDirectory()){
/*
* 递归调用
*/
getFiles(file.getAbsolutePath());
filelist.add(file.getAbsolutePath());
System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());
}else{
System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());
}
}
}
}


4.删除文件


public static boolean deleteFile(String filePath){
boolean result = false;
File file = new File(filePath);
if(file.exists() && file.isFile()){
result = file.delete();
}

return result;
}


5.按行读取文件内容


fin = new FileInputStream(new File("d:\\c\\123.txt"));
//避免乱码所以用GBK
BufferedReader buff = new BufferedReader(new InputStreamReader(fin,
"GBK"));
String tempStr = "";
while ((tempStr = buff.readLine()) != null) {
System.out.println(tempStr);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值