A. 有两个名字一样的java文件,怎样比较其内容的不同地方(一个是以前写的,一个更新的程序)有什么方法
1、如果一个是本地文件,一个是svn服务器上的,可以直接使用eclipse里面svn的“比较对象”——“资源库中的最新内容”(你的应该是英文)
2、UltraEdit中“文件”——“比较文件”,选中要比较的两个文件即可。
B. 如何用java判断两个文件下的txt文件内容一致与否
请把问题描述的清楚点,“两个文件下的txt文件”这个让人费解。这么说吧,你的意思我版大概明白,也都知道该怎权么做,但是作为我的客户,我一定让你得到最满意的答案。这样,你按以下的目录填空:
两个文件名(包括路径):1、 2、
如果一致则出现什么结果:
不一致需要产生什么文件:
C. java编程:再上传文件的时候,怎样判断所上传到的文件夹里有没有跟上传文件同名的文件,如果有的话需要在
String[] fileName =zipEntryName.split("\\."); // 文件名
outPath = (descDir+"/"+zipEntryName).replaceAll("\\*", "/");//存储路径
for(int i=1;i<999999999;i++){ //
File files = new File(outPath);
if(files.exists()){
outPath = (descDir+"/"+fileName[0]+"("+i+")."+fileName[1]).replaceAll("\\*", "/");
} else{
break;
}
}
//写入文件
InputStream in = zip.getInputStream(entry);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close(); }
D. java如何快速比较两个文件是否相同
计算或SHA-1,一样的就是同一个文件
下面的代码,不需要额外使用第三方组件,且支持超大文件
//计算文件的MD5值
publicstaticStringgetFileMD5(Filefile){
if(!file.isFile()){
returnnull;
}
MessageDigestdigest=null;
FileInputStreamin=null;
bytebuffer[]=newbyte[8192];
intlen;
try{
digest=MessageDigest.getInstance("MD5");
in=newFileInputStream(file);
while((len=in.read(buffer))!=-1){
digest.update(buffer,0,len);
}
BigIntegerbigInt=newBigInteger(1,digest.digest());
returnbigInt.toString(16);
}catch(Exceptione){
e.printStackTrace();
returnnull;
}finally{
try{
in.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
//计算文件的SHA-1值
publicstaticStringgetFileSha1(Filefile){
if(!file.isFile()){
returnnull;
}
MessageDigestdigest=null;
FileInputStreamin=null;
bytebuffer[]=newbyte[8192];
intlen;
try{
digest=MessageDigest.getInstance("SHA-1");
in=newFileInputStream(file);
while((len=in.read(buffer))!=-1){
digest.update(buffer,0,len);
}
BigIntegerbigInt=newBigInteger(1,digest.digest());
returnbigInt.toString(16);
}catch(Exceptione){
e.printStackTrace();
returnnull;
}finally{
try{
in.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
E. java如何判断俩个文件是否相同的
//判断的办法是分别将两个文件读进来
//先判断着两个文件的长度,如果长度不一样,那肯定不一样
//如果两个文件的长度(大小)一样,那么就对文件中的每一个字节进行比对
//只要有一个字节不同,两个文件就不同。
//否则两个文件就是相同的
//代码如下:(JDK1.4+)(代码格式被吃掉了,请自己手工排一下)
package 兲髙亻壬鸟飞;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Answer1 {
public static void main(String[] args) {
//文件名要用绝对路径
//相对路径也可以,但是容易弄错
String fileName1 = "xxx";//写你需要判断的第一个文件的文件名
String fileName2 = "yyy";//写你需要判断的第二个文件的文件名
FileInputStream fis1 = null;
FileInputStream fis2 = null;
try {
fis1 = new FileInputStream(fileName1);
fis2 = new FileInputStream(fileName2);
int len1 = fis1.available();
int len2 = fis2.available();
if (len1 == len2) {//长度相同,则比较具体内容
//建立两个字节缓冲区
byte[] data1 = new byte[len1];
byte[] data2 = new byte[len2];
//分别将两个文件的内容读入缓冲区
fis1.read(data1);
fis2.read(data2);
//依次比较文件中的每一个字节
for (int i=0; i<len1; i++) {
//只要有一个字节不同,两个文件就不一样
if (data1[i] != data2[i]) {
System.out.println("文件内容不一样");
return;
}
}
System.out.println("两个文件完全相同");
} else {
//长度不一样,文件肯定不同
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {//关闭文件流,防止内存泄漏
if (fis1 != null) {
try {
fis1.close();
} catch (IOException e) {
//忽略
e.printStackTrace();
}
}
if (fis2 != null) {
try {
fis2.close();
} catch (IOException e) {
//忽略
e.printStackTrace();
}
}
}
}
}
F. 用Java写一个类,用来比较两个文件夹中的文件:
给你个思路(自己仔细做做,其实不难):
1、查找两个文件中所有的文件很简单,使用两个map,把文件名加上文件大小作为键,把文件所在的路径加上A或者B作为值,这样能够得到连个map,解决问题一
2、重新定义一个map,将B文件生成的map添加进新的map,然后将A文件生成的map添加进新map(新添加的时候判断map的健是不是已经存在,如果存在,将该键的值改成A文件的路径)
3、遍历新生成的map,如果map元素键对应的value中包含了B而没有包含A就说明,这个文件在B文件夹中,而没有在A文件夹中(可以使用contains这个方法)
G. 用java判断d盘下,图片名是否相同,另存为,如果相同就另存为别的名称,这个应该用java怎么写!
my Chinese input is broken....
you can use file.exists() method to judge if your image name is already existed.
here is an example from my project, using SaveDialog frame.
privatevoidsaveAs()
{
if(image!=null)
{
JFileChooserjfc=newJFileChooser(".");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
=newFileNameExtensionFilter("*.png","png");
=newFileNameExtensionFilter("*.jpg","jpg");
=newFileNameExtensionFilter("*.jpeg","jpeg");
jfc.addChoosableFileFilter(filter1);
jfc.addChoosableFileFilter(filter2);
jfc.addChoosableFileFilter(filter3);
jfc.setAcceptAllFileFilterUsed(false);
jfc.setFileFilter(filter1);
jfc.setSelectedFile(newFile("2D_Code"));
intreturnVal=jfc.showSaveDialog(MainFrame.this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{
Filefile=jfc.getSelectedFile();
Stringtmp=jfc.getFileFilter().getDescription();
StringimgType=tmp.substring(tmp.lastIndexOf(".")+1,tmp.length());
Stringfilename=file.getPath()+"."+imgType;
file=newFile(filename);
if(file.exists())
{
inti=JOptionPane.showConfirmDialog(this,"Fileexists!Overwriteit?","Attention",JOptionPane.OK_OPTION);
if(i!=JOptionPane.OK_OPTION)
return;
}
try{
ImageIO.write(image,imgType,file);
JOptionPane.showMessageDialog(this,"Successfullysaved!");
}catch(IOExceptione1){
JOptionPane.showMessageDialog(this,"Failedsaving.");
e1.printStackTrace();
}
}
}
else
{
JOptionPane.showMessageDialog(this,"Noimagegenerated!");
}
}