JAVA编写程序段

来源:百度知道 编辑:UC知道 时间:2024/05/15 14:52:09
从同个文件夹中选出两个文章进行比较,如果找不到文章或打开时出现问题则报错,比较每一行,并打印出相同行,算出相同行的行数
不是很明白
不过这题主要是如何导入两篇文章再是进行比较

如果文件太大不能一次全读入!!
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();
/**
* 在这里进行比较
* 可以通过字节对比
* 也