/*查看目录下所有的文件*/
package cn.file;
import java.io.File;
public class Text2 {
public static void main(String[] args) {
File a=new File("F:\\图片");
File a1=new File("F:\\你的");
chek(a1);
}
public static void chek(File f3) {//查看目录下的所有文件
if(f3.exists()){
if(f3.isDirectory()){
File [] f=f3.listFiles();//查看目录的文件
for(int i=0;i<f.length;i++){
chek(f[i]);
}
}
else {System.out.println(f3);};
}
}
复制子目录
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
//练习:关于目录的拷贝?
public class Text6 {
// 缓冲区大小
private static final int buffer_size = 1024*2;
// 源和目标检测
private static void doCopy(File src, File dst)throws IOException {
if (src.isFile()) {
copyFile(src, dst);
} else {
File dir = copyDirectory(src, dst);
File[] files = src.listFiles();
if (files.length == 0) {
} else {
for (File file : files) {
doCopy(file, dir);
}
}
}
}
// 拷贝实现
public static void copy(File src, File dst) throws IOException {
doCopy(src, dst);
}
// 文件拷贝
private static void copyFile(File src, File dst) throws IOException {
File file = new File(dst, src.getName());
copy(new BufferedInputStream(new FileInputStream(src)), new BufferedOutputStream(new FileOutputStream(file)));
}
// 目录拷贝 要在目标设备中创建目录
private static File copyDirectory(File src, File dst) {
File file = new File(dst, src.getName());
file.mkdir();
return file;
}
// 拷贝
private static int copy(BufferedInputStream in, BufferedOutputStream out) throws IOException {
int byteCount = 0; // 问号 byteCount
try {
int bytesReader = 0;
byte [] buffer = new byte[buffer_size];
while ((bytesReader = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesReader);
byteCount += bytesReader;
}
out.flush();
} finally {
in.close();
out.close();
}
return byteCount;
}
public static void main(String[] args)throws IOException
{
File a=new File("F:\\测试的文件");
File b=new File("F:\\我的");
copy(a,b);
System.out.println("finish.................");
}
}
给文件改名
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Text1{
public static void main(String[] args)throws IOException {
File a=new File("F:\\测试的文件\\我的");
File b=new File("F:\\测试的文件\\我的");
copyList(a,b);
}
public static void copyList(File a,File b )throws IOException{//第一步
File c=a;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
byte bs []=new byte [1024];
if (!b.exists()){ //判断b的文件夹是否存在
b.mkdir();
}
if(c.isDirectory()){
File d [] =c.listFiles();//得到目录文件的数组
if(d!=null){//判断文件目录是否为空
for (int i=0;i<d.length;i++){
copyList(d[i],b);//递归查询
//System.out.println(d[i]);//查看目录里的文件
if(d[i].isFile()){
if(d[i].getName().endsWith(".jpg")){//根据条件判断的复制
int rel=0;
bis=new BufferedInputStream(new FileInputStream(d[i]));
bos=new BufferedOutputStream(new FileOutputStream(new File(b,i+".jpg")));
while((rel=bis.read(bs))!=-1){
bos.write(bs, 0, rel);
bos.flush();
}
bos.close();
bis.close();
d[i].delete();//复制后删去复制的文件 这样看起来像是剪切
}
}
}
}
}else{System.out.println(c);}
}
}
/**过滤后复制文件夹*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//经典过滤方案 用到 iO流 递归
public class Text4 {
public static void print(File dir,File f2 )throws IOException{
//判断鱼塘是否存在
if (dir!=null){
//判断鱼塘是否有鱼
if (dir.isDirectory()){
//把鱼塘里面所有东西装箱
File f []=dir.listFiles();
//判断箱子是否为空
if (f!=null){
//遍历箱子里面的东西
for (int i=0;i<f.length;i++){
//假如箱子里还有箱子就继续打开
print(f[i],f2);
if (f[i].isFile()){
String name =f[i].getName();
if(name.endsWith(".jpg")){
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(f[i]));
BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream(new File(f2,f[i].getName())));
int rel=0;
byte [] bs=new byte [1024];
while((rel=bi.read(bs))!=-1){
bo.write(bs,0,rel);
bo.flush();
//f[i].delete();//复制后删去复制的文件 这样看起来像是剪切
}
}
}
}
}else{//System.out.println(dir);
};
}
}
}
public static void main(String[] args) throws IOException {
File f1=new File("F:\\音乐");
File f2=new File("D:\\速度1");
print(f1,f2);
System.out.println("复制成功");
//print(new File("F:"+File.separator+"音乐"));
//System.out.println("遍历文件夹");
}
}
/*删除文件与文件夹*/
import java.io.File;
import java.io.IOException;
public class Text3 {
public static void main(String[] args) throws IOException {
File f=new File("F:\\我的");
delect(f);
}
public static void delect(File s){
File b []=null;
if(s.exists()){//判读是否存在
if(s.isDirectory()){//判断是不是目录
b=s.listFiles();
if(b!=null){
for(int i=0;i<b.length;i++){
if(b[i].isFile()){ //判断是不是目录
b[i].delete();//删除子文件
}else{
delect(b[i]);//递归
b[i].delete();}//删除子目录
}
}
}else{s.delete();};
s.delete();
}
}
}