如题所述
package com.example.PhoneSafe.test;
import java.io.*;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-8-7
* Time: 上午10:58
* To change this template use File | Settings | File Templates.
*/
public class RandomAccessFileTest {
public static void main(String [] args){
try{
int fileSize; //描述文件大小
int startPos; //文件开始写入位置
Thread [] threads = new Thread[5]; //定义一个线程数组
File file = new File("E:\\baby\\info.txt"); //需要复制的文件
FileInputStream fis = new FileInputStream(file); //装载到的文件
fileSize = (int) file.length()/5+1; //获得单个线程写入文件的单元大小
//采用for循环来实习多线程的启动
for(int i=0;i<5;i++){
startPos = i*fileSize; //线程写入的起始位置
threads[i] = new Thread(new DownloadFile(fis,new RandomAccessFile("E:\\baby\\info_copy.txt","rw"),startPos,fileSize));
threads[i].start();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class DownloadFile implements Runnable{
private FileInputStream is; //文件输入流
private RandomAccessFile rAF; //文件输出流
private int startPos;
private int fileUnitSize;
private int length;
public DownloadFile(FileInputStream is,RandomAccessFile rAF,int startPos,int fileUnitSize){
this.is = is;
this.rAF = rAF;
this.startPos = startPos;
this.fileUnitSize = fileUnitSize;
}
@Override
public void run() {
try{
is.skip(startPos);
rAF.seek(startPos);
byte [] buffer = new byte[1024];
int hasbyte = 0;
while((hasbyte=is.read(buffer))!=-1){
rAF.write(buffer,0,hasbyte);
length +=hasbyte;
if(length<fileUnitSize){
return;
}
}
}catch(IOException e){
e.printStackTrace();
} finally{
System.out.println("end");
try{
rAF.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
import java.io.*;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-8-7
* Time: 上午10:58
* To change this template use File | Settings | File Templates.
*/
public class RandomAccessFileTest {
public static void main(String [] args){
try{
int fileSize; //描述文件大小
int startPos; //文件开始写入位置
Thread [] threads = new Thread[5]; //定义一个线程数组
File file = new File("E:\\baby\\info.txt"); //需要复制的文件
FileInputStream fis = new FileInputStream(file); //装载到的文件
fileSize = (int) file.length()/5+1; //获得单个线程写入文件的单元大小
//采用for循环来实习多线程的启动
for(int i=0;i<5;i++){
startPos = i*fileSize; //线程写入的起始位置
threads[i] = new Thread(new DownloadFile(fis,new RandomAccessFile("E:\\baby\\info_copy.txt","rw"),startPos,fileSize));
threads[i].start();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class DownloadFile implements Runnable{
private FileInputStream is; //文件输入流
private RandomAccessFile rAF; //文件输出流
private int startPos;
private int fileUnitSize;
private int length;
public DownloadFile(FileInputStream is,RandomAccessFile rAF,int startPos,int fileUnitSize){
this.is = is;
this.rAF = rAF;
this.startPos = startPos;
this.fileUnitSize = fileUnitSize;
}
@Override
public void run() {
try{
is.skip(startPos);
rAF.seek(startPos);
byte [] buffer = new byte[1024];
int hasbyte = 0;
while((hasbyte=is.read(buffer))!=-1){
rAF.write(buffer,0,hasbyte);
length +=hasbyte;
if(length<fileUnitSize){
return;
}
}
}catch(IOException e){
e.printStackTrace();
} finally{
System.out.println("end");
try{
rAF.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2011-10-31
Java的内8核和class文6件是基于kunicode的,这使Java程序具有良好的跨平台性,但也m带来了f一i些中2文3乱码问题的麻烦。原因主要有两方8面,Java和JSP文2件本身编译时产生的乱码问题和Java程序于t其他媒介8交互8产生的乱码问题。 这篇文5章讲很细: >>Java。JSP中2文2乱码问题解决心6得 -编程 - BlogJava -=>hキnq省处gkēècエ司Xkēègycエ司X追问
哥们。。你 在 说 些 啥???
要复制。。。也要找 个 像点 的 啊。。。
有点专业精神 好 不。。