文件对比

file-report layout:side-by-side &
options:display-mismatches &
output-to:%3 %1 %2

以上命令保存bc.txt
BCompare.exe @C:\bc.txt Mine\1.class Theirs\2.class Report.txt


--------------------------------------

*

  * 读取char

  */

  private String readtxt() throws IOException{

  BufferedReader br=new BufferedReader(new FileReader("d:/sql.txt"));

  String str="";

  String r=br.readLine();

  while(r!=null){

  str+=r;

  r=br.readLine();

  }

  return str;

  }

  Java代码

  /*

  * 读取char

  */

  private String readtxt() throws IOException{

  BufferedReader br=new BufferedReader(new FileReader("d:/sql.txt"));

  String str="";

  String r=br.readLine();

  while(r!=null){

  str+=r;

  r=br.readLine();

  }

  return str;

  }

  Java代码

  /*

  * 读取char

  */

  private String readtxt2() throws IOException{

  String str="";

  FileReader fr=new FileReader("d:/sql.txt");

  char[] chars=new char[1024];

  int b=0;

  while((b=fr.read(chars))!=-1){

  str+=String.valueOf(chars);

  }

  return str;

  }

  Java代码

  /*

  * 读取char

  */

  private String readtxt2() throws IOException{

  String str="";

  FileReader fr=new FileReader("d:/sql.txt");

  char[] chars=new char[1024];

  int b=0;

  while((b=fr.read(chars))!=-1){

  str+=String.valueOf(chars);

  }

  return str;

  }

  Java代码

  /*

  * 读取bytes

  */

  private Byte[] readtxt3() throws IOException{

  InputStream input=new FileInputStream("d:/sql.txt");

  byte[] b=new byte[1024];

  ArrayList<Byte> lsbytes=new ArrayList<Byte>();

  int n=0;

  while((n=input.read(b))!=-1){

  for(int i=0;i<n;i++){

  lsbytes.add(b[i]);

  }

  }

  return (Byte[])(lsbytes.toArray());

  }

  Java代码

  /*

  * 读取bytes

  */

  private Byte[] readtxt3() throws IOException{

  InputStream input=new FileInputStream("d:/sql.txt");

  byte[] b=new byte[1024];

  ArrayList<Byte> lsbytes=new ArrayList<Byte>();

  int n=0;

  while((n=input.read(b))!=-1){

  for(int i=0;i<n;i++){

  lsbytes.add(b[i]);

  }

  }

  return (Byte[])(lsbytes.toArray());

  }
