多进程拷贝文件
package com.softeem.tastone;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CopyFileextends Thread {
private File file;//源文件
private File dir;//目標文件
private long start;//指针起始位置
private long end;//指针结束位置
public CopyFile(Filefile, File dir, long start,long end) {
super();
this.file =file;
this.dir =dir;
this.start =start;
this.end =end;
}
@Override
public void run() {
RandomAccessFile input = null;
RandomAccessFile output = null;
int count = 0;//当前拷贝字节长度
try {
input = new RandomAccessFile(file,"r");
output = new RandomAccessFile(dir,"rw");
input.seek(start);//关键点1,读写位置都在start
output.seek(start);//
byte[] b = new byte[1024];//复制文件
int len = 0;
while ((len =input.read(b)) != -1 &&count <= (end -start)) {
count += len;
output.write(b, 0,len);
}
System.out.println("完成拷贝!");
} catch (FileNotFoundExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}//fianlly保护文件逐个关闭
finally {
try {
if (output !=null) {
output.close();
}
if (input !=null) {
input.close();
}
} catch (IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[]args) {
// TODO Auto-generated method stub
File file = new File("E:\\大虫子\\larva搞笑虫子\\1.mp4");
File dir = new File("E:\\by",file.getName());
long temp =file.length() / 4;
for (int i = 0;i < 4; i++) {
//关键点2,i*temp,(i+1)*temp,控制每次拷贝的长度
CopyFile cp = new CopyFile(file,dir, i * temp, (i + 1) *temp);
cp.start();
}
}
}
/*小知识点
* 字符串转日期
* SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Stringfname=
* sdf.format(new Date());
*/
/*
字节输入流转化为字符输入流
关键点1 定义一个char数组去接收字节
关键点2 数组接收的字节写入,当数组接收不到时不再写入
*/
package com.softeem.conversion;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class Dome1 {
public static void main(String[]args) throws IOException {
// 标准输出
InputStream is = System.in;//键盘输入字节
// 将字节输入流装换为字符输入流流InputStream——》Reader
Reader reader = new InputStreamReader(is);
char[] c = new char[100];
int len = 0;
while ((len =reader.read(c)) != -1) {
String s = new String(c, 0,len);
System.out.println(s);
if (s.trim().equals("quit")) {
System.out.println("结束");
System.exit(0);
}
}
reader.close();
}
}