请人帮忙 一个流类怎么使用? ----在线等。。。。。。。。。。。(50分)

  • 主题发起人 guofengdelphi
  • 开始时间
G

guofengdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在要做一个复制文件的程序,我准备用缓冲输入、输出流类的方式实现。
程序中用到四个类:FileInputStream,FileOutputStream,
BufferedInputStream,BfferedOutputStream.
那位大虾做过这方面的程序 ,指导以下小弟他们之间怎么调用,能使程序运行起来跟快!
特别是后两个类之间,怎么传递数据 ?
能有具体的实例最好,小弟将感激不尽!!!
多谢了
 
怎么没人能解决吗,是不是问题太简单了,大家不屑于。
不过,小弟太笨,搞不定,
还请各位大哥多帮忙?
 
奇怪!这些流的使用还用来这里提问吗?最烂的JAVA书里面都会有!
>>特别是后两个类之间,怎么传递数据 ?
他们通过自己的方法进行传递啊!
例如定义一个
byte[] b;
然后使用BufferedInputStream的read(byte[] b, int off, int len) 将数据读到b里面。
然后再使用BfferedOutputStream的write(byte[] b, int off, int len)将数据写到输出流中。
 
我刚才写了一个用 FileInputStream, FileOutputStream 实现的文件拷贝,速度怎么样我没有测试,你先看看,有什么意见跟我说就行,我马上改:
import java.io.*;
class FileCopy{
public static void Copy(String src_file, String dest_file){
try {
FileInputStream in = new FileInputStream(new File(src_file));
FileOutputStream out = new FileOutputStream(new File(dest_file));

byte[] buf = new byte[65535];
int readByte = in.read(buf);
while (readByte > 0){
out.write(buf, 0, readByte);
readByte = in.read(buf);
}
in.close();
out.close();
} catch (Exception e){ e.printStackTrace();
}
}
public static void main(String[] args){
Copy(args[0], args[1]);
}
}
 
to :Sterntaler
怎么控制一个输入流读到某个规定的位置。不再往下读?
怎么指定一个位置?
怎么控制到此位置结束?
谢谢
 
流的读方法 int read(byte[] buf, int start, int length) 的意思不是很明显吗?
读取从 start 开始,长度为 length 字节的数据到缓冲区 buf 中。
 
但假如说一个文件很大的话<有20M>,我在读这个文件的话,我不可能把缓冲区定义的很大?
这样,影响系统的 性能,
我想把这个文件给分割成几部分,每一部分只读取文件的其中一部分.
并且,每一部分的读取采用线程的方式.
那么,怎么分割这个文件呢?
请问.怎么实现?
 
你实用程序判断一下不就可以了么,
在out.write字节流是的时候,你判断是否
是你的文件块就可以了。
 
我试试看?
但是,一个字节一个字节的读,绝对是慢的让人受不了?(我试过的)
 
用 java.io.RandomAccessFile 可以从文件的任意位置读取一段长度的数据。
你把文件分割成几个块,保存每个块的开始地址和长度。
 
to:Sterntaler
相关的方法我怎么没有找到,能说的具体一些吗?
我这里的api里,没有找到.
谢谢
 
我又花了一个点,才写出这个东西,你看看。我也在测试。
import java.io.*;
class BlockCopier implements Runnable{
final int BLK_SIZE = 4096;
int curPos, bytesToRead;
RandomAccessFile in, out;

BlockCopier(String src, String dest, int start, int len){
try {
in = new RandomAccessFile(src, "r");
out = new RandomAccessFile(dest, "rw");
curPos = start;
bytesToRead = len;
in.seek(curPos);
out.seek(curPos);
} catch(Exception e){ e.printStackTrace();
}
}

public void run(){
byte[] buf = new byte[BLK_SIZE];

try {
int readBytes = in.read(buf, curPos, Math.min(BLK_SIZE, bytesToRead));
while (readBytes > 0 &amp;&amp;
bytesToRead > 0){
bytesToRead -= readBytes;
out.write(buf, curPos, readBytes);
curPos += readBytes;
readBytes = in.read(buf, curPos, Math.min(BLK_SIZE, bytesToRead));
}

in.close();
out.close();
} catch (Exception e){ e.printStackTrace();
}
}
}
class Copy2{
final static int FILE_BLKS = 5;

public static void main(String[] args){
int[] posbegin
;
int[] len;
Thread[] copiers;

if (args.length != 2){
System.out.println("Usage: CopyMulThread src_file dest_file");
System.exit(0);
}

posbegin
= new int[FILE_BLKS];
len = new int[FILE_BLKS];
copiers = new Thread[FILE_BLKS];

/**
* Set block begin
positions and end positions
*/
File file = new File(args[0]);
int blkSize = (int)file.length() / FILE_BLKS;
posbegin
[0] = 0;
for (int i = 1;
i< FILE_BLKS;
++i){
posbegin
= posbegin
[i-1] + blkSize;
len = blkSize;
}
len[FILE_BLKS-1] = (int)file.length() - posbegin
[FILE_BLKS-1];
/**
* Start several thread todo
the job
*/
for (int i = 0;
i< FILE_BLKS;
++i){
copiers = new Thread(new BlockCopier(args[0], args[1], posbegin
, len));
copiers.start();
}
}
}
 
