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