- /*
- RandomAccessFile 类
- getFilePointer();//获取指针
- skipBytes();//跳过字节
- seek(); 设置指针
- 实现多线程分段输出
- */
- import java.io.*;
- class Demo3
- {
- public static void main(String[] args)throws Exception
- {
- //读取
- RandomAccessFile raf=new RandomAccessFile("g.mp3","rw");
- //写入
- RandomAccessFile raf1=new RandomAccessFile("gg.mp3","rw");
- //三个线程进行分段写入
- //seek 设置指针位置 实现分段功能
- new Thread(new Thread1(raf,raf1,0)).start();
- new Thread(new Thread1(raf,raf1,2097152)).start();
-
- new Thread(new Thread1(raf,raf1,4194304)).start();
- }
- }
- //多线程
- class Thread1 implements Runnable
- {
- private RandomAccessFile raf;
- private RandomAccessFile raf1;
- private int skip;
- public Thread1(RandomAccessFile raf,RandomAccessFile raf1,int skip){
- this.raf=raf;
- this.raf1=raf1;
- this.skip=skip;
- }
- //run方法
- public void run(){
- try
- {
- byte []buf=new byte[1024*1024*2];
- int len=0;
- len=raf.read(buf);
- raf1.write(buf,0,len);
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- }
- finally
- {
- try
- {
- if(raf!=null)
- raf.close();
- if(raf1!=null)
- raf1.close();
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- }
- }
- }
- }
复制代码
我这个貌似很彩笔 |