不行,出异常了, IndexOutOfBoundsException
 
那位大哥帮忙啊!我不行了。。
 
to:Sterntaler
你刚才写的那个方法readBytes = in.read(buf, curPos, Math.min(BLK_SIZE, bytesToRead));
你怎么理解了?
我认为他是从in的当前位置,把数据读到buf数组里,但从curpos位置开始.不是从 in的
curpos位置开始.
 
to:Sterntaler
问题关键是怎么判断读到某个位置,能让他停下来!
在使用 缓冲的 情况下.
如果单字节的话,可以实现,但就是速度慢的惊人!
 
大家,谁还有什么 好办法?
 
-> 我认为他是从in的当前位置,把数据读到buf数组里,但从curpos位置开始.不是从 in的
curpos位置开始.
是。这里是有问题,多谢指出。在构造函数里面,curPos 的初始值改为 0,而不是 start,同时in.seek(start), out.seek(start),这下如何?
-> 判断读到某个位置,能让他停下来
函数 BlockCopier(String src, String dest, int start, int len) 的作用就是从文件 src 中读取 start 开始,到 start + len 结束的数据,写到文件 dest 中。
 
多谢,现在已经好了 :D
速度也还可以,复制 20M 的东西也就那么几秒钟。
import java.io.*;
class BlockCopier implements Runnable{
final int BLK_SIZE = 4096;
int sumReadBytes = 0, bytesToRead;
RandomAccessFile in, out;

BlockCopier(String src, String dest, int start, int len){
try {
in = new RandomAccessFile(src, "r");
out = new RandomAccessFile(dest, "rw");
bytesToRead = len;
in.seek(start);
out.seek(start);

System.out.println("Thread started: begin
" + start + " len: " + len);
} catch(Exception e){ e.printStackTrace();
}
}

public void run(){
byte[] buf = new byte[BLK_SIZE];

try {
int readBytes = in.read(buf);
while (sumReadBytes < bytesToRead){
sumReadBytes += readBytes;
out.write(buf, 0, readBytes);
readBytes = in.read(buf);
}

in.close();
out.close();
} catch (Exception e){ e.printStackTrace();
}
}
}
class Copy4{
final static int FILE_BLKS = 5;

public static void main(String[] args){
int[] posbegin
;
int[] len;
Thread[] copiers;

if (args.length != 2){
System.out.println("Usage: java Copy4 src_file dest_file");
System.exit(0);
}

posbegin
= new int[FILE_BLKS];
len = new int[FILE_BLKS];
copiers = new Thread[FILE_BLKS];

/**
* Set block begin
positions and end positions
*/
File file = new File(args[0]);
int blkSize = (int)file.length() / FILE_BLKS;
posbegin
[0] = 0;
len[0] = blkSize;
for (int i = 1;
i< FILE_BLKS;
++i){
posbegin
= posbegin
[i-1] + blkSize;
len = blkSize;
}
len[FILE_BLKS-1] = (int)file.length() - posbegin
[FILE_BLKS-1];
/**
* Start several thread todo
the job
*/
for (int i = 0;
i< FILE_BLKS;
++i){
System.out.println("Starting thread " + i);
copiers = new Thread(new BlockCopier(args[0], args[1], posbegin
, len));
copiers.start();
}
}
}
 
顶部