编写一个文件传输的JAVA程序

来源:互联网 发布:南朝汉服淘宝 编辑:程序博客网 时间:2024/05/22 14:16

来源:http://zhidao.baidu.com/question/29588961.html 

 

如果文件太大不能一次全读入!!
JAVA里关于文件读写的有几十个类,不知道你想要如何实现,
以下是读文件的一个程序,如果有问题,发信息给我吧........

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class javaTest {
public static void main(String[] args) {
String file1=System.getProperty("user.dir")+"/1.txt";//文件,自己修改

FileInputStream myFile = null;

try {
myFile = new FileInputStream(file1); //
} catch(FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}

FileChannel myChannel = myFile.getChannel();
//这里定义缓冲区大小,每次读入字节数
ByteBuffer mybuf = ByteBuffer.allocate(1024);

try {
while(myChannel.read(mybuf) != -1) {
byte[] mybytes = mybuf.array();//读入的文件转为字节数组
mybuf.clear();
/**
* 在这里进行比较
* 可以通过字节对比
* 也可以把字节转成字符串再对比
*
*/
}
myFile.close();


}catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}

原创粉丝点击