java中如何实现多线程对同一文件的读和写 一个文件上能否建立多个文件通道(java.nio.FileChannel)?也就是能否在一个文件上建立多个输入与输出流? FileInputStream.getChannel(),FileOutputStream.getChannel()返回FileChannel对象。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.nio.*;import java.nio.channels.*;import java.util.*;public class FileTest extends Thread{ private File from; private File to; private long start; private long end; public static final int BYTE_SIZE=2048*2048; public FileTest(File from,File to,long start,long end) { this.from=from; this.to=to; this.start=start; this.end=end; } public void run() { try { RandomAccessFile input=new RandomAccessFile(from,"r");//打开读文件 RandomAccessFile output=new RandomAccessFile(to,"rw");//打开写文件 input.seek(start);//将读标志定位到start output.seek(start);//将写标志定位到start long length=end-start+1;//文件段的长度 byte[]b=new byte[BYTE_SIZE]; long m=length/b.length;//读和写入2048个字节的次数 int leftsize=(int)(length-m*b.length);//剩下的字节数 System.out.println("开始从文件"+from.getPath()+"的"+start+"位置读数据,并写入"+to.getPath()+ "文件的"+start+"位置"); long timestart=System.currentTimeMillis(); for(long i=1;i<=m;i++) { input.read(b);//将数据从from文件读入 output.write(b);//将数据写入to文件 } input.read(b,0,leftsize);//读入最后剩下的字节 output.write(b,0,leftsize);//写入最后剩下的字节 input.close();//关闭输入 output.close();//关闭输出 long timeend=System.currentTimeMillis(); System.out.println("线程"+Thread.currentThread().getName()+"读写完毕,共使用"+(timeend-timestart)+"毫秒"); }catch(IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("测试文件多线程的读和写"); System.out.print("请输入要进行测试的文件路径:"); String fpath=sc.nextLine(); File from=new File(new File(fpath).getPath()); if(from.exists()) { if(from.isFile()) { System.out.print("将使用n(n>=1&&n<=10)个线程去读,n="); int n=Integer.parseInt(sc.nextLine()); while(n<=0||n>10) { System.out.print("输入的n值不在1~10之间,请重新输入:"); n=sc.nextInt(); } System.out.print("请输入要将测试文件使用多线程复制到的目录路径:"); String tpath=sc.nextLine(); File toDir=new File(new File(tpath).getPath()); if(toDir.exists()&&toDir.isDirectory()) { File to=new File(toDir,from.getName()); long filelength=from.length();//要复制文件的长度 //下面在指定的目录下创建一个大小为from文件大小的全0文件 try { DataOutputStream output=new DataOutputStream(new FileOutputStream(to)); byte[]b=new byte[FileTest.BYTE_SIZE]; long m=filelength/b.length; int leftsize=(int)(filelength-m*b.length); for(long i=1;i<=m;i++) output.write(b); output.write(b,0,leftsize); output.close(); } catch (FileNotFoundException e) { // TODO 自动生成 catch 块 e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } //开始测试,有问题,待会儿解决 long size=filelength/n; long point=0; for(int i=0;i<n;i++) { if(i!=n) { FileTest f=new FileTest(from,to,point,point+size-1); point+=size; f.start(); } else { FileTest f=new FileTest(from,to,point,filelength-1); f.start(); } } }else { System.out.println(toDir.getPath()+"不存在,或者不是一个文件夹"); } } else System.out.println(from.getName()+"是一文件夹,不在测试范围"); } else System.out.println(from.getPath()+"不存在,请检查"); }} 界面怎么不能显示 空字符 == null吗? 如何得到一个org.w3c.dom.Node的实例节点 byte【】 转化 求好心人帮忙!!! java怎么将一个有序数据转换为一个ANSI字符,相当于delphi的Chr函数?? 数据库查询问题 类之间互相访问成员的问题???急!!! 怎样将一个字符串根据一个字符分成两个字符串!那个函数叫什么,在哪个类里!谢谢! 一个简单的程序! 同时显示一个JFrame和一个JDialog,在没有点击JDialog确定前不能点击JFrame,怎么做? 关于在linux下创建目录的问题! 将map值封装到list,怎么在jsp页面循环取出?
import java.nio.*;
import java.nio.channels.*;
import java.util.*;public class FileTest extends Thread{
private File from;
private File to;
private long start;
private long end;
public static final int BYTE_SIZE=2048*2048;
public FileTest(File from,File to,long start,long end)
{
this.from=from;
this.to=to;
this.start=start;
this.end=end;
}
public void run()
{
try {
RandomAccessFile input=new RandomAccessFile(from,"r");//打开读文件
RandomAccessFile output=new RandomAccessFile(to,"rw");//打开写文件
input.seek(start);//将读标志定位到start
output.seek(start);//将写标志定位到start
long length=end-start+1;//文件段的长度
byte[]b=new byte[BYTE_SIZE];
long m=length/b.length;//读和写入2048个字节的次数
int leftsize=(int)(length-m*b.length);//剩下的字节数
System.out.println("开始从文件"+from.getPath()+"的"+start+"位置读数据,并写入"+to.getPath()+
"文件的"+start+"位置");
long timestart=System.currentTimeMillis();
for(long i=1;i<=m;i++)
{
input.read(b);//将数据从from文件读入
output.write(b);//将数据写入to文件
}
input.read(b,0,leftsize);//读入最后剩下的字节
output.write(b,0,leftsize);//写入最后剩下的字节
input.close();//关闭输入
output.close();//关闭输出
long timeend=System.currentTimeMillis();
System.out.println("线程"+Thread.currentThread().getName()+"读写完毕,共使用"+(timeend-timestart)+"毫秒");
}catch(IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("测试文件多线程的读和写");
System.out.print("请输入要进行测试的文件路径:");
String fpath=sc.nextLine();
File from=new File(new File(fpath).getPath());
if(from.exists())
{
if(from.isFile())
{
System.out.print("将使用n(n>=1&&n<=10)个线程去读,n=");
int n=Integer.parseInt(sc.nextLine());
while(n<=0||n>10)
{
System.out.print("输入的n值不在1~10之间,请重新输入:");
n=sc.nextInt();
}
System.out.print("请输入要将测试文件使用多线程复制到的目录路径:");
String tpath=sc.nextLine();
File toDir=new File(new File(tpath).getPath());
if(toDir.exists()&&toDir.isDirectory())
{
File to=new File(toDir,from.getName());
long filelength=from.length();//要复制文件的长度
//下面在指定的目录下创建一个大小为from文件大小的全0文件
try {
DataOutputStream output=new DataOutputStream(new FileOutputStream(to));
byte[]b=new byte[FileTest.BYTE_SIZE];
long m=filelength/b.length;
int leftsize=(int)(filelength-m*b.length);
for(long i=1;i<=m;i++)
output.write(b);
output.write(b,0,leftsize);
output.close();
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
//开始测试,有问题,待会儿解决
long size=filelength/n;
long point=0;
for(int i=0;i<n;i++)
{
if(i!=n)
{
FileTest f=new FileTest(from,to,point,point+size-1);
point+=size;
f.start();
}
else
{
FileTest f=new FileTest(from,to,point,filelength-1);
f.start();
}
}
}else {
System.out.println(toDir.getPath()+"不存在,或者不是一个文件夹");
}
}
else
System.out.println(from.getName()+"是一文件夹,不在测试范围");
}
else
System.out.println(from.getPath()+"不存在,请检查");
}
}