JavaIO流习题练习
1.遍历输出某个文件夹中所有的文件名
/*遍历输出某个文件夹中所有的文件名.*/
public class demo1 {
public static void main(String[] args) {
File f=new File("D:/JavaIO/demo2");//选定指定文件的绝对地址
Chazhao(f);//将创建的f对象放入我们在下面自定义的方法之中
}
public static void Chazhao(File file) {//利用递归进行运算
File[]files=file.listFiles();//得到的是一个 File 类型的数组
for(File file2:files){
System.out.println(file2.toString());
if (file2.isDirectory()){//判断file2是否存在
Chazhao(file2);
}
}
}
}
- 写一个方法,将一个文件分割为每份1MB大小的若干份,存储在一个temp的文件夹中(每份文件名自己定义),
然后再写一个方法,将这若干份合并为一个文件
public class demo3 {
public static void main(String[] args) throws IOException {
File f=new File("D:/JavaIO/demo2/8IO.pdf");
Devote(f);
Join(f);
}
//进入拆分部分
private static void Devote(File f) {
int m=1024*1024;//将文件设计分为1MB大小的文件
long num= ((f.length()%m==0)?f.length()/m:(f.length())/m+1);//文件大小是否能被整除,不能再创新文件,num表示创建文件个数
int sum=0;
for(int i=0;i<f.length();i++){
try {
FileInputStream in=new FileInputStream("D:/JavaIO/demo2/8IO.pdf");//写入输入文件的绝对路径
FileOutputStream fileOutputStream=new FileOutputStream("D:/JavaIO/demo2/temp"+i+".temp");//输出如干份,+i进行排序".temp"表示文件类型
byte[]b=new byte[1024];
int length=0;
while((length=in.read())!=-1){//文件为空时返回的默认值为-1
fileOutputStream.write(b,0,length);//输出到指定文件地址,输出指定数组的内容
sum+=length;
}
if(sum==num){//进行中断循环
sum=0;
break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//进入合并部分
private static void Join(File temp) throws IOException {
FileOutputStream out=new FileOutputStream("D:/JavaIO/demo2/8IO.pdf");//填写输出的地址
File f=new File(String.valueOf(temp));//创建的f对象包含所有后缀名为temp的文档
File[]fs=f.listFiles();//将所有文档放入集合当中
for(int i=0;i<fs.length;i++){//进行循环合并
FileInputStream in=new FileInputStream("D:/JavaIO/demo2/temp"+i+".temp");
byte[]b=new byte[1024];
int length=0;
while((length=in.read())!=-1){
out.write(b,0,length);
}
in.close();//关闭输入流
}
out.close();//关闭输出流
for(File t:fs){
t.delete();//将之前创造出的多余文件删除,以免浪费内存
}
f.delete();
}
}
3.对整个文件夹进行复制操作
public class TestExp2 {
public static void main(String[] args) throws IOException {
Scanner sc2 = new Scanner(System.in);
System.out.println("请输入你想拷贝的文件地址");
String msg1 = sc2.next();
System.out.println("你想拷贝到哪里(地址)");
String msg2 = sc2.next();
CopyFile(msg1, msg2);
}
public static void CopyFile(String str, String str2) throws IOException {
// 用于读取源文件的子目录或子文件
File file = new File(str);
// 根据给定的正则表达式拆分源文件。
String[] s = str.split("/");
// 获取源文件的最后一个文件并拼接到目标文件
String str3 = s[s.length - 1];
String str4 = str2 + "/" + str3;
// 用于把源文件的子目录或子文件写出到目标文件
File file2 = new File(str4);
// 判断源文件是否存在
if (file.exists()) {
// 判断源文件是否是目录
if (file.isDirectory()) {
// 在目标文件创建该目录
file2.mkdir();
// 获取源文件的子目录
String[] f = file.list();
for (String ss : f) {
// 递归本方法
CopyFile(str + "/" + ss, str4);
}
} else {
// 在目标文件创建子文件
file2.createNewFile();
// 创建输入流管道
InputStream input = new FileInputStream(file);
// 创建输出流管道
OutputStream output = new FileOutputStream(file2);
byte[] by = new byte[1024];
int count = 0;
// 在源文件读取该文件的数据
while ((count = input.read(by, 0, by.length)) != -1) {
// 在目标文件写入该文件的数据
output.write(by, 0, count);
}
// 关闭输入流
input.close();
// 关闭输出流前强制写出所有输出字节
output.flush();
// 关闭输出流
output.close();
}
} else {
System.out.println("文件不存在!");
}
}
}