Java文件处理类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

包含一些对文件的基本操作。

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class fileTest {

static int countFiles=0;
static int countFolders=0;

public static void  createFile(String filepath)
{
File file=new File(filepath);
if(file.exists())
{
System.out.println("目标文件已经存在!");
return;
}
if(filepath.endsWith(File.separator))
{
System.out.println("目标文件不能为目录!");
return;
}
try {
if(file.createNewFile())
System.out.println("文件创建成功!");
else
{
System.out.println("文件创建失败!");
return;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void createDir(String dirname)
{
if(!dirname.endsWith(File.separator))
dirname=dirname+File.separator;
File dir=new File(dirname);
if(dir.exists())
{
System.out.println("目录已存在!");
return;
}
if(dir.mkdirs())
System.out.println("目录创建成功!");
else
{
System.out.println("目录创建失败!");
return;
}
}

public static boolean checkDirExist(String dirname)
{
File file=new File(dirname);
if(file.exists())
{
if(file.isDirectory())
return true;
}
return false;
}

public static File[] getDirFiles(String dirname)
{
File[] result=null;
File fileDir = new File(dirname);
if (!fileDir.exists()) 
{
System.out.println("文件夹不存在");
return null;
}
else
{
if (!fileDir.isDirectory()) 
{
fileDir = new File(fileDir.getParent());
}
result = fileDir.listFiles();
}
return result;
}

public static void deleteFiles(String filename)
{
File file=new File(filename);
if(!file.exists())
{
System.out.println("文件不存在!");
return;
}
if(file.delete())
{
System.out.println(filename+"文件删除成功!");
}
}

public static void deleteDirs(String dirname)
{
if(!dirname.endsWith(File.separator))
dirname=dirname+File.separator;
File dirn=new File(dirname);
if(!dirn.exists())
{
System.out.println("文件目录不存在!");
return;
}
File[] files=dirn.listFiles();
for(int i=0;i<files.length;i++)
{
if(files[i].isFile())
{
deleteFiles(files[i].getAbsolutePath());
}
else
deleteDirs(files[i].getAbsolutePath());
}
if(dirn.delete())
{
System.out.println("目录删除成功!");
}
}

public static void searchFiles(String dirname,String filename)
{
if(!dirname.endsWith(File.separator))
dirname=dirname+File.separator;
File dir=new File(dirname);
if(!dir.exists())
{
System.out.println("该文件夹不存在");
return;
}
File[] files=dir.listFiles();
for(int i=0;i<files.length;i++)
{
if(files[i].isFile())
{
if(files[i].getName().equals(filename))
System.out.println("文件路径为:"+files[i].getAbsolutePath());
}
else
{
searchFiles(files[i].getAbsolutePath(),filename);
}
}
}

public static File[] searchFile(File folder,final String keyWord)
{    
File[] subFolders=folder.listFiles(new FileFilter()
{
public boolean accept(File pathname)
{
if(pathname.isFile())
countFiles++;
else
countFolders++;
if(pathname.isDirectory()||(pathname.isFile()&&pathname.getName().contains(keyWord)))
return true;
return false;
}

});
List<File> result=new ArrayList<File>();
for(int i=0;i<subFolders.length;i++)
{
if(subFolders[i].isFile())
result.add(subFolders[i]);
else
{
File[] foldResult=searchFile(subFolders[i],keyWord);
for(int j=0;j<foldResult.length;j++)
result.add(foldResult[j]);
}
}
File files[]=new File[result.size()];
result.toArray(files);
return files;
}

public static void copyFileToFile(File tofile,File fromfile)
{
if(tofile.exists())
{
System.out.println("文件已经存在!");
return;
} 
else
{
try {
tofile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("复制文件从"+fromfile.getAbsolutePath()+"到"+tofile.getAbsolutePath());
try {
InputStream is=new FileInputStream(fromfile);
FileOutputStream fos=new FileOutputStream(tofile);
byte []buffer=new byte[1024];
try {
while(is.read(buffer)!=-1)
fos.write(buffer);
is.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* @param file 要复制的文件
* @param dirname 文件复制到目标目录
*/
public static void copyFileToDir(File file,String dirname)
{
createDir(dirname);
String filename=dirname+"/"+file.getName();
File nfile=new File(filename);
copyFileToFile(nfile,file);
}

public static void copyDirToDir(String todir,String fromdir)
{
File []f=getDirFiles(fromdir);
for(int i=0;i<f.length;i++)
{
if(f[i].isFile())
copyFileToDir(f[i],todir);
else
copyDirToDir(todir+"/"+f[i].getName(),fromdir+"/"+f[i].getName());
}
System.out.println(fromdir+"复制到文件夹"+todir+"复制完成!");
}



/**
* @param args
* @throws IOException 
*/
public static byte[] readFilebyByte(String filename) throws IOException
{
byte[] result = null;
File file = new File(filename);
if (file.exists() && file.isFile() && file.length() > 0) 
{
result = new byte[(int) file.length()];
FileInputStream inStream = new FileInputStream(filename);
inStream.read(result);
inStream.close();
}
return result;
}

public static void readFilebyLine(String filename) throws IOException
{
File file=new File(filename);
BufferedReader reader=null;
System.out.println("以行为单位读取数据,一次读取一行:");
try {
reader=new BufferedReader(new FileReader(file));
String temp=null;
int line=1;
while((temp=reader.readLine())!=null)
{
System.out.println("line"+line+":"+temp);
line++;
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static void writeFilebyByte(String filename) throws IOException
{
String content="fsfni ds dw wod";
File file=new File(filename);
OutputStream out=null;
System.out.println("以字节为单位写入文件:"+content);
out=new FileOutputStream(file);
byte []bytes=content.getBytes();
out.write(bytes);
out.close();
}

public static void writeFilebyChar(String filename) throws IOException
{
String content="fsfni ds dw wod";
File file=new File(filename);
Writer writer=null;
System.out.println("以字符为单位写入文件:"+content);
writer=new OutputStreamWriter(new FileOutputStream(file));
writer.write(content);
writer.close();
}

public static void writeFilebyLine(String filename) throws IOException
{
String content="fsfni ds dw wod";
File file=new File(filename);
PrintWriter writer=null;
System.out.println("以行为单位写入文件:"+content);
writer=new PrintWriter(new FileOutputStream(file));
writer.println(content);
writer.print(2355);
writer.flush();
writer.close();
}

public static void addContent1(String filename) throws IOException
{
FileWriter writer=new FileWriter(filename,true);
String content="this is a nice man,so you should get it";
writer.write(content);
writer.close();
}

public static void addContent2(String filename) throws IOException
{
RandomAccessFile rfile=new RandomAccessFile(filename, "rw");
long filelength=rfile.length();
rfile.seek(filelength);
String content="hello world";
rfile.write(content.getBytes("gb2312"));
rfile.close();

}
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


String dirname="c:/cc/";
String todir="c:/dd/";
String tdir="c:/ee/";
String fn="c:/cc/c.txt";
String filename="ww.txt";
File folder=new File("c:/cc/");
String keyword="ww";
//readFilebyLine(fn);
writeFilebyLine(fn);
addContent1(fn);
addContent2(fn);
//File fromfile=new File("c:/cc/c.txt");
//File tofile=new File("e:/ccc.txt");
//byte[] fb=readFilebyByte(fn);
//for(int i=0;i<fb.length;i++)
// System.out.println((char)fb[i]);
//System.out.println(fb.toString());
//copyFile(tofile,fromfile);
//copyFileToDir(fromfile,todir);
//copyDirToDir(tdir,dirname);
//File[] f=searchFile(folder,keyword);
//createDir(dirname);
//deleteDirs(dirname);
//searchFiles(dirname, filename);
//File[] f=getDirFiles(dirname);
//for(int i=0;i<f.length;i++)
{
// System.out.println(f[i].getAbsolutePath());
}
}


}