當前位置:編程學習大全網 - 網站源碼 - 幫忙編寫壹個文件傳輸的JAVA程序

幫忙編寫壹個文件傳輸的JAVA程序

如果文件太大不能壹次全讀入!!

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);

}

  • 上一篇:如何解析出在線視頻文件的下載地址?
  • 下一篇:釘釘測溫的原理
  • copyright 2024編程學習大全網