---------------------------------------
File file = new File("F:\\ip.txt");
Reader reader = null;
try {
reader = new FileReader(file);
char[] charArray = new char[100];
int i = 0;
while ((i = reader.read(charArray, 0, charArray.length)) != -1) {
System.out.print(new String(charArray, 0, i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} | 回答者: chen320320 | 三级 | 2011-8-3 10:04 
          
public class ReadFromFile {
    /**
     * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
     */
    public static void readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        try {
            System.out.println("以字节为单位读取文件内容,一次读一个字节:");
            // 一次读一个字节
            in = new FileInputStream(file);
            int tempbyte;
            while ((tempbyte = in.read()) != -1) {
                System.out.write(tempbyte);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        try {
            System.out.println("以字节为单位读取文件内容,一次读多个字节:");
            // 一次读多个字节
            byte[] tempbytes = new byte[100];
            int byteread = 0;
            in = new FileInputStream(fileName);
            ReadFromFile.showAvailableBytes(in);
            // 读入多个字节到字节数组中,byteread为一次读入的字节数
            while ((byteread = in.read(tempbytes)) != -1) {
                System.out.write(tempbytes, 0, byteread);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 以字符为单位读取文件,常用于读文本,数字等类型的文件
     */
    public static void readFileByChars(String fileName) {
        File file = new File(fileName);
        Reader reader = null;
        try {
            System.out.println("以字符为单位读取文件内容,一次读一个字节:");
            // 一次读一个字符
            reader = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
                // 但如果这两个字符分开显示时,会换两次行。
                // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
                if (((char) tempchar) != '\r') {
                    System.out.print((char) tempchar);
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            System.out.println("以字符为单位读取文件内容,一次读多个字节:");
            // 一次读多个字符
            char[] tempchars = new char[30];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(fileName));
            // 读入多个字符到字符数组中,charread为一次读取字符数
            while ((charread = reader.read(tempchars)) != -1) {
                // 同样屏蔽掉\r不显示
                if ((charread == tempchars.length)
                        && (tempchars[tempchars.length - 1] != '\r')) {
                    System.out.print(tempchars);
                } else {
                    for (int i = 0; i < charread; i++) {
                        if (tempchars[i] == '\r') {
                            continue;
                        } else {
                            System.out.print(tempchars[i]);
                        }
                    }
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 以行为单位读取文件,常用于读面向行的格式化文件
     */
    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
你参考下把 回答者: vltava︶︺︸ | 六级 | 2011-8-3 10:16 
          
jiu就用读取文件就可以了 回答者: flxiaoqi | 一级 | 2011-8-5 11:59 
          
你这里的,张三,李四,王五  间隔是全角逗号,注意一下,根据需要换程序里的内容
我把里面的换成了半角的来作的。

package org.app.demo;

import java.io.BufferedReader;
import java.io.FileReader;

public class Test {

public static void main(String[] args) throws Exception {
exexute();
}

public static void exexute() throws Exception {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("F:\\test.txt");
br = new BufferedReader(fr);
while (br.ready()) {
String line = br.readLine().trim();
String[] group = line.split("\",\"");
String[] names = group[0].split(",");
for (int i = 0; i < names.length; i++) {
String name = names[i];
if (i == 0) {
name = names[i] + "\"";
} else {
name = "\"" + names[i] + "\"";

}
System.out.println(name + ",\"" + group[1] + ",\""
+ group[2]);

}
}
} catch (Exception e) {

} finally {
if (br != null) {
br.close();
}
if (fr != null) {
br.close();
}
}
}
}
----------------------------------------------------------------------------------------------------
结果


----------------------------------------------



下边是在项目用到的压缩解压class ,粘出来,分享。



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.lang.StringUtils;

public class ZipTool {
    private static final int BUFFER = 2048;
    private int level;

    public ZipTool() {
        level = 0;
    }

    /**
     * setLevel
     * @param level int
     */
    public void setLevel(int level) {
        this.level = level;

    }

    /**
     * zip a File or Directory
     * @param inputFile File
     * @param outputFile File
     * @throws ZipException
     */
    public void zipFile(File inputFile, File outputFile) throws ZipException {
        try {
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(
                outputFile), BUFFER);
            ZipOutputStream out = new ZipOutputStream(bout);
            zip(out, inputFile, inputFile.getName());
            out.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }

    /**
     * zip some Files
     * @param inputFiles File[]
     * @param outputFile File
     * @throws ZipException
     */
    public void zipFiles(File[] inputFiles, File outputFile) throws ZipException {
        try {
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outputFile),
                BUFFER);
            ZipOutputStream out = new ZipOutputStream(bout);
            for (int i = 0; i < inputFiles.length; i++) {
                zip(out, inputFiles[i], inputFiles[i].getName());
            }
            out.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }

    /**
     * unzip a File
     *
     * @param inputFile File
     * @param outputFile File
     * @throws ZipException
     */
    public void unZipFile(File inputFile, File outputFile) throws ZipException {
        try {
            FileInputStream tin = new FileInputStream(inputFile);
            CheckedInputStream cin = new CheckedInputStream(tin, new CRC32());
            BufferedInputStream bufferIn = new BufferedInputStream(cin, BUFFER);
            ZipInputStream in = new ZipInputStream(bufferIn);
            ZipEntry z = in.getNextEntry();

            while (z != null) {
                String name = z.getName();
                if (z.isDirectory()) {
                    File f = new File(outputFile.getPath() + File.separator + name);
                    f.mkdir();
                }
                else {
                    File f = new File(outputFile.getPath() + File.separator + name);
                    f.createNewFile();
                    FileOutputStream out = new FileOutputStream(f);
                    byte data[] = new byte[BUFFER];
                    int b;

                    while ( (b = in.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, b);
                    }
                    out.close();
                }
                z = in.getNextEntry();
            }

            in.close();
        }

        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }

    }

    private void zip(ZipOutputStream out, File f, String base) throws
        FileNotFoundException, ZipException {
        if (level != 0) {
            out.setLevel(level);
        }
        if (f.isDirectory()) {
            zipDirectory(out, f, base);
        }
        else {
            if (StringUtils.isEmpty(base)) {
                base = f.getName();
            }
            zipLeapFile(out, f, base);
        }

    }

    private void zipDirectory(ZipOutputStream out, File f, String base) throws
        FileNotFoundException, ZipException {
        File[] files = f.listFiles();
        if (level != 0) {
            out.setLevel(level);
        }
        try {
            out.putNextEntry(new ZipEntry(base + "/"));
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
        if (StringUtils.isEmpty(base)) {
            base = new String();
        }
        else {
            base = base + "/";
        }

        for (int i = 0; i < files.length; i++) {
            zip(out, files[i], base + files[i].getName());
        }

    }

    private void zipLeapFile(ZipOutputStream out, File f, String base) throws
        FileNotFoundException, ZipException {
        if (level != 0) {
            out.setLevel(level);
        }
        try {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            BufferedInputStream bufferIn = new BufferedInputStream(in, BUFFER);
            byte[] data = new byte[BUFFER];
            int b;
            while ( (b = bufferIn.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, b);
            }
            bufferIn.close();
        }
        catch (IOException ex) {
            throw new ZipException(ex.getMessage());
        }
    }
}
-----------------------------------------------------------

在ant脚本中对外部ant任务的调用,在多项目管理中特别有用。两种方法总结一下:
使用antfile、使用exec
一:使用antfile
    <target name="copy_lib" description="Copy library files from  project1 to project2">
          <ant antfile="build.xml"
              dir="${project1dir}"
              inheritall="false"
              inheritrefs="false"
              target="copy_to_project2_lib"
          />
    </target>
antfile表示子项目的构建文件。
dir表示构建文件所再的目录,缺省为当前目录。
inheritall表示父项目的所有属性在子项目中都可使用,并覆盖子项目中的同名属性。缺省为true。
inheritrefs表示父项目中的所有引用在子项目中都可以使用,并且不覆盖子项目中的同名引用。缺省为false。
如果在ant任务中显示的定义引用,如上例<reference refid="filter.set">则该引用将会覆盖子项目中的同名引用。 
target表示所要运行的子项目中的target,如果不写则为缺省target。
二:使用exec
    <target name="copy_lib" description="Copy library files from  project1 to project2">
        <exec executable="cmd.exe">
            <arg line="/c &quot;cd ..\project1 &amp;&amp; ant copy_to_project2_lib &quot; "/>
    </exec>
    </target>
翻译为命令行就是:cmd.exe /c "cd ..\project && ant copy_to_project2_lib" 
意思是直接调用系统控制台,先执行cd命令,再执行ant脚本指定任务,/c 表示执行后续 String 指定的命令,然后停止。
arg中的值不能直接使用双引号, 否则会出错. 请使用xml中双引号的描述符 &quot;代替
http://sanmaoyouxiao.iteye.com/blog/82888
http://snowolf.iteye.com/blog/642492


T t = new T();
t.setA(new String[]{"123","32"});
   Field f[] = t.getClass().getDeclaredFields();
   for (int i=0;i<f.length ; i++)
   {
   System.out.println(f[i].getName());
   String name = f[i].getName();
   name = "get"+ name.substring(0,1).toUpperCase()+name.substring(1);
   Method met = t.getClass().getMethod(name, null);
   Object o = met.invoke(t, null);
  Class c =  o.getClass().getComponentType(); 
 
  Object a =   Array.newInstance(c,Array.getLength(o) );
   
   System.out.println("----"+c.getName());
  
  
  
   }

你可能感兴趣的:(文件)