package day1901;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
System.out.println("输入源文件");
String s = new Scanner(System.in).nextLine();
File from = new File(s);
if(! from.exists()){
System.out.println("输入的文件路径不存在");
return;
}
if(from.isDirectory()){
System.out.println("不支持目录,请输入文件路径");
return;
}
System.out.println("请输入拆分文件的大小(kb):");
long size = new Scanner(System.in).nextLong();
size *= 1024;
try {
split(from,size);
System.out.println("拆分完成");
}
catch (Exception e){
System.out.println("拆分失败");
//打印完整异常信息
e.printStackTrace();
}
}
private static void split(File from, long size) throws Exception{
/*准备目录
* 1.获得from的完整路径并在后面拼接“_split”
* 在封装成File对象,赋给dir
* 2.如果目录不存在,创建目录
* 3.否则
* 4.对目录列表,遍历所有文件删除
*/
File dir = new File (from.getAbsolutePath()+"_split");
if(! dir.exists()){
dir.mkdir();
}
else {
File[] files = dir.listFiles();
for (File f:files){
f.delete();
}
}
/*拆分
* 1.定义字节计数变量 long byteCount = 0
* 2.定义文件计数变量 fileCount = 0
* 3.创建 FileInputStream 赋给 in
* 接文件 from
* 4.定义 FileInputStream 变量 out = null
* 5.单字节读取,读取的字节值赋给 b
* 6.如果 out == null 或者 byteCount == size
* 7.如果 out != null
* 8.out.close()
* 9.创建FileOutputStream 赋给 out
* 接新的拆分文件
* 文件路径
* new File(dir,name="."+(++fileCount))
* 10.byteCount = 0
* 11.将 b 向输出流 out 输出
* 12.字节技术 byteCount++
* 13.in.close()
* out.close()
*/
String name = from.getName();
long byteCount = 0;
int fileCount = 0;
BufferedInputStream in = (new BufferedInputStream
(new FileInputStream(from)));
BufferedOutputStream out = null;
int b;
while ((b = in.read()) != -1){
if (out == null || byteCount == size);if(out != null) out.close();
out = new BufferedOutputStream(
new FileOutputStream(
new File(dir,name+"."+(++fileCount))));
byteCount = 0;}
out.write(b);
byteCount++;
in.close();
out.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
System.out.println("输入源文件");
String s = new Scanner(System.in).nextLine();
File from = new File(s);
if(! from.exists()){
System.out.println("输入的文件路径不存在");
return;
}
if(from.isDirectory()){
System.out.println("不支持目录,请输入文件路径");
return;
}
System.out.println("请输入拆分文件的大小(kb):");
long size = new Scanner(System.in).nextLong();
size *= 1024;
try {
split(from,size);
System.out.println("拆分完成");
}
catch (Exception e){
System.out.println("拆分失败");
//打印完整异常信息
e.printStackTrace();
}
}
private static void split(File from, long size) throws Exception{
/*准备目录
* 1.获得from的完整路径并在后面拼接“_split”
* 在封装成File对象,赋给dir
* 2.如果目录不存在,创建目录
* 3.否则
* 4.对目录列表,遍历所有文件删除
*/
File dir = new File (from.getAbsolutePath()+"_split");
if(! dir.exists()){
dir.mkdir();
}
else {
File[] files = dir.listFiles();
for (File f:files){
f.delete();
}
}
/*拆分
* 1.定义字节计数变量 long byteCount = 0
* 2.定义文件计数变量 fileCount = 0
* 3.创建 FileInputStream 赋给 in
* 接文件 from
* 4.定义 FileInputStream 变量 out = null
* 5.单字节读取,读取的字节值赋给 b
* 6.如果 out == null 或者 byteCount == size
* 7.如果 out != null
* 8.out.close()
* 9.创建FileOutputStream 赋给 out
* 接新的拆分文件
* 文件路径
* new File(dir,name="."+(++fileCount))
* 10.byteCount = 0
* 11.将 b 向输出流 out 输出
* 12.字节技术 byteCount++
* 13.in.close()
* out.close()
*/
String name = from.getName();
long byteCount = 0;
int fileCount = 0;
BufferedInputStream in = (new BufferedInputStream
(new FileInputStream(from)));
BufferedOutputStream out = null;
int b;
while ((b = in.read()) != -1){
if (out == null || byteCount == size);if(out != null) out.close();
out = new BufferedOutputStream(
new FileOutputStream(
new File(dir,name+"."+(++fileCount))));
byteCount = 0;}
out.write(b);
byteCount++;
in.close();
out.close();
}
}