A. java 文本 比较
不知道你的两个文本是怎么样的
不过你可以这样
读取出文本后转成char数组
然后循环最长的那个数组,一个一个字符进行比较
如果发现不一样的,就增加到一个新的数组里
B. java输入文本框与准备好的文本比较是否一致
通过equals方式进行判断。
1、定义2个文本框和一个按钮
JTextField textField1 = new JTextField(20);//文本框1
JTextField textField2 = new JTextField(20);//文本框2
JButton button = new JButton("按钮");
2、定义按钮事件,判断文字是否一致
button.addActionListener(new buttonListenerClass());//设置按钮监听事件
class buttonListenerClass implements ActionListener{ //定义按钮监听
@Override public void actionPerformed(ActionEvent e) {
if(textField1.getText().equals(textField1.getText())){//通过equals方式比较
//文字一致
}else{
//文字不一致
}
}}
C. 如何用JAVA技术编写一个两文件内容比较的程序
1)程序运行开始的时候,提示输出两个文件的路径;
2)程序一行一行比较,当其中有一行不一样的时候,提示文件内容不同的行数以及内容;
3)如果文件内容一样,提示用户内容一样。
package buffered;
import java.io.*;
import java.lang.String;
import java.util.Scanner;
public class BufferedTest
{
public static void main(String[] args){
try{Scanner readr=new Scanner(System.in); <br/>System.out.println("输入第一个文件的路径及文件名:"); <br/>String file1=readr.next(); <br/>System.out.println("输入第二个文件的路径及文件名:"); <br/>String file2=readr.next(); <br/>FileReader inOne1=new FileReader(file1); <br/>BufferedReader inTwo1=new BufferedReader(inOne1); <br/>String s1=null; <br/>int i=0;//行数 <br/>boolean b=true;//检察是行数 <br/>FileReader inOne2=new FileReader(file2); <br/>BufferedReader inTwo2=new BufferedReader(inOne2); <br/>String s2=null; <br/>//文件内容对比 <br/>while((s1=inTwo1.readLine())!=null){ <br/>i++; <br/>while((s2=inTwo2.readLine())!=null){ <br/><br/>if(s1.equals(s2)!=true){ <br/>System.out.println("第"+i+"行:"+s1+"和"+s2+"内容不一样"); <br/>b=false; <br/>}
break;
}
}
if(b){System.out.println("文件内容一样");}
}
catch(IOException e){
System.out.println(e);
}
}
}
D. java学到哪才可以检测两个文本的相似度
学到IO流可以比较文本了, 不过用IO比较文本比较麻烦, 而且效率低
如果要效率高的话, 就需要学
linux + thread + progress
这样子可以通过java调用linux命令
linux中有一个comm -12命令来实现比较两个文件的共同数据, 生成一个新文本。 拿到新文本的大小/旧文本的大小, 旧可以粗略计算出相似度, 而且效率极其高
如果文本有上百万行数据的话, 用IO可能要十几个小时才有结果, 但是linux命令只需要1分钟左右就可以出结果
E. Java 读取一个文本,然后比较每一行
我的理解:
如果hello.java中有空行(不包括任何字符,只是用回车或者换行符进行分割的空白行),读文件时,将该行忽略;如果不是空行,则读出该行。
如果是这样的话,实现如下:
package _file_test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) {
String filePathChar = "E:\\my doc\\note\\testchar.txt";
Test1 t1 = new Test1();
t1.readFileByLine(filePathChar);
}
/**
* 以行为单位读取文件(文本文件)
* @param filePath
*/
public void readFileByLine(String filePath) {
File file = new File(filePath);
BufferedReader bd = null;
try {
System.out.println("按行读取文件,一次读取一行:");
System.out.println("\n==========start reading==========");
bd = new BufferedReader(new FileReader(file));
String temp = "";
//int line = 1;
while ((temp = bd.readLine()) != null) {
if(temp.length() > 0) {
System.out.println(temp);
}
//++line;
}
System.out.println("\n==========end==========");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bd != null) bd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
F. JAVA实现如何比较两个文档之间的异同点
楼上回答驴唇不对马嘴。
这个文件比较功能,我做过比较txt简单文本文件的。office文件没比过,说下想法,希望能对你有帮助
打开要读取的两份文件,依次读取A所有行数,将每一行的数据以字符串的形式存储在一个字符串数组里,假如是 StringA[] ,同时也一样处理 B文件
分别从开始遍历两个 字符串数组,比较 相似(相同) 字符百分比。当然有时候可能A文件第10行是一个空行,但是其下面的所有数据都和B文件一样,所以这个遍历对比不是门当户对的 行行相比,而是 一次遍历多对多的比较(但是仍要考虑顺序,比如A第10行和B第15行相同,但是A第12行又和B第10行相同,那么这一个范围段都是不匹配片断),找出相似度最高的。
希望能对你有帮助吧
G. 文本比较有哪些算法
用java比较两个文本文件的不同
RangeDifferencer
public class RangeDifferencer {
private static final RangeDifference[] EMPTY_RESULT= new RangeDifference[0];
/* (non Javadoc)
* Cannot be instantiated!
*/
private RangeDifferencer() {
// nothing to do
}
/**
* Finds the differences between two <code>IRangeComparator</code>s.
* The differences are returned as an array of <code>RangeDifference</code>s.
* If no differences are detected an empty array is returned.
*
* @param left the left range comparator
* @param right the right range comparator
* @return an array of range differences, or an empty array if no differences were found
*/
public static RangeDifference[] findDifferences(IRangeComparator left, IRangeComparator right) {
int rightSize= right.getRangeCount();
int leftSize= left.getRangeCount();
//
// Differences matrix:
// only the last d of each diagonal is stored, i.e., lastDiagonal[k] = row of d
//
int diagLen= 2 * Math.max(rightSize, leftSize); // bound on the size of edit script
int maxDiagonal= diagLen;
int lastDiagonal[]= new int[diagLen + 1]; // the row containing the last d
// on diagonal k (lastDiagonal[k] = row)
int origin= diagLen / 2; // origin of diagonal 0
// script corresponding to d[k]
LinkedRangeDifference script[]= new LinkedRangeDifference[diagLen + 1];
int row, col;
// find common prefix
for (row= 0; row < rightSize && row < leftSize && rangesEqual(right, row, left, row) == true;)
row++;
lastDiagonal[origin]= row;
script[origin]= null;
int lower= (row == rightSize) ? origin + 1 : origin - 1;
int upper= (row == leftSize) ? origin - 1 : origin + 1;
if (lower > upper)
return EMPTY_RESULT;
//System.out.println("findDifferences: " + maxDiagonal + " " + lower + " " + upper);
// for each value of the edit distance
for (int d= 1; d <= maxDiagonal; ++d) { // d is the current edit distance
if (right.skipRangeComparison(d, maxDiagonal, left))
return EMPTY_RESULT; // should be something we already found
// for each relevant diagonal (-d, -d+2 ..., d-2, d)
for (int k= lower; k <= upper; k += 2) { // k is the current diagonal
LinkedRangeDifference edit;
if (k == origin - d || k != origin + d && lastDiagonal[k + 1] >= lastDiagonal[k - 1]) {
//
// move down
//
row= lastDiagonal[k + 1] + 1;
edit= new LinkedRangeDifference(script[k + 1], LinkedRangeDifference.DELETE);
} else {
//
// move right
//
row= lastDiagonal[k - 1];
edit= new LinkedRangeDifference(script[k - 1], LinkedRangeDifference.INSERT);
}
col= row + k - origin;
edit.fRightStart= row;
edit.fLeftStart= col;
//Assert.isTrue(k >= 0 && k <= maxDiagonal);
script[k]= edit;
// slide down the diagonal as far as possible
while (row < rightSize && col < leftSize && rangesEqual(right, row, left, col) == true) {
++row;
++col;
}
//Assert.isTrue(k >= 0 && k <= maxDiagonal); // Unreasonable value for diagonal index
lastDiagonal[k]= row;
if (row == rightSize && col == leftSize) {
//showScript(script[k], right, left);
return createDifferencesRanges(script[k]);
}
if (row == rightSize)
lower= k + 2;
if (col == leftSize)
upper= k - 2;
}
--lower;
++upper;
}
// too many differences
//Assert.isTrue(false);
return null;
}
/**
* Finds the differences among two <code>IRangeComparator</code>s.
* In contrast to <code>findDifferences</code>, the result
* contains <code>RangeDifference</code> elements for non-differing ranges too.
*
* @param left the left range comparator
* @param right the right range comparator
* @return an array of range differences
*/
public static RangeDifference[] findRanges(IRangeComparator left, IRangeComparator right) {
RangeDifference[] in= findDifferences(left, right);
List out= new ArrayList();
RangeDifference rd;
int mstart= 0;
int ystart= 0;
for (int i= 0; i < in.length; i++) {
RangeDifference es= in[i];
rd= new RangeDifference(RangeDifference.NOCHANGE, mstart, es.rightStart() - mstart, ystart, es.leftStart() - ystart);
if (rd.maxLength() != 0)
out.add(rd);
out.add(es);
mstart= es.rightEnd();
ystart= es.leftEnd();
}
rd= new RangeDifference(RangeDifference.NOCHANGE, mstart, right.getRangeCount() - mstart, ystart, left.getRangeCount() - ystart);
if (rd.maxLength() > 0)
out.add(rd);
return (RangeDifference[]) out.toArray(EMPTY_RESULT);
}
//---- private methods
/*
* Creates a Vector of DifferencesRanges out of the LinkedRangeDifference.
* It coalesces adjacent changes.
* In addition, indices are changed such that the ranges are 1) open, i.e,
* the end of the range is not included, and 2) are zero based.
*/
private static RangeDifference[] createDifferencesRanges(LinkedRangeDifference start) {
LinkedRangeDifference ep= reverseDifferences(start);
ArrayList result= new ArrayList();
RangeDifference es= null;
while (ep != null) {
es= new RangeDifference(RangeDifference.CHANGE);
if (ep.isInsert()) {
es.fRightStart= ep.fRightStart + 1;
es.fLeftStart= ep.fLeftStart;
RangeDifference b= ep;
do {
ep= ep.getNext();
es.fLeftLength++;
} while (ep != null && ep.isInsert() && ep.fRightStart == b.fRightStart);
} else {
es.fRightStart= ep.fRightStart;
es.fLeftStart= ep.fLeftStart;
RangeDifference a= ep;
//
// deleted lines
//
do {
a= ep;
ep= ep.getNext();
es.fRightLength++;
} while (ep != null && ep.isDelete() && ep.fRightStart == a.fRightStart + 1);
boolean change= (ep != null && ep.isInsert() && ep.fRightStart == a.fRightStart);
if (change) {
RangeDifference b= ep;
//
// replacement lines
//
do {
ep= ep.getNext();
es.fLeftLength++;
} while (ep != null && ep.isInsert() && ep.fRightStart == b.fRightStart);
} else {
es.fLeftLength= 0;
}
es.fLeftStart++; // meaning of range changes from "insert after", to "replace with"
}
//
// the script commands are 1 based, subtract one to make them zero based
//
es.fRightStart--;
es.fLeftStart--;
result.add(es);
}
return (RangeDifference[]) result.toArray(EMPTY_RESULT);
}
/*
* Tests if two ranges are equal
*/
private static boolean rangesEqual(IRangeComparator a, int ai, IRangeComparator b, int bi) {
return a.rangesEqual(ai, b, bi);
}
/*
* Tests whether <code>right</code> and <code>left</code> changed in the same way
*/
private static boolean rangeSpansEqual(IRangeComparator right, int rightStart, int rightLen, IRangeComparator left, int leftStart, int leftLen) {
if (rightLen == leftLen) {
int i= 0;
for (i= 0; i < rightLen; i++) {
if (!rangesEqual(right, rightStart + i, left, leftStart + i))
break;
}
if (i == rightLen)
return true;
}
return false;
}
/*
* Reverses the range differences
*/
private static LinkedRangeDifference reverseDifferences(LinkedRangeDifference start) {
LinkedRangeDifference ep, behind, ahead;
ahead= start;
ep= null;
while (ahead != null) {
behind= ep;
ep= ahead;
ahead= ahead.getNext();
ep.setNext(behind);
}
return ep;
}
}
下面是一段关于如何使用这些类的简单的测试代码
public class RangeDifferencerTest extends TestCase {
InputStream left = null;
InputStream right = null;
/**
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
String file1 = "d:/temp/1.txt";
String file2 = "d:/temp/2.txt";
left = new FileInputStream(new File(file1));
right = new FileInputStream(new File(file2));
super.setUp();
}
/**
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
left.close();
right.close();
super.tearDown();
}
public static void main(String[] args) {
}
/*
* Test method for 'com.greatroad.smbnm.compare.RangeDifferencer.findDifferences(IRangeComparator, IRangeComparator)'
*/
public void testFindDifferences() {
try {
RangeDifference[] rds = RangeDifferencer.findRanges(new LineComparator(left,"GBK"),new LineComparator(right,"GBK"));
if(rds != null ){
for(int i=0; i<rds.length; i++){
RangeDifference rd = rds[i];
int length = rd.leftLength();
System.out.println(
"kind = "+rd.kind()
+",left["+rd.leftStart()+"-"+rd.leftEnd()
+"],right["+rd.rightStart()+"-"+rd.rightEnd()+"]");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
H. java实现两个文本比较,其中每个文本中有两段内容完全一致。
import java.io.*;
public class T
{
public static void main(String[] args) throws Exception
{
String str1="ijh www bian 编程 ssss java程序设计 jkiop eekk";
String str2="ooo 编程 pwidk java程序设计";
String regex="\\s+";
String[] strs1=str1.split(regex),strs2=str2.split(regex);
System.out.println(String.format("字符串\"%1$s\"与字符串\"%2$s\"具有以下相同的子串:",str1,str2));
for(int i=0;i<strs1.length;i++)
{
for(int j=0;j<strs2.length;j++)
{
if(strs1[i].equals(strs2[j]))
{
System.out.println(strs1[i]);
strs2[j]=null;
}
}
}
}
}
I. java中文本框输入字符串怎么与数据库表中的字符串比较
可以用simpleformatdate把时间format成date,然后用stmt.setdate(1,date)。这样就万事ok了。
J. java如何调文本比较工具功能,实现对俩个文件的检测比较
那首先要看你使用的是什么比较工具,其次是怎么打开它,然后怎么将文面路径通过接口传给他