java IO应用

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

/**
* @author 刘毅
* @date 2010-2-25
* @ClassName FileUtils.java
* @Email liu_yi126@163.com
* @param 文件操作
* @param
*/
public class FileUtils {
/**
* 复制文件,无乱码
* @param srcPath 原文件路径
* @param destPath 目的文件路径
*/
public static void copyForFile(String srcPath,String destPath){
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
br =new BufferedInputStream(new FileInputStream(srcPath));
bw = new BufferedOutputStream(new FileOutputStream(destPath));
byte[] b = new byte[1024];
int size = -1;
//循环读
while ((size = br.read(b)) != -1) {
bw.write(b, 0, size);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
if(bw != null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读文件,无乱码
* @param srcFile 原文件
*/
public static void readerForFile(String srcFile){
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile),"gbk"));
//一行一行的读
String Line;
while((Line = br.readLine()) != null){
System.out.println(Line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取url,无乱码
* @param urlStr url,需加http://
*/
public static void readerForURLToFile(String urlStr){
BufferedReader br = null;
StringBuffer sb = null;
try {
URL url = new URL(urlStr);
br = new BufferedReader(new InputStreamReader(url.openStream(),"gbk"));
sb = new StringBuffer();
String str = null;
while((str = br.readLine())!= null){
sb.append(str).append("\r\n");
}
System.out.println(sb);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 写文件,追加模式,无乱码
* @param srcFile 原文件
*/
public static void writerForFileAddModel(String srcFile){
FileWriter fw = null;
try {
fw = new FileWriter(srcFile,true);
fw.write("\r\n");
fw.write("sjkd39840823094你好啊啊啊啊啊啊 ");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 写文件,无乱码,以数组形式写入
* @param srcFile 原文件
* @param arg0
*/
public static void writerForFile(String srcFile,String []arg0){
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(srcFile));
for (int i = 0; i < arg0.length; i++) {
pw.write(arg0[i]);
pw.println();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(pw != null){
pw.close();
}

}
}
/**
* 获取所有文件夹下所有文件名字
* @param listFile 文件
*/
public static void getALLFileNameForFolder(String listFile){
File file = new File(listFile);
System.out.println("是目录吗?" + file.isDirectory());
System.out.println("是文件吗?" + file.isFile());
System.out.println("最后修改时间:" + new Date(file.lastModified()));
System.out.println("路径:" + file.getPath());
System.out.println("绝对路径:" + file.getAbsolutePath());
System.out.println("文件大小:" + file.length() + "字节");
System.out.println("是否可写:" + file.canWrite());
File[] array = file.listFiles();
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
System.out.println("文件名:" + array[i].getName()+
"文件路径:"+array[i].getParent()+
"文件全路径:"+array[i].getPath());
}
}
}
/**
* 根据名字删除文件
* @param fileName 文件名路径
* @return
*/
public static boolean deleteForFileName(String fileDir){
boolean flag = false;
File file = new File(fileDir);
if(file.isFile() && file.exists()){
file.delete();
flag = true;
}
return flag;
}
/**
* 重命名文件
* @param newFileName 文件名
*/
public static void renameForFileName(String newFileName){
File afile = new File(newFileName);
File bfile = new File("c:/1.txt");
afile.renameTo(bfile);
}
/**
* 删除文件夹,包括子文件夹,文件.包括隐藏文件(效率????)
* @param fileDir 文件夹路径
*/
public static void deleteForALLFile(File fileDir){
if(fileDir.exists()){ //是否存在
if(fileDir.isDirectory()){//是否是目录
File files[] = fileDir.listFiles();
for(int i=0;i<files.length;i++){
deleteForALLFile(files[i]);
}
}else{
fileDir.delete();
}
}else{
System.out.println("不存在目录:"+fileDir.getPath());
}
}
/**
* 根据后缀名删除文件 ?
* @param file 文件名
*/
public static void deleteForFileSuffix(String fileDir){
// 删除单一文件
String aFile = fileDir;
//根据字符返回最后一处出现的索引
int suffixSymbol = aFile.lastIndexOf(".");
String suffix =aFile.substring(suffixSymbol+1);
System.out.println(suffix);
File file = new File(aFile);
if(suffix.equalsIgnoreCase("txt")){
file.delete();
}
}
/**
* 计算文件个数
* @param fileDir 文件
*/
public static void numForFile(String fileDir){
File file = new File(fileDir);
File []arrayFile =file.listFiles();
System.out.println(arrayFile.length);
}

public static void main(String ...args){
//copyForFile("c:\\你.txt","d:\\好.txt");
//readerForFile("c:\\你.txt");
writerForFileAddModel("c:\\你.txt");
/*
String str[] = new String[3];
str[0]="你=好";
str[1]="0=1";
str[2]="0=1";
writerForFile("c:\\你.txt",str);
*/
//deleteForFileName("src/test.properties");
//getALLFileNameForFolder("C:/Youku/db");
// File file= new File("c://1");
// deleteForALLFile(file);
//deleteForFileSuffix("F:/1");
//numForFile("F:/1");
//renameForFileName("c:/你.txt");
// readerForURLToFile("http://www.qq.com");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值