实现文件分离(切割)
package com.IO;
import java.io.*;
import java.util.Arrays;
public class FileCutter02 {
public static void main(String[] args) throws IOException {
File file = new File("d:/music.flac");
long size = 1024 * 1024 * 10;
File dir = new File("d:/mus");
int split = split(file, size, dir);
System.out.println(split);
}
public static int split(final File original, final long size, final File directory) throws IOException {
if (original == null || !original.isFile()){
throw new RuntimeException();
}
if (!directory.exists()){
directory.mkdirs();
}
int count = 0;
FileInputStream file = new FileInputStream(original);
if (size <= Integer.MAX_VALUE - 8){
int s;
byte[] bytes = new byte[(int) size];
while ((s = file.read(bytes)) > 0) {
File inFile = new File(directory + "/" + (count + 1) + ".temp");
OutputStream out = new FileOutputStream(inFile);
out.write(bytes, 0, s);
count++;
out.close();
}
}else {
File outFile = new File(directory,count++ + ".temp");
OutputStream outputStream = new FileOutputStream(outFile);
long sum = size;
while (size > Integer.MAX_VALUE - 8 && file.available() != 0){
byte[] bytes = new byte[Integer.MAX_VALUE - 8];
int readSize = file.read(bytes);
outputStream.write(bytes,0, readSize);
sum -= readSize;
}
if (file.available() != 0){
byte[] bytes = new byte[(int) sum];
int readSize = file.read(bytes);
outputStream.write(bytes,0,readSize);
}
outputStream.close();
}
file.close();
return count;
}
}
实现文件合并
package com.IO;
import java.io.*;
import java.util.Arrays;
public class FileCutter02 {
public static void main(String[] args) throws IOException {
FileCombiner combiner = new FileCombiner();
File file1 = new File("d:/mus");
File file2 = new File("d:/vedio-combined.flac");
long join = combiner.join(file1, file2);
System.out.println(join);
}
}
class FileCombiner {
public long join(final File dir, final File target) throws IOException {
if (dir == null || !dir.isDirectory()){
throw new NullPointerException();
}
if (target == null){
throw new NullPointerException();
}
if (!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}
long size = 0;
File[] files = dir.listFiles((file,name) -> name.endsWith(".temp"));
if (files != null && files.length > 0){
Arrays.sort(files,(f1,f2) -> {
String n1 = f1.getName();
String n2 = f2.getName();
int index1 = Integer.parseInt(n1.split("\\.")[0]);
int index2 = Integer.parseInt(n2.split("\\.")[0]);
return index1 - index2;
});
FileOutputStream outputStream = new FileOutputStream(target, true);
for (File file : files) {
if (file.length() <= Integer.MAX_VALUE - 8){
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = inputStream.readAllBytes();
outputStream.write(bytes);
inputStream.close();
}else {
FileInputStream inputStream = new FileInputStream(file);
while (inputStream.available() != 0){
byte[] bytes = new byte[Integer.MAX_VALUE - 8];
int readSize = inputStream.read(bytes);
outputStream.write(bytes,0,readSize);
}
inputStream.close();
}
size += file.length();
}
outputStream.close();
}
return size;
}
}
文件复制
- 第一种方法:通过byte 数组一个一个的写入到outputStream中
- 第二种方法:通过readAllBytes 方法
- 第三种方法:通过transferTo 方法
package com.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedOutputStreamDemo03 {
public static void main(String[] args) {
try {
FileInputStream inputStream = new FileInputStream("d:/input-file.txt");
FileOutputStream outputStream = new FileOutputStream("d:/kaifamiao/input-file.txt");
long l = inputStream.transferTo(outputStream);
System.out.println(l);
outputStream.close();
inputStream.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}