Java文件操作大全

ne3g 9年前發布 | 20K 次閱讀 Java

1.創建文件夾
//import java.io.*;
File myFolderPath = new File(%%1);
try {
if (!myFolderPath.exists())
myFolderPath.mkdir();
}
catch (IOException e) {
System.err.println("新建目錄操作出錯");
}

2.創建文件 //import java.io.*; File myFilePath = new File(%%1); try { if (!myFilePath.exists()) myFilePath.createNewFile(); FileWriter resultFile = new FileWriter(myFilePath); PrintWriter myFile = new PrintWriter(resultFile); myFile.println(%%2); myFile.flush(); resultFile.close(); } catch (IOException e) { System.err.println("新建文件操作出錯"); }

3.刪除文件 //import java.io.*; File myDelFile = new File(%%1); try { if(myDelFile.delete()) { %%2 } } catch (IOException e) { System.err.println("刪除文件操作出錯"); }

4.刪除文件夾 / import java.io.; import java.util.;/ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { fileList.add(files[i]); } } for (File f : fileList) { f.delete(); } } folderList = new LinkedList<String>(); folderList.add(%%1); while (folderList.size() > 0) { File file = new File((String)folderList.getLast()); if (file.delete()) folderList.removeLast(); else { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { folderList.add(files[i].getPath()); } } }

5.刪除一個文件下夾所有的文件夾 / import java.io.; private static LinkedList<String> folderList=null; */ File delfile=new File(%%1); File[] files=delfile.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ if(!files[i].delete()){ folderList = new LinkedList<String>(); folderList.add(files[i]); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) folderList.add(files[i].getPath()); else fileList.add(files[i]); } for (File f : fileList) f.delete(); } folderList = new LinkedList<String>(); folderList.add(files[i]); while (folderList.size() > 0) { File file = new File((String)folderList.getLast()); if (file.delete()) folderList.removeLast(); else { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { folderList.add(files[i].getPath()); } } } } } }

6.清空文件夾 //import java.io.*; File delfilefolder=new File(%%1); if (!delfilefolder.exists() && !delfilefolder.delete()){ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(delfilefolder.getAbsolutePath()); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) folderList.add(files[i].getPath()); else fileList.add(files[i]); } for (File f : fileList) f.delete(); } folderList = new LinkedList<String>(); folderList.add(delfilefolder.getAbsolutePath()); while (folderList.size() > 0) { File file = new File((String)folderList.getLast()); if (file.delete()) folderList.removeLast(); else { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { folderList.add(files[i].getPath()); } } } } delfilefolder.mkdir();

7.讀取文件 //import java.io.*; 7.1.操作系統默認編碼 FileReader fr=new FileReader(%%1); BufferedReader br = new BufferedReader(); String %%2=nul; try { while ((%%2 = reader.readLine()) != null) { %%3 } } catch (IOException e) { e.printStackTrace(); } finally{ try { br.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } }

7.2.UTF-8編碼 FileInputStream fis=new FileInputStream(new File(%%1)); InputStreamReader read = new InputStreamReader(fis,"UTF-8"); BufferedReader reader=new BufferedReader(read); String %%2=null; try { while ((%%2 = reader.readLine()) != null) { %%3 } } catch (IOException e) { e.printStackTrace(); } finally{ try { br.close(); read.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } }

7.3.分塊讀取 FileInputStream fis=new FileInputStream(new File(%%1)); byte[] buffer=new byte[10240]; try { int byteread; while ((byteread=fis.read(buffer)) != -1) { String %%2=new String(buffer); %%3 } } catch (IOException e) { e.printStackTrace(); } finally{ try { read.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } }

8.寫入文件 //import java.io.*; 8.1.操作系統默認編碼 try { FileWriter fw = new FileWriter(%%1); fw.write(%%2); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); }

8.2.UTF-8編碼 try { OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(%%1),"UTF-8"); out.write(sb.toString()); out.flush(); out.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); }

9.寫入隨機文件 //import java.io.*; try { RandomAcessFile logFile=new RandomAcessFile(%%1,"rw"); long lg=logFile.length(); logFile.seek(%%2); logFile.writeByte(%%3); }catch(IOException ioe){ System.out.println("無法寫入文件:"+ioe.getMessage()); }

10.讀取文件屬性 //import java.io.*; // 文件屬性的取得 File af = new File(%%1); if (af.exists()) { System.out.println(f.getName() + "的屬性如下: 文件長度為:" + f.length()); System.out.println(f.isFile() ? "是文件" : "不是文件"); System.out.println(f.isDirectory() ? "是目錄" : "不是目錄"); System.out.println(f.canRead() ? "可讀取" : "不"); System.out.println(f.canWrite() ? "是隱藏文件" : ""); System.out.println("文件夾的最后修改日期為:" + new Date(f.lastModified())); } else { System.out.println(f.getName() + "的屬性如下:"); System.out.println(f.isFile() ? "是文件" : "不是文件"); System.out.println(f.isDirectory() ? "是目錄" : "不是目錄"); System.out.println(f.canRead() ? "可讀取" : "不"); System.out.println(f.canWrite() ? "是隱藏文件" : ""); System.out.println("文件的最后修改日期為:" + new Date(f.lastModified())); } if(f.canRead()){ %%2 } if(f.canWrite()){ %%3 }

11.寫入屬性 //import java.io.*; File filereadonly=new File(%%1); try { boolean %%2=filereadonly.setReadOnly(); } catch (IOException e) { System.err.println("拒絕寫訪問:"+e.printStackTrace()); }

12.枚舉一個文件夾中的所有文件 / import java.io.; import java.util.;/ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); List<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) folderList.add(files[i].getPath()); else fileList.add(files[i]); } for (File f : fileList) { %%2=f.getAbsoluteFile(); %%3 } }

13.復制文件夾 / import java.io.; import java.util.;/ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); LinkedList<String> folderList2 = new LinkedList<String>(); folderList2.add(%%2+ %%1.substring(%%1.lastIndexOf("\"))); while (folderList.size() > 0) { (new File(folderList2.peek())).mkdirs(); // 如果文件夾不存在 則建立新文件夾 File folders = new File(folderList.peek()); String[] file = folders.list(); File temp = null; try { for (int i = 0; i < file.length; i++) { if (folderList.peek().endsWith(File.separator)) temp = new File(folderList.peek() + File.separator

  • file[i]); else temp = new File(folderList.peek() + File.separator
  • file[i]); if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(new File( folderList2.peek() ,temp.getName().toString())); byte[] b = new byte[10240]; int len; while ((len = input.read(b)) != -1) output.write(b, 0, len); output.flush(); output.close(); input.close(); } else if (temp.isDirectory()) {// 如果是子文件夾 for (File f : temp.listFiles()) { if (f.isDirectory()) { folderList.add(f.getPath()); folderList2.add(folderList2.peek()
  • File.separator + f.getName()); } else if(f.isFile()) { FileInputStream input = new FileInputStream(f); FileOutputStream output = new FileOutputStream(folderList2.peek()+File.separator+temp.getName()+File.separator+ f.getName()); byte[] b = new byte[10240]; int len; while ((len = input.read(b)) != -1) output.write(b, 0, len); output.flush(); output.close(); input.close(); } } } } } catch (IOException e) { System.err.println("復制整個文件夾內容操作出錯"); } folderList.removeFirst(); folderList2.removeFirst(); }

14.復制一個目錄下所有的文件夾到另一個文件夾下 / import java.io.; import java.util.;/ File copyfolders=new File(%%1); File[] copyfoldersList=copyfolders.listFiles(); for(int k=0;k<copyfoldersList.length;k++){ if(copyfoldersList[k].isDirectory()){ List<String>folderList=new ArrayList<String>(); folderList.add(copyfoldersList[k].getPath()); List<String>folderList2=new ArrayList<String>(); folderList2.add(%%2+"/"+copyfoldersList[k].getName()); for(int j=0;j<folderList.size();j++){ (new File(folderList2.get(j))).mkdirs(); //如果文件夾不存在 則建立新文件夾 File folders=new File(folderList.get(j)); String[] file=folders.list(); File temp=null; try { for (int i = 0; i < file.length; i++) { if(folderList.get(j).endsWith(File.separator)) temp=new File(folderList.get(j),file[i]); else temp=new File(folderList.get(j),file[i]); FileInputStream input = new FileInputStream(temp); if(temp.isFile()){ FileOutputStream output = new FileOutputStream(new File(folderList2.get(j) ,temp.getName()).toString())); byte[] b = new byte[10240]; while ( (int len = input.read(b)) != -1) output.write(b, 0, len); output.flush(); output.close(); input.close(); } else if(temp.isDirectory()){//如果是子文件夾 for (File f : temp.listFiles()) { if (f.isDirectory()) { folderList.add(f.getPath()); folderList2.add(folderList2.peek()

  • File.separator + f.getName()); } else if(f.isFile()) { FileInputStream input = new FileInputStream(f); FileOutputStream output = new FileOutputStream(folderList2.peek()+File.separator+temp.getName()+File.separator+ f.getName()); byte[] b = new byte[10240]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } } } } } } catch (IOException e) { System.err.println("復制整個文件夾內容操作出錯"); } } } }

15.移動文件夾 / import java.io.; import java.util.;/ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); LinkedList<String> folderList2 = new LinkedList<String>(); folderList2.add(%%2 + %%1.substring(%%1.lastIndexOf("\"))); while (folderList.size() > 0) { (new File(folderList2.peek())).mkdirs(); // 如果文件夾不存在 則建立新文件夾 File folders = new File(folderList.peek()); String[] file = folders.list(); File temp = null; try { for (int i = 0; i < file.length; i++) { if (folderList.peek().endsWith(File.separator)) { temp = new File(folderList.peek() , file[i]); } else { temp = new File(folderList.peek() ,file[i]); } if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream( folderList2.peek() + File.separator

  • (temp.getName()).toString()); byte[] b = new byte[10240]; while ((int len = input.read(b)) != -1) output.write(b, 0, len); output.flush(); output.close(); input.close(); if (!temp.delete()) //刪除單個文件操作出錯 } else if (temp.isDirectory()) {// 如果是子文件夾 for (File f : temp.listFiles()) { if (f.isDirectory()) { folderList.add(f.getPath()); folderList2.add(folderList2.peek()
  • File.separator + f.getName()); } else if (f.isFile()) { FileInputStream input = new FileInputStream(f); FileOutputStream output = new FileOutputStream(folderList2.peek()+File.separator+temp.getName()+File.separator+ f.getName()); byte[] b = new byte[10240]; while ((int len = input.read(b)) != -1) output.write(b, 0, len); output.flush(); output.close(); input.close(); if (!temp.delete()) //刪除單個文件操作出錯 } } } } } catch (IOException e) { //復制整個文件夾內容操作出錯 e.printStackTrace(); } folderList.removeFirst(); folderList2.removeFirst();

} File f = new File(%%1); if (!f.delete()) { for (File file : f.listFiles()) { if (file.list().length == 0) file.delete(); } }

16.移動一個目錄下所有的文件夾到另一個目錄下 / import java.io.; import java.util.;/ File movefolders=new File(%%1); File[] movefoldersList=movefolders.listFiles(); for(int k=0;k<movefoldersList.length;k++){ if(movefoldersList[k].isDirectory()){ List<String>folderList=new ArrayList<String>(); folderList.add(movefoldersList[k].getPath()); List<String>folderList2=new ArrayList<String>(); folderList2.add(%%2+File.separator+movefoldersList[k].getName()); for(int j=0;j<folderList.size();j++){ (new File(folderList2.get(j))).mkdirs(); //如果文件夾不存在 則建立新文件夾 File folders=new File(folderList.get(j)); String[] file=folders.list(); File temp=null; try { for (int i = 0; i < file.length; i++) { if(folderList.get(j).endsWith(File.separator)) temp=new File(folderList.get(j),file[i]); else temp=new File(folderList.get(j),file[i]); FileInputStream input = new FileInputStream(temp); if(temp.isFile()){ FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(new File(folderList2.get(j),temp.getName().toString())); byte[] b = new byte[10240]; while ( (int len = input.read(b)) != -1) output.write(b, 0, len); output.flush(); output.close(); input.close(); temp.delete(); } else if(temp.isDirectory()){//如果是子文件夾 for (File f : temp.listFiles()) { if (f.isDirectory()) { folderList.add(f.getPath()); folderList2.add(folderList2.peek()

  • File.separator + f.getName()); } else if (f.isFile()) { FileInputStream input = new FileInputStream(f); FileOutputStream output = new FileOutputStream(folderList2.peek()+File.separator+temp.getName()+File.separator+ f.getName()); byte[] b = new byte[10240]; while ((int len = input.read(b)) != -1) output.write(b, 0, len); output.flush(); output.close(); input.close(); if (!temp.delete()) //刪除單個文件操作出錯 } } } } catch (IOException e) { //復制整個文件夾內容操作出錯 e.printStackTrace(); } } movefoldersList[k].delete(); } }

17.以一個文件夾的框架在另一個目錄創建文件夾和空文件 / import java.io.; import java.util.;/ boolean b=false;//不創建空文件 List<String>folderList=new ArrayList<String>(); folderList.add(%%1); List<String>folderList2=new ArrayList<String>(); folderList2.add(%%2); for(int j=0;j<folderList.size();j++){ (new File(folderList2.get(j))).mkdirs(); //如果文件夾不存在 則建立新文件夾 File folders=new File(folderList.get(j)); String[] file=folders.list(); File temp=null; try { for (int i = 0; i < file.length; i++) { if(folderList.get(j).endsWith(File.separator)) temp=new File(folderList.get(j),file[i]); else temp=new File(folderList.get(j),file[i]); if(temp.isFile() && b) temp.createNewFile(); else if(temp.isDirectory()){//如果是子文件夾 folderList.add(folderList.get(j)+File.separator+file[i]); folderList2.add(folderList2.get(j)+File.separator+file[i]); } } } catch (IOException e) { //復制整個文件夾內容操作出錯 e.printStackTrace(); } }

18.復制文件 //import java.io.*; File oldfile = new File(%%1); try { if (oldfile.exists()) { //文件存在時 FileInputStream inStream = new FileInputStream(oldfile); //讀入原文件 FileOutputStream fs = new FileOutputStream(new File(%%2,oldfile.getName())); byte[] buffer = new byte[10240]; int byteread; while ( (byteread = inStream.read(buffer)) != -1) fs.write(buffer, 0, byteread); inStream.close(); } } catch (IOException e) { //復制單個文件操作出錯 e.printStackTrace(); }

19.復制一個目錄下所有的文件到另一個目錄 //import java.io.*; File copyfiles=new File(%%1); File targetfiles = new File(%%2); if (!targetfiles.exists()) targetfiles.mkdirs(); File[] files=copyfiles.listFiles(); for(int i=0;i<files.length;i++){ if(files[i].isFile()){ try { InputStream inStream = new FileInputStream(files[i]); //讀入原文件 FileOutputStream fs = new FileOutputStream(new File(%%2,files[i].getName())); byte[] buffer = new byte[10240]; int byteread; while ( (byteread = inStream.read(buffer)) != -1) fs.write(buffer, 0, byteread); inStream.close(); } catch (IOException e) { //復制單個文件操作出錯 e.printStackTrace(); } } }

20.提取擴展名 String %%2=%%1.substring(%%1.lastIndexOf('.'));

21.提取文件名 String %%2=%%1.substring(%%1.lastIndexOf("\")+1);

22.提取文件路徑 String %%2=%%1.substring(0,%%1.lastIndexOf("\"));

23.替換擴展名 //import java.io.*; File replaceExt=new File(%%1); replaceExt.renameTo(replaceExt.getName().split(".")[0]+"."+%%2);

24.追加路徑 final String path=%%1.endsWith("\")?%%1:%%1+"\"; %%3=path+%%2;

25.移動文件 //import java.io.*; File oldfile = new File(%%1); try { if (oldfile.exists()) { //文件存在時 InputStream inStream = new FileInputStream(oldfile); //讀入原文件 FileOutputStream fs = new FileOutputStream(new File(%%2,oldfile.getName())); byte[] buffer = new byte[10240]; int byteread; while ( (byteread = inStream.read(buffer)) != -1) fs.write(buffer, 0, byteread); inStream.close(); oldfile.delete(); } } catch (IOException e) { //復制單個文件操作出錯 e.printStackTrace(); }

26.移動一個目錄下所有文件到另一個目錄 //import java.io.*; File movefile=new File(%%1); File[] movefiles=movefile.listFiles(); for(int i=0;i<movefiles.length;i++){ if(movefiles[i].isFile()){ File oldfile = new File(movefiles[i]); try { if (oldfile.exists()) { //文件存在時 InputStream inStream = new FileInputStream(oldfile); //讀入原文件 FileOutputStream fs = new FileOutputStream(new File(%%2,oldfile.getName())); byte[] buffer = new byte[10240]; int byteread; while ( (byteread = inStream.read(buffer)) != -1) fs.write(buffer, 0, byteread); inStream.close(); oldfile.delete(); } } catch (IOException e) { //復制單個文件操作出錯 e.printStackTrace(); } } }

27.指定目錄下搜索文件 //import java.io.; private static final String filter=%%1; //".*" private static void doSearch(String path){ File file = new File(path); if(file.exists()) { if(file.isDirectory()) { File[] fileArray = file.listFiles(); for(File f:fileArray) { if(f.isDirectory()) { doSearch(f.getPath()); } else { if(f.getName().indexOf(filter) >= 0) { //f.getPath() } } //f.getPath() } //"The numbers of files had been found:" + countFiles } else { //"Couldn't open the path!" } } else { System.err.println("目錄不存在"); } } doSearch(%%1);

28.打開對話框 / import java.io.; import javax.swing.;/ JFileChooser Jfc = new JFileChooser(); //建立選擇檔案對話方塊盒 Jfc Jfc.showDialog(null, %%1); if (Jfc.getSelectedFile() != null) { File %%2 = Jfc.getSelectedFile(); }

29.文件分割 //import java.io.*; try { File f=new File(%%1); FileInputStream fileInputStream = new FileInputStream(f); byte[] buffer = new byte[fileInputStream.available()]; FileInputStream.read(buffer); fileInputStream.close(); String strFileName = f.getName(); FileOutputStream fileOutputStream = new FileOutputStream(new File(%%2+"\"+ strFileName + "1")); fileOutputStream.write(buffer,0,buffer.length/2); fileOutputStream.close(); fileOutputStream = new FileOutputStream(new File(%%2+"\"+ strFileName + "2")); fileOutputStream.write(buffer, buffer.length/2, buffer.length-buffer.length/2); fileOutputStream.close(); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); }

30.文件合并 //import java.io.*; String strFileName = %%1.substring(%%1.LastIndexOf("\") + 1); try { FileInputStream fileInputStream1 = new FileInputStream(new File(%%2 + strFileName + "1")); FileInputStream fileInputStream2 = new FileInputStream(new File(%%2 + strFileName + "2")); byte[] buffer = new byte[fileInputStream1.available()+fileInputStream2.available()]; FileInputStream.read(buffer, 0, fileInputStream1.available()); FileInputStream2.read(buffer, fileInputStream1.available(), fileInputStream2.available()); fileInputStream.close(); fileInputStream2.close(); FileOutputStream fileOutputStream = new FileOutputStream(new File(%%2+"\"+ strFileName)); fileOutputStream.write(buffer,0,buffer.length); fileOutputStream.close(); } catch(IOException e){ e.printStackTrace(); }

31.文件簡單加密 / import java.io.; import javax.swing.; private static final String CharSet = "0123456789ABCDEF";/ JFileChooser jfc = new JFileChooser(); JFileChooser jfc2 = new JFileChooser(); jfc.showDialog(null, "請選擇要加密編碼的文件"); jfc2.showDialog(null, "請選擇要輸出的文件名"); if (jfc.getSelectedFile() != null && jfc2.getSelectedFile() != null) { File oldfile = jfc.getSelectedFile(); FileInputStream inStream = null; FileWriter fw = null; try { if (oldfile.exists()) { inStream = new FileInputStream(oldfile); fw = new FileWriter(jfc2.getSelectedFile()); byte[] sRead = new byte[10240]; int byteread; while ((byteread = inStream.read(sRead)) != -1) { StringBuilder smi = new StringBuilder(byteread 2); int ka = 3, kb = 5, kc = 2, kd = 7, js = 0; if (byteread % 2 != 0) js = 1; for (int i = 0; i < byteread - 1; i += 2) { char c1 = (char) sRead[i]; char c2 = (char) sRead[i + 1]; int tmp = ka c1 + kc c2; while (tmp < 0) tmp += 1024; byte s1 = (byte) (tmp % 1024); int js1 = (int) s1 >> 4 & 0xf; smi.append(CharSet.substring(js1, js1 + 1)); int ks1 = s1 & 0xf; smi.append(CharSet.substring(ks1, ks1 + 1)); tmp = kb c1 + kd * c2; while (tmp < 0) tmp += 1024; byte s2 = (byte) (tmp % 1024); int js2 = (int) s2 >> 4 & 0xf; smi.append(CharSet.substring(js2, js2 + 1)); int ks2 = s2 & 0xf; smi.append(CharSet.substring(ks2, ks2 + 1)); } if (js == 1) { byte s3 = (byte) ((sRead[byteread - 1] - 4) % 1024); int js3 = (int) s3 >> 4 & 0xf; smi.append(CharSet.substring(js3, js3 + 1)); int ks3 = (int) s3 & 0xf; smi.append(CharSet.substring(ks3, ks3 + 1)); } fw.write(smi.toString()); } fw.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { fw.close(); inStream.close(); } catch (IOException e) { e.printStackTrace(); } } }

32.文件簡單解密 / import java.io.; import javax.swing.; private static final String CharSet = "0123456789ABCDEF";/ private static int niyuan(int m, int n) { int a, b, c, d, t, yu = 0, shang, mod; a = m; b = n; mod = a; c = 0; d = 1; while (b < 0) b += a; if (a % b == 0 || b % 2 == 0) return 0; while (b != 1) { t = a % b; shang = a / b; a = b; b = t; yu = c - shang d; c = d; d = yu; } if (yu < 0) yu += mod; return yu; } JFileChooser jfc = new JFileChooser(); JFileChooser jfc2 = new JFileChooser(); jfc.showDialog(null, "請選擇要解碼解密的文件"); jfc2.showDialog(null, "請選擇要輸出的文件名"); if (jfc.getSelectedFile() != null && jfc2.getSelectedFile() != null) { FileOutputStream fw = null; try { FileInputStream fis = new FileInputStream(jfc.getSelectedFile()); fw = new FileOutputStream(jfc2 .getSelectedFile()); byte[] buffer = new byte[20480]; int ka = 3, kb = 5, kc = 2, kd = 7, js = 0, tmp; int aany, ddny; int r00 = ka kc kd; int r01 = -ka kb kc; int r10 = -kb kc kc; int r11 = ka kb kc; int x00 = ka ka kc kd - ka kb kc kc; int x11 = ka kb kc kd - kb kb kc kc; while (x00 % 2 == 0) { x00 /= 2; r00 /= 2; r01 /= 2; } while (x11 % 2 == 0) { x11 /= 2; r10 /= 2; r11 /= 2; } aany = x00; ddny = x11; if (niyuan(1024, aany) != 0 && niyuan(1024, ddny) != 0) { int kn00 = r00 niyuan(1024, x00); int kn01 = r01 niyuan(1024, x00); int kn10 = r10 niyuan(1024, x11); int kn11 = r11 niyuan(1024, x11); ka = kn00; kb = kn01; kc = kn10; kd = kn11; } else { JOptionPane.showMessageDialog(null, "無逆矩陣!"); System.exit(0); } while (ka < 0) ka += 1024; while (kb < 0) kb += 1024; while (kc < 0) kc += 1024; while (kd < 0) kd += 1024; ka %= 1024; kb %= 1024; kc %= 1024; kd %= 1024; try { int byteread; while ((byteread = fis.read(buffer)) != -1) { int nLen = byteread / 2; byte[] sming = new byte[nLen]; String chs=new String(buffer,"US-ASCII"); for (int i = 0; i < nLen; i++) { byte bTmp; if (byteread < 2) bTmp = -1; bTmp = (byte) (CharSet.indexOf(chs.substring(i 2,i 2+1)) 16 + CharSet .indexOf(chs.substring(i 2 + 1,i 2 + 2))); sming[i] = bTmp; } if (nLen % 2 != 0) js = 1; for (int i = 0; i < nLen - 1; i += 2) { char c1 = (char) sming[i]; char c2 = (char) sming[i + 1]; tmp = ka c1 + kc c2; while (tmp < 0) tmp += 1024; char s1 = (char) (tmp % 1024); fw.write(s1); tmp = kb c1 + kd c2; while (tmp < 0) tmp += 1024; char s2 = (char) (tmp % 1024); fw.write(s2); } if (js == 1) { char c3 = (char) ((sming[nLen - 1] - 4) % 1024); fw.write(c3); } } fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }

33.讀取ini文件屬性 / import java.io.; import java.util.; import java.util.regex.; private static HashMap configMap=null; private static FileReader fileReader = null; / private static boolean readIni() { if (configMap == null) { configMap = new HashMap<String, ArrayList>(); String strLine = null; String currentNode = null; String previousNode = null; ArrayList<Properties> vec = new ArrayList<Properties>(); int row = 0; BufferedReader bufferedReader = new BufferedReader(fileReader); try { while ((strLine = bufferedReader.readLine()) != null) { String oneLine = strLine.trim(); if (oneLine.length() >= 1) { Pattern p = Pattern.compile("\[\s.\s\]"); int nodelen = oneLine.split("[;]").length; String[] strArray1 = new String[4]; if (nodelen == 1) { oneLine = oneLine.split("[;]")[0].trim(); } else if (nodelen == 2) { strArray1[3] = oneLine.split("[;]")[1].trim(); oneLine = oneLine.split("[;]")[0].trim(); } Matcher m = p.matcher(oneLine); if (m.matches()) { strArray1[0] = "@Node"; strArray1[1] = oneLine; strArray1[2] = ""; } else { int keylen = oneLine.split("=").length; if (keylen == 1) { strArray1[0] = "@Key"; strArray1[1] = oneLine.split("=")[0]; strArray1[2] = ""; } else if (keylen == 2) { strArray1[0] = "@Key"; strArray1[1] = oneLine.split("=")[0]; strArray1[2] = oneLine.split("=")[1]; } else { strArray1[0] = "@ElementError"; strArray1[1] = ""; strArray1[2] = ""; strArray1[3] = ""; } } if (strArray1[0].equals("@Node")) { previousNode = currentNode; currentNode = strArray1[1]; if (row > 0) { configMap.put(previousNode, (ArrayList)vec.clone()); vec.clear(); row = 0; } } else if (strArray1[0].equals("@Key") && row == 0) { Properties ht = new Properties(); ht.setProperty(strArray1[1], strArray1[2]); vec.add(ht); row++; } else if (strArray1[0].equals("@Key") && row > 0) { Properties ht2 = new Properties(); ht2.put(strArray1[1], strArray1[2]); vec.add(ht2); row++; } } } configMap.put(currentNode, (ArrayList)vec.clone()); } catch (FileNotFoundException e) { configMap = null; e.printStackTrace(); return false; } catch (IOException e) { configMap = null; e.printStackTrace(); return false; } } return true; } try { fileReader = new FileReader(%%1); //"Setup.ini" } catch (FileNotFoundException e1) { e1.printStackTrace(); } if (readIni()) { ArrayList<Properties> li = null; li = (ArrayList<Properties>) configMap.get(%%2); //"[DataSource]" for (Properties pro : li) { if(pro.containsKey(%%3)) %%4=pro.getProperty(%%3); } } try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); }

34.合并一個目錄下所有的文件 //import java.io.*; File combinefiles=new File(%%1); File[] files=combinefiles.listFiles(); FileOutputStream fs; try { fs=new FileOutputStream(new File(%%2)); } catch(IOException e){ e.printStackTrace(); } for(int i=0;i<files.length;i++){ if(files[i].isFile()){ try { FileInputStream inStream=new FileInputStream(files[i]); byte[] buffer = new byte[10240]; int byteread; while((byteread=inStream.read(buffer))!=-1) fs.write(buffer,0,byteread); inStream.close(); } catch(Exception e){ //復制文件出錯 e.printStackTrace(); } } } try { fs.close(); } catch(IOException e){ e.printStackTrace(); }

35.寫入ini文件屬性 / import java.io.; import java.util.; import java.util.regex.; private static HashMap configMap=null; */ if (readIni()) { ArrayList<Properties> li = null; try { FileWriter fw = new FileWriter(%%1); li = (ArrayList<Properties>) configMap.get(%%2); //"[DataSource]" fw.write("%%2\r\n"); for (Properties pro : li) { if (pro.containsKey(%%3)) //"ip" fw.write("%%3=" + %%6 + "\r\n"); } fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } }

36.獲得當前路徑 String %%1=getClass().getResource("/").getPath(); //String %%1=System.getProperty("user.dir");

37.讀取XML數據庫 / import java.io.; import javax.xml.parsers.; import org.w3c.dom.; private static Document document; private static Element node; */ File xml_file = new File(%%1); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(xml_file); } catch (Exception e) { e.printStackTrace(); } String subNodeTag = %%2; Element rootNode = document.getDocumentElement(); //%%2="Product" //%%4="id" //%%6="port" //%%3="Name" //%%5="001" NodeList nlist = rootNode.getElementsByTagName(subNodeTag); int len = nlist.getLength(); for (int i = 0; i < len; i++) { node = nlist.item(i); String getNodeAttrValue = null; NamedNodeMap attrList = node.getAttributes(); for (int j = 0; j < attrList.getLength(); j++) { if (attrList.item(j).getNodeName().equals(%%4)) { getNodeAttrValue = attrList.item(j).getNodeValue(); break; } } if (getNodeAttrValue.equals(%%5)) { nlist = node.getChildNodes(); String %%9=((Element) node).getElementsByTagName(%%3).item(0) .getFirstChild().getNodeValue(); break; } }

38.寫入XML數據庫 / import java.io.; import javax.xml.parsers.; import org.w3c.dom.; import javax.xml.transform.; import javax.xml.transform.dom.; import javax.xml.transform.stream.; private Document document; private Element node;/ File xml_file = new File(%%1); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(xml_file); } catch (Exception e) { e.printStackTrace(); } String subNodeTag = %%2; Element rootNode = document.getDocumentElement(); // %%2="Product" //%%4="pid" //%%6="author" // %%3="Name" //%%5="price" NodeList nlist = rootNode.getElementsByTagName(subNodeTag); String ss = null; int len = nlist.getLength(); for (int i = 0; i < len; i++) { node = (Element) nlist.item(i); //node.setAttribute(%%4, "0"+String.valueOf(i)); //ID格式化 String getNodeAttrValue = null; NamedNodeMap attrList = node.getAttributes(); for (int j = 0; j < attrList.getLength(); j++) { if (attrList.item(j).getNodeName().equals(%%4)) { getNodeAttrValue = attrList.item(j).getNodeValue(); break; } } if (getNodeAttrValue.equals("001")) { nlist = node.getChildNodes(); ss = ((Element) node).getElementsByTagName(%%3).item(0) .getFirstChild().getNodeValue(); ss = ((Element) node).getElementsByTagName(%%6).item(0) .getFirstChild().getNodeValue(); ss = ((Element) node).getElementsByTagName(%%5).item(0) .getFirstChild().getNodeValue(); ((Element) node).getElementsByTagName(%%3).item(0) .getFirstChild().setTextContent(%%7); ((Element) node).getElementsByTagName(%%6).item(0) .getFirstChild().setTextContent(%%8); ((Element) node).getElementsByTagName(%%5).item(0) .getFirstChild().setTextContent(%%9); break; } } if (ss == null) { node = document.createElement(%%2); node.setAttribute(%%4, String.valueOf(nlist.getLength() + 1)); node.appendChild(document.createTextNode("\n")); Element server = document.createElement(%%3); server.appendChild(document.createTextNode(%%7)); node.appendChild(server); Element ipNode = document.createElement(%%6); ipNode.appendChild(document.createTextNode(%%8)); node.appendChild(ipNode); node.appendChild(document.createTextNode("\n")); Element port = document.createElement(%%5); port.appendChild(document.createTextNode(%%9)); node.appendChild(port); node.appendChild(document.createTextNode("\n")); rootNode.appendChild(node); } TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(xml_file); transformer.transform(source, result); } catch (IOException e) { e.printStackTrace(); }

39.ZIP壓縮文件 / import java.io.; import java.util.zip.;/ //創建文件輸入流對象 FileInputStream fis=new FileInputStream(%%1); //創建文件輸出流對象 FileOutputStream fos=new FileOutputStream(%%2); //創建ZIP數據輸出流對象 ZipOutputStream zipOut=new ZipOutputStream(fos); //創建指向壓縮原始文件的入口 ZipEntry entry=new ZipEntry(args[0]); try { zipOut.putNextEntry(entry); //向壓縮文件中輸出數據 int nNumber; byte[] buffer=new byte[1024]; while((nNumber=fis.read(buffer))!=-1) zipOut.write(buffer,0,nNumber); //關閉創建的流對象 zipOut.close(); fos.close(); fis.close(); } catch(IOException e) { e.printStackTrace(); }

40.ZIP解壓縮 / import java.io.; import java.util.zip.;/ //創建文件輸入流對象實例 FileInputStream fis=new FileInputStream(%%1); //創建ZIP壓縮格式輸入流對象實例 ZipInputStream zipin=new ZipInputStream(fis); //創建文件輸出流對象實例 FileOutputStream fos=new FileOutputStream(%%2); //獲取Entry對象實例 ZipEntry entry=zipin.getNextEntry(); byte[] buffer=new byte[1024]; int nNumber; try{ while((nNumber=zipin.read(buffer,0,buffer.length))!=-1) fos.write(buffer,0,nNumber); //關閉文件流對象 zipin.close(); fos.close(); fis.close(); } catch(IOException e) { e.printStackTrace(); }

41.獲得應用程序完整路徑 String %%1=System.getProperty("user.dir");

42.遞歸刪除目錄中的文件 / import java.io.; import java.util.;/ ArrayList<String> folderList = new ArrayList<String>(); folderList.add(%%1); for (int j = 0; j < folderList.size(); j++) { File file = new File(folderList.get(j)); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { fileList.add(files[i]); } } for (File f : fileList) { f.delete(); } }

43.ZIP壓縮文件夾 / import java.io.; import java.util.; import java.util.zip.; */ public static String zipFileProcess(ArrayList outputZipFileNameList, String outputZipNameAndPath) { ArrayList fileNames = new ArrayList(); ArrayList files = new ArrayList(); FileOutputStream fileOut = null; ZipOutputStream outputStream = null; FileInputStream fileIn = null; StringBuffer sb = new StringBuffer(outputZipNameAndPath); // FileInputStream fileIn =null; try { if (outputZipNameAndPath.indexOf(".zip") != -1) { outputZipNameAndPath = outputZipNameAndPath; } else { sb.append(".zip"); outputZipNameAndPath = sb.toString(); } fileOut = new FileOutputStream(outputZipNameAndPath); outputStream = new ZipOutputStream(fileOut); int outputZipFileNameListSize = 0; if (outputZipFileNameList != null) { outputZipFileNameListSize = outputZipFileNameList.size(); } for (int i = 0; i < outputZipFileNameListSize; i++) { File rootFile = new File(outputZipFileNameList.get(i).toString()); listFile(rootFile, fileNames, files, ""); } for (int loop = 0; loop < files.size(); loop++) { fileIn = new FileInputStream((File) files.get(loop)); outputStream.putNextEntry(new ZipEntry((String) fileNames.get(loop))); byte[] buffer = new byte[1024]; while (fileIn.read(buffer) != -1) { outputStream.write(buffer); } outputStream.closeEntry(); fileIn.close(); } return outputZipNameAndPath; } catch (IOException ioe) { return null; } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } if (fileIn != null) { try { fileIn.close(); } catch (IOException e) { } } } } private static void listFile(File parentFile, List nameList, List fileList, String directoryName) { if (parentFile.isDirectory()) { File[] files = parentFile.listFiles(); for (int loop = 0; loop < files.length; loop++) { listFile(files[loop], nameList, fileList, directoryName + parentFile.getName() + "/"); } } else { fileList.add(parentFile); nameList.add(directoryName + parentFile.getName()); } } String savePath=%%1; ArrayList<String> outputZipFileName=new ArrayList<String>(); outputZipFileName.add(%%2); zipFileProcess(outputZipFileName,savePath);

44.IDEA加密算法 private byte[] bytekey; public byte[] getKey(String key){ int len1 =key.length(); if (len1>=16) { key=key.substring(0, 16); } else { for (int i=0;i<16-len1;i++){ key=key.concat("0"); } } bytekey=key.getBytes(); return bytekey; } /**

  • 加密String明文輸入,String密文輸出
  • @param strMing
  • @return */ public String getEncString(String strMing) { byte[] byteMi = null; byte[] byteMing = null; String strMi = ""; try { return byte2hex(IdeaEncrypt(bytekey,strMing.getBytes(),true) ); } catch(Exception e){ e.printStackTrace(); } finally { byteMing = null; byteMi = null; } return strMi; } /**
  • 解密 以String密文輸入,String明文輸出
  • @param strMi
  • @return */ public String getDesString(String strMi) { byte[] byteMing = null; byte[] byteMi = null; String strMing = ""; try { String tmp= new String(IdeaEncrypt(bytekey,hex2byte(strMi.getBytes()),false )); int len1=tmp.length(); return tmp.substring(0, len1-6); } catch(Exception e) { e.printStackTrace(); } finally { byteMing = null; byteMi = null; } return strMing; } private byte[] Encrypt(byte[] bytekey, byte[] inputBytes, boolean flag) { byte[] encryptCode = new byte[8]; // 分解子密鑰 int[] key = get_subkey(flag, bytekey); // 進行加密操作 encrypt(key, inputBytes, encryptCode); // 返回加密數據 return encryptCode; }

private int bytesToInt(byte[] inBytes, int startPos) { return ((inBytes[startPos] << 8) & 0xff00) + (inBytes[startPos + 1] & 0xff); }

private void intToBytes(int inputInt, byte[] outBytes, int startPos) { outBytes[startPos] = (byte) (inputInt >>> 8); outBytes[startPos + 1] = (byte) inputInt; }

private int x_multiply_y(int x, int y) { if (x == 0) { x = 0x10001 - y; } else if (y == 0) { x = 0x10001 - x; } else { int tmp = x * y; y = tmp & 0xffff; x = tmp >>> 16; x = (y - x) + ((y < x) ? 1 : 0); }

return x & 0xffff; }

private void encrypt(int[] key, byte[] inbytes, byte[] outbytes) { int k = 0; int a = bytesToInt(inbytes, 0); int b = bytesToInt(inbytes, 2); int c = bytesToInt(inbytes, 4); int d = bytesToInt(inbytes, 6);

for (int i = 0; i < 8; i++) { a = x_multiply_y(a, key[k++]); b += key[k++]; b &= 0xffff; c += key[k++]; c &= 0xffff; d = x_multiply_y(d, key[k++]);

int tmp1 = b; int tmp2 = c; c ^= a; b ^= d; c = x_multiply_y(c, key[k++]); b += c; b &= 0xffff; b = x_multiply_y(b, key[k++]); c += b; c &= 0xffff; a ^= b; d ^= c; b ^= tmp2; c ^= tmp1; }

intToBytes(x_multiply_y(a, key[k++]), outbytes, 0); intToBytes(c + key[k++], outbytes, 2); intToBytes(b + key[k++], outbytes, 4); intToBytes(x_multiply_y(d, key[k]), outbytes, 6); }

private int[] encrypt_subkey(byte[] byteKey) { int[] key = new int[52];

if (byteKey.length < 16) { byte[] tmpkey = new byte[16]; System.arraycopy(byteKey, 0, tmpkey, tmpkey.length - byteKey.length, byteKey.length); byteKey = tmpkey; }

for (int i = 0; i < 8; i++) { key[i] = bytesToInt(byteKey, i * 2); }

for (int j = 8; j < 52; j++) { if ((j & 0x7) < 6) { key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 6] >> 7)) & 0xffff; } else if ((j & 0x7) == 6) { key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff; } else { key[j] = (((key[j - 15] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff; } }

return key; }

private int fun_a(int a) { if (a < 2) { return a; }

int b = 1; int c = 0x10001 / a;

for (int i = 0x10001 % a; i != 1;) { int d = a / i; a %= i; b = (b + (c * d)) & 0xffff;

if (a == 1) { return b; } d = i / a; i %= a; c = (c + (b * d)) & 0xffff; }

return (1 - c) & 0xffff; }

private int fun_b(int b) { return (0 - b) & 0xffff; }

private int[] uncrypt_subkey(int[] key) { int dec = 52; int asc = 0; int[] unkey = new int[52]; int aa = fun_a(key[asc++]); int bb = fun_b(key[asc++]); int cc = fun_b(key[asc++]); int dd = fun_a(key[asc++]); unkey[--dec] = dd; unkey[--dec] = cc; unkey[--dec] = bb; unkey[--dec] = aa;

for (int k1 = 1; k1 < 8; k1++) { aa = key[asc++]; bb = key[asc++]; unkey[--dec] = bb; unkey[--dec] = aa; aa = fun_a(key[asc++]); bb = fun_b(key[asc++]); cc = fun_b(key[asc++]); dd = fun_a(key[asc++]); unkey[--dec] = dd; unkey[--dec] = bb; unkey[--dec] = cc; unkey[--dec] = aa; }

aa = key[asc++]; bb = key[asc++]; unkey[--dec] = bb; unkey[--dec] = aa; aa = fun_a(key[asc++]); bb = fun_b(key[asc++]); cc = fun_b(key[asc++]); dd = fun_a(key[asc]); unkey[--dec] = dd; unkey[--dec] = cc; unkey[--dec] = bb; unkey[--dec] = aa;

return unkey; }

private int[] get_subkey(boolean flag, byte[] bytekey) { if (flag) { return encrypt_subkey(bytekey); } else { return uncrypt_subkey(encrypt_subkey(bytekey)); } }

private byte[] ByteDataFormat(byte[] data, int unit) { int len = data.length; int padlen = unit - (len % unit); int newlen = len + padlen; byte[] newdata = new byte[newlen]; System.arraycopy(data, 0, newdata, 0, len);

for (int i = len; i < newlen; i++) newdata[i] = (byte) padlen;

return newdata; }

public byte[] IdeaEncrypt(byte[] idea_key, byte[] idea_data, boolean flag) { byte[] format_key = ByteDataFormat(idea_key, 16); byte[] format_data = ByteDataFormat(idea_data, 8);

int datalen = format_data.length; int unitcount = datalen / 8; byte[] result_data = new byte[datalen];

for (int i = 0; i < unitcount; i++) { byte[] tmpkey = new byte[16]; byte[] tmpdata = new byte[8]; System.arraycopy(format_key, 0, tmpkey, 0, 16); System.arraycopy(format_data, i * 8, tmpdata, 0, 8);

byte[] tmpresult = Encrypt(tmpkey, tmpdata, flag); System.arraycopy(tmpresult, 0, result_data, i * 8, 8); }

return result_data; }

/**

  • 二行制轉字符串
  • @param b
  • @return */ public static String byte2hex(byte[] b) { //一個字節的數, // 轉成16進制字符串 String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { //整數轉成十六進制表示 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) hs = hs + "0" + stmp; else hs = hs + stmp; } return hs.toUpperCase(); //轉成大寫 }

public static byte[] hex2byte(byte[] b) { if((b.length%2)!=0) throw new IllegalArgumentException("長度不是偶數"); byte[] b2 = new byte[b.length/2]; for (int n = 0; n < b.length; n+=2) { String item = new String(b,n,2); // 兩位一組,表示一個字節,把這樣表示的16進制字符串,還原成一個進制字節 b2[n/2] = (byte)Integer.parseInt(item,16); }

return b2; }

public static void main(String[] args) {

IDEA idea = new IDEA();

idea.getKey("aadd");//生成密匙

String strEnc = idea.getEncString("1234567890");//加密字符串,返回String的密文 System.out.println(strEnc);

String strDes = idea.getDesString(strEnc);//把String 類型的密文解密 System.out.println(strDes);

// String key = "0000000000000000"; // String data = "11111111馮"; // byte[] bytekey = key.getBytes(); // byte[] bytedata = data.getBytes(); // // IDEA idea = new IDEA(); // byte[] encryptdata = idea.IdeaEncrypt(bytekey, bytedata, true); // byte[] decryptdata = idea.IdeaEncrypt(bytekey, encryptdata, false); // // System.out.println("--------------------------------"); // // for (int i = 0; i < bytedata.length; i++) { // System.out.print(" " + bytedata[i] + " "); // } // // System.out.println(""); // // for (int i = 0; i < encryptdata.length; i++) { // System.out.print(" " + encryptdata[i] + " "); // } // // System.out.println(""); // // for (int i = 0; i < decryptdata.length; i++) { // System.out.print(" " + decryptdata[i] + " "); // }

}

45.驗證Schema / import javax.xml.; import javax.xml.transform.stream.; import javax.xml.validation.; import org.xml.sax.;/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); StreamSource ss = new StreamSource(%%1); //"mySchema.xsd" try { Schema schema = factory.newSchema(ss); } catch (SAXException e) { e.printStackTrace(); }

46.Grep / import java.util.regex.; import java.io.;/ throws Exception Pattern pattern = Pattern.compile(%%1); // 第一個參數為需要匹配的字符串 Matcher matcher = pattern.matcher(""); String file = %%2; BufferedReader br = null; String line; try { br = new BufferedReader (new FileReader (file)); // 打開文件 } catch (IOException e) { // 沒有打開文件,則產生異常 System.err.println ("Cannot read '" + file

  • "': " + e.getMessage()); } while ((line = br.readLine()) != null) { // 讀入一行,直到文件結束 matcher.reset (line); // 匹配字符串 if (matcher.find()) { // 如果有匹配的字符串,則輸出 //line } } br.close(); // 關閉文件

47.直接創建多級目錄 //import java.io.*; File f=new File(%%1); f.mkdirs();

48.批量重命名 //import java.io.*; File target = new File("%%1"); String[] files = target.list(); File f = null; String filename = null; for (String file : files) { f = new File(target, file); filename = f.getName(); if (filename.substring(filename.lastIndexOf('.')).equalsIgnoreCase( "%%2")) { f.renameTo(new File(target.getAbsolutePath(), filename.replace( "%%2", "%%3"))); // 這里可以反復使用replace替換,當然也可以使用正則表達式來替換了 ".txt" ".bat" } }

49.文本查找替換 //import java.nio.; String s1=%%1; String s2=%%2; String s3=%%3; int pos=%%4; /變量i和j分別表示主串和模式串中當前字符串的位置,k表示匹配次數/ int i,j,k=0; i = pos; j = 0; //將s1轉化成StringBuffer型進行操作 repStr = new StringBuffer(s1); while(i<repStr.length()&&j<s2.length()) { if(repStr.charAt(i) == s2.charAt(j)) { ++i; ++j; if(j==s2.length()) { /j=s2.length()表示字符串匹配成功,匹配次數加1,此外對主串進行字符串替換*/ k = k+1; repStr.replace(i-j,i,s3); //將j進行重新賦值開始新的比較 j = 0; } } else {i = i-j+1; j = 0;} } return k;

50.文件關聯 //import java.io.*; try { Runtime.getRuntime().exec(%%1); //"assoc .txt =mynote" "assoc [.ext[=[filetype]]]" } catch (IOException e) { e.printStackTrace(); }

51.批量轉換編碼從GB2312到Unicode

52.設置JDK環境變量 @echo off IF EXIST %1\bin\java.exe ( rem 如輸入正確的 Java2SDK 安裝目錄,開始設置環境變量 @setx JAVA_HOME %1 @setx path %path%;%JAVA_HOME%\bin @setx classpath %classpath%;. @setx classpath %classpath%;%JAVA_HOME%\lib\tools.jar @setx classpath %classpath%;%JAVA_HOME%\lib\dt.jar @setx classpath %classpath%;%JAVA_HOME%\jre\lib\rt.jar @echo on @echo Java 2 SDK 環境參數設置完畢,正常退出。 ) ELSE ( IF "%1"=="" ( rem 如沒有提供安裝目錄,提示之后退出 @echo on @echo 沒有提供 Java2SDK 的安裝目錄,不做任何設置,現在退出環境變量設置。 ) ELSE ( rem 如果提供非空的安裝目錄但沒有bin\java.exe,則指定的目錄為錯誤的目錄 @echo on @echo 非法的 Java2SDK 的安裝目錄,不做任何設置,現在退出環境變量設置。 ) ) //http://sourceforge.net/projects/jregistrykey/ //import ca.beq.util.win32.registry.; //import java.util.; 1.打開鍵 RegistryKey r = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"); 2.添加鍵 RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\BEQ Technologies"); r.create(); 9.寫入字符串值 RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\BEQ Technologies"); RegistryValue v = new RegistryValue("myVal", ValueType.REG_SZ, "data"); r.setValue(v); 6.獲取DWORD值 RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\BEQ Technologies"); if(r.hasValue("myValue")) { RegistryValue v = r.getValue("myValue"); v.setType(ValueType.REG_DWORD); } // if

53.批量轉換編碼從Unicode到GB2312

54.刪除空文件夾 //import java.io.*; File f=new File(%%1); if (isFolerNull(f)) { for (File file :f.listFiles()) { if (file.list().length == 0) file.delete(); } }

55.GB2312文件轉UTF-8格式 //import java.io.; public class CharsetConvertor { public static void main(String[] args) { String str = "This is a test for 中網!@#$。,?"; try { File f = new File("D:/test.txt"); FileOutputStream fio = new FileOutputStream(f); String s = gbToUtf8(str); fio.write(s.getBytes("UTF-8")); fio.close(); } catch (Exception e) { e.printStackTrace(); } }

public static String gbToUtf8(String str) throws UnsupportedEncodingException { StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { String s = str.substring(i, i + 1); if (s.charAt(0) > 0x80) { byte[] bytes = s.getBytes("Unicode"); String binaryStr = ""; for (int j = 2; j < bytes.length; j += 2) { // the first byte String hexStr = getHexString(bytes[j + 1]); String binStr = getBinaryString(Integer.valueOf(hexStr, 16)); binaryStr += binStr; // the second byte hexStr = getHexString(bytes[j]); binStr = getBinaryString(Integer.valueOf(hexStr, 16)); binaryStr += binStr; } // convert unicode to utf-8 String s1 = "1110" + binaryStr.substring(0, 4); String s2 = "10" + binaryStr.substring(4, 10); String s3 = "10" + binaryStr.substring(10, 16); byte[] bs = new byte[3]; bs[0] = Integer.valueOf(s1, 2).byteValue(); bs[1] = Integer.valueOf(s2, 2).byteValue(); bs[2] = Integer.valueOf(s3, 2).byteValue(); String ss = new String(bs, "UTF-8"); sb.append(ss); } else { sb.append(s); } } return sb.toString(); }

private static String getHexString(byte b) { String hexStr = Integer.toHexString(b); int m = hexStr.length(); if (m < 2) { hexStr = "0" + hexStr; } else { hexStr = hexStr.substring(m - 2); } return hexStr; }

private static String getBinaryString(int i) { String binaryStr = Integer.toBinaryString(i); int length = binaryStr.length(); for (int l = 0; l < 8 - length; l++) { binaryStr = "0" + binaryStr; } return binaryStr; } }

56.UTF-8文件轉GB2312格式 private String utf8Togb2312(String str){ StringBuffer sb = new StringBuffer(); for(int i=0; i<str.length(); i++) { char c = str.charAt(i); switch (c) { case '+': sb.append(' '); break; case '%': try { sb.append((char)Integer.parseInt( str.substring(i+1,i+3),16)); } catch (NumberFormatException e) { throw new IllegalArgumentException(); } i += 2; break; default: sb.append(c); break; } } // Undo conversion to external encoding String result = sb.toString(); String res=null; try{ byte[] inputBytes = result.getBytes("8859_1"); res= new String(inputBytes,"UTF-8"); } catch(Exception e){} return res; }

57.獲取文件路徑的父路徑 String %%2=%%1.substring(0,%%1.lastIndexOf("\"));

58.Unicode文件轉UTF-8格式 try { // Convert from Unicode to UTF-8 String string = "abc\u5639\u563b"; byte[] utf8 = string.getBytes("UTF-8"); // Convert from UTF-8 to Unicode string = new String(utf8, "UTF-8"); } catch (UnsupportedEncodingException e) { } /**

  • unicode 轉換成 utf-8
  • @author fanhui
  • 2007-3-15
  • @param theString
  • @return */ public static String unicodeToUtf8(String theString) { char aChar; int len = theString.length(); StringBuffer outBuffer = new StringBuffer(len); for (int x = 0; x < len;) { aChar = theString.charAt(x++); if (aChar == '\') { aChar = theString.charAt(x++); if (aChar == 'u') { // Read the xxxx int value = 0; for (int i = 0; i < 4; i++) { aChar = theString.charAt(x++); switch (aChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException( "Malformed \uxxxx encoding."); } } outBuffer.append((char) value); } else { if (aChar == 't') aChar = '\t'; else if (aChar == 'r') aChar = '\r'; else if (aChar == 'n') aChar = '\n'; else if (aChar == 'f') aChar = '\f'; outBuffer.append(aChar); } } else outBuffer.append(aChar); } return outBuffer.toString(); }

59.CRC循環冗余校驗 / import java.nio.; import java.util.zip.;/ try { FileInputStream in = new FileInputStream(%%1); FileChannel channel = in.getChannel(); CRC32 crc = new CRC32(); int length = (int)channel.size(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length); for(int i = 0;i<length;i++) { int c = buffer.get(i); crc.update(c); } System.out.println("crc校驗和:"+(Long.toHexString(crc.getValue())).toUpperCase()); } catch (Exception e) { e.printStackTrace(); }

60.判斷是否為空文件 //import java.io.*; FileReader fr=new FileReader(%%1); if(fr.read()==1) //空白文件

61.終止程序 Runtime.exec("taskkill /F /IM %%1.exe");

62.定時關機 import java.awt.; import java.awt.event.; import java.awt.geom.; import java.util.; import javax.swing.*;

public class ClockFrame extends JFrame { private JComboBox hourBox, minuteBox, secondBox;

private int hour, minute, second, totalSeconds, currentSeconds;

private long argue;

private GregorianCalendar calendar;

private boolean change = true;

private static final int WIDTH = 200;

private static final int HEIGHT = 150;

public ClockFrame() {

setTitle("關機定時"); setSize(200, 150);

Container contentPanel = getContentPane();

JPanel timePanel = new JPanel(); timePanel.setLayout(new GridLayout(4, 2));

JLabel minuteLable = new JLabel("設置分鐘"); timePanel.add(minuteLable); minuteBox = new JComboBox(); timePanel.add(minuteBox); for (int i = 0; i < 60; i++) { minuteBox.addItem(i); } minuteBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { minute = ((Integer) minuteBox.getSelectedItem()).intValue(); } });

JLabel secondLable = new JLabel("設置秒鐘"); timePanel.add(secondLable); secondBox = new JComboBox(); timePanel.add(secondBox); for (int i = 0; i < 60; i++) { secondBox.addItem(i); } secondBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { second = ((Integer) secondBox.getSelectedItem()).intValue(); } }); contentPanel.add(timePanel, BorderLayout.CENTER);

JButton check = new JButton("確定"); contentPanel.add(check, BorderLayout.SOUTH); check.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { JButton check=(JButton) evt.getSource(); if (check.getText().equals("確定")) { calendar = new GregorianCalendar(); int currentSeconds = calendar.get(Calendar.HOUR_OF_DAY)

  • 3600 + calendar.get(Calendar.MINUTE) * 60
  • calendar.get(Calendar.SECOND); totalSeconds = hour 3600 + minute 60 + second;

if (totalSeconds - currentSeconds >= 0) { argue = (totalSeconds - currentSeconds) * 1000; JOptionPane.showMessageDialog(ClockFrame.this, "您設置的時間為 " + hour + ":" + minute + ":" + second

  • "\n程序將在后臺運行,并在此時自動關閉計算機!", "設置成功", JOptionPane.INFORMATION_MESSAGE); hideFrame(); } try { // Thread.sleep(argue);//這句沒用 Runtime.getRuntime().exec( "shutdown.exe -s -c \"我要關機了噢!不好意思!\" -t "
  • totalSeconds); check.setText("取消"); } catch (Exception e) { e.printStackTrace(); } }else{ try { Runtime.getRuntime().exec("shutdown.exe -a"); check.setText("確定"); } catch (Exception e) { e.printStackTrace(); } } } }); }

private void hideFrame() { this.setVisible(false); }

public static void main(String[] args) { JFrame frame = new ClockFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); frame.show(); } }

63.顯示進程列表 //import java.io.*; BufferedReader br=null; try { Process proc=Runtime.getRuntime().exec("tasklist"); br=new BufferedReader(new InputStreamReader(proc.getInputStream())); @SuppressWarnings("unused") String line=null; while((line=br.readLine())!=null){ //br.readLine() } } catch (IOException e) { e.printStackTrace(); }finally{ if(br!=null){ try { br.close(); } catch (Exception e) { e.printStackTrace(); } } }

64.遍歷文件夾列出文件大小

65.目錄下所有文件移動到整合操作 / import java.io.; import java.util.; import javax.swing.; */ JFileChooser Jfc = new JFileChooser("請選擇源路徑"); // 建立選擇檔案對話方塊盒 Jfc Jfc.showDialog(null, %%1); if (!Jfc.getSelectedFile() != null) { return; } String %%1 = Jfc.getSelectedFile().getParent(); Jfc = new JFileChooser("請選擇目標路徑"); // 建立選擇檔案對話方塊盒 Jfc Jfc.showDialog(null, %%1); if (!Jfc.getSelectedFile() != null) { return; } String %%2 = Jfc.getSelectedFile().getParent();

66.對目標壓縮文件解壓縮到指定文件夾 / import java.io.; import java.util.zip.;/ String zipFileName=%%1; String extPlace=%%2; File myFolderPath = new File(extPlace); try { if (!myFolderPath.exists()) { myFolderPath.mkdir(); } } catch (Exception e) { //新建目錄操作出錯 e.printStackTrace(); return; } try { ZipInputStream in = new ZipInputStream(new FileInputStream( zipFileName)); ZipEntry entry = null; while ((entry = in.getNextEntry()) != null) { String entryName = entry.getName(); File file = new File(extPlace , entryName); if (entry.isDirectory()) { file.mkdirs(); } else { FileOutputStream os = new FileOutputStream(file); // Transfer bytes from the ZIP file to the output // file byte[] buf = new byte[10240]; int len; while ((len = in.read(buf)) > 0) { os.write(buf, 0, len); } os.close(); in.closeEntry(); } } } catch (IOException e) { e.printStackTrace(); }

67.創建目錄副本整合操作 / import java.io.; import java.util.; import javax.swing.; */ JFileChooser Jfc = new JFileChooser("請選擇源路徑"); // 建立選擇檔案對話方塊盒 Jfc Jfc.showDialog(null, %%1); if (!Jfc.getSelectedFile() != null) { return; } String %%1 = Jfc.getSelectedFile().getParent(); Jfc = new JFileChooser("請選擇目標路徑"); // 建立選擇檔案對話方塊盒 Jfc Jfc.showDialog(null, %%1); if (!Jfc.getSelectedFile() != null) { return; } String %%2 = Jfc.getSelectedFile().getParent();

68.打開網頁 //import java.io.*; try{ String command = "C:\Program Files\Internet Explorer\Iexplore.exe "+%%1; Runtime.getRuntime().exec(command); } catch (IOException ex) { ex.printStackTrace(); }

69.刪除空文件夾整合操作 / import java.io.; import java.util.; import javax.swing.; */

70.獲取磁盤所有分區,把結果放在數組drives中 String root; //root代表盤符路徑 for(i=0;i<20;i++) //0-20代表最大的盤符數 { root.Format("%c:\",allfenqu[i]); if(GetDriveType(root)==5) allfenqu[i]='\0'; }

但我用這樣的代碼時結果卻無法去掉光驅盤符,allfenqu[]中還是會包含光驅盤符: String root; for(i=0;i<20;i++) { root=allfenqu[i]+":\"; if(GetDriveType(root)==5) allfenqu[i]='\0'; }

71.激活一個程序或程序關聯的文件 //import java.io.*; try { Runtime.getRuntime().exec(%%1); } catch (IOException e) { e.printStackTrace(); }

72.MP3播放 //必須下載 jmf包 //import javax.media.bean.playerbean.MediaPlayer; //必須下載 jmf 媒體播放包 MediaPlayer player; player = new MediaPlayer(); setLayout(new FlowLayout()); try{ player.setMediaLocation("file:/F:\音樂\mp3\黑白配.mp3");// <<file:/>>不能刪除 音頻文件路徑 } catch (Exception e) { System.err.println("文件不存在"); } player.start(); player.stop();

73.WAV播放 / import javax.sound.sampled.; import java.io.;/ private AudioFormat format; private byte[] samples; private String filename; try { // open the audio input stream AudioInputStream stream =AudioSystem.getAudioInputStream(new File(filename)); format = stream.getFormat(); // get the audio samples samples = getSamples(stream); } catch (UnsupportedAudioFileException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); }

private byte[] getSamples(AudioInputStream audioStream) { // get the number of bytes to read int length = (int)(audioStream.getFrameLength() * format.getFrameSize());

// read the entire stream byte[] samples = new byte[length]; DataInputStream is = new DataInputStream(audioStream); try { is.readFully(samples); } catch (IOException ex) { ex.printStackTrace(); }

// return the samples return samples; }

public void play(InputStream source) {

// use a short, 100ms (1/10th sec) buffer for real-time // change to the sound stream int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10); byte[] buffer = new byte[bufferSize];

// create a line to play to SourceDataLine line; try { DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); line = (SourceDataLine)AudioSystem.getLine(info); line.open(format, bufferSize); } catch (LineUnavailableException ex) { ex.printStackTrace(); return; }

// start the line line.start();

// copy data to the line try { int numBytesRead = 0; while (numBytesRead != -1) { numBytesRead = source.read(buffer, 0, buffer.length); if (numBytesRead != -1) { line.write(buffer, 0, numBytesRead); } } } catch (IOException ex) { ex.printStackTrace(); }

// wait until all data is played, then close the line line.drain(); line.close(); }

throws Exception String filename=%%1; InputStream stream =new ByteArrayInputStream(sound.getSamples()); // play the sound sound.play(stream);

74.寫圖像到剪切板 / import java.awt.; import java.awt.datatransfer.; import java.io.; private final Image image; */ Transferable trans = new Transferable() { public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[] { DataFlavor.imageFlavor }; }

public boolean isDataFlavorSupported(DataFlavor flavor) { return DataFlavor.imageFlavor.equals(flavor); }

public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (isDataFlavorSupported(flavor)) return image; throw new UnsupportedFlavorException(flavor); } }; Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);

75.從剪貼板復制圖像到窗體

76.刪除文件夾下的所有文件且不刪除文件夾下的文件夾 / import java.io.; import java.util.;/ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { fileList.add(files[i]); } } for (File f : fileList) { f.delete(); } }

78.Unicode文件轉GB2312格式 private static boolean isNeedConvert(char para){ return ((para&(0x00FF))!=para); } String dataStr=%%1;"\u9009 \u5173"; int index = 0; StringBuffer buffer = new StringBuffer(); while(index<dataStr.length()) { if(!"\u".equals(dataStr.substring(index,index+2))){ buffer.append(dataStr.charAt(index)); index++; continue; } String charStr = ""; charStr = dataStr.substring(index+2,index+6); char letter = (char) Integer.parseInt(charStr, 16 ); buffer.append(letter); index+=6; } String %%2=buffer.toString();

79.開源程序庫Xercesc-C++代碼工程中內聯 import java.io.; import java.util.; import java.util.regex.*; public class InlineXercesc { private final String filter = ".cpp";

private ArrayList<String> all = new ArrayList<String>();

private LinkedList<String> fal2 = new LinkedList<String>();

private static String CurDir = System.getProperty("user.dir");

private void doSearch(String path) { File filepath = new File(path); if (filepath.exists() && filepath.isDirectory()) { File[] fileArray = filepath.listFiles(); for (File f : fileArray) { if (f.isDirectory()) { doSearch(f.getPath()); } else { if (f.getName().indexOf(filter) >= 0) { for (String file : all) { if (file.substring(file.lastIndexOf("\") + 1) .equals(f.getName())) { fal2.add(f.getAbsolutePath()); } } } } } } }

public InlineXercesc(String lib) throws IOException { String SourceLib = "D:\Desktop\大項目\xerces-c-3.0.1\src"; Pattern pattern = Pattern.compile("include.?" + lib + ".?>"); // 第一個參數為需要匹配的字符串 Matcher matcher = pattern.matcher(""); LinkedList<String> fal = new LinkedList<String>(); File delfile = new File(CurDir); File[] files2 = delfile.listFiles(); for (int l = 0; l < files2.length; l++) { if (files2[l].isDirectory()) { String enumDir = CurDir + "\" + files2[l].getName() + "\"; LinkedList<String> folderList = new LinkedList<String>(); folderList.add(files2[l].getAbsolutePath()); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { String fileStr = files[i].getAbsolutePath(); // 第2個參數開始,均為文件名。 BufferedReader br = null; String line; try { br = new BufferedReader(new FileReader(fileStr)); // 打開文件 } catch (IOException e) { // 沒有打開文件,則產生異常 System.err.println("Cannot read '" + fileStr

  • "': " + e.getMessage()); continue; } StringBuilder sb = new StringBuilder(2048); while ((line = br.readLine()) != null) { // 讀入一行,直到文件結束 matcher.reset(line); // 匹配字符串 if (matcher.find()) { // 如果有匹配的字符串,則輸出 sb.append(line.replace( line.substring(line.indexOf("<"), line.lastIndexOf("/") + 1), "\"").replace('>', '\"')); line = line.substring( line.indexOf("<") + 1, line.lastIndexOf(">")).replace('/', '\'); fal.add(SourceLib + "\" + line); } else { sb.append(line); } sb.append("\r\n"); } br.close(); // 關閉文件 FileWriter fw2 = new FileWriter(fileStr); fw2.write(sb.toString()); fw2.flush(); fw2.close(); } } } while (fal.size() > 0) { String file = fal.poll(); // 第2個參數開始,均為文件名。 String targetPath = enumDir
  • file.substring(file.lastIndexOf("\") + 1); if (!new File(targetPath).exists()) { BufferedReader br = null; String line; try { br = new BufferedReader(new FileReader(file)); // 打開文件 } catch (IOException e) { // 沒有打開文件,則產生異常 System.err.println("Cannot read '" + file + "': "
  • e.getMessage()); continue; } FileWriter fw = new FileWriter(targetPath); while ((line = br.readLine()) != null) { // 讀入一行,直到文件結束 matcher.reset(line); // 匹配字符串 if (matcher.find()) { // 如果有匹配的字符串,則輸出 fal.add(SourceLib
  • "\"
  • line.substring(line.indexOf("<") + 1, line.lastIndexOf(">")).replace( '/', '\')); line = line.replace(line.substring(line .indexOf("<"), line.lastIndexOf("/") + 1), "\""); line = line.replace(">", "\"");

} fw.write(line + "\r\n"); } fw.flush(); fw.close(); br.close(); // 關閉文件

} } LinkedList<String> folderListArr = new LinkedList<String>(); folderListArr.add(CurDir); while (folderListArr.size() > 0) { File file = new File(folderListArr.poll()); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderListArr.add(files[i].getPath()); } else { if (files[i].getName().substring( files[i].getName().lastIndexOf('.')) .equals(".hpp")) all.add(files[i].getAbsoluteFile().toString() .replace(".hpp", ".cpp")); } } } int count = 1; while (count > 0) { doSearch(SourceLib); all.clear(); while (fal2.size() > 0) { String file1 = fal2.poll(); // 第2個參數開始,均為文件名。 String targetPath = enumDir

  • file1.substring(file1.lastIndexOf("\") + 1); if (!new File(targetPath).exists()) { BufferedReader br = null; String line; try { br = new BufferedReader(new FileReader(file1)); // 打開文件 } catch (IOException e) { // 沒有打開文件,則產生異常 System.err.println("Cannot read '" + file1
  • "': " + e.getMessage()); continue; } FileWriter fw; try { fw = new FileWriter(targetPath); while ((line = br.readLine()) != null) { // 讀入一行,直到文件結束 matcher.reset(line); // 匹配字符串 if (matcher.find()) { // 如果有匹配的字符串,則輸出 fal2.add(SourceLib
  • "\"
  • line.substring( line.indexOf('<') + 1, line.lastIndexOf('>')) .replace('/', '\')); all.add(fal2.getLast().replace(".hpp", ".cpp")); line = line.replace(line.substring(line .indexOf('<'), line .lastIndexOf('/') + 1), "\""); line = line.replace('>', '\"'); } fw.write(line + "\r\n"); } fw.flush(); fw.close(); br.close(); // 關閉文件 } catch (IOException e) { e.printStackTrace(); } } } count = all.size(); } } } }

public static void main(String[] args) { try { new InlineXercesc("xercesc"); // 將數據寫入文件 try { FileWriter fw = new FileWriter(CurDir + "\DetailCpp.cmd"); fw.write("copy StdAfx.cpp+.c+.cpp " + CurDir

  • "\StdAfx.cpp && del .c && del .cpp"); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { } } }

80.提取包含頭文件列表 import java.io.; import java.util.regex.; import java.util.; public class InlineExt { private String CurDir = System.getProperty("user.dir"); public InlineExt() { Pattern pattern = Pattern.compile("include.?\".*?.hpp\""); // 第一個參數為需要匹配的字符串 Matcher matcher = pattern.matcher(""); File delfile = new File(CurDir); File[] files2 = delfile.listFiles(); for (int l = 0; l < files2.length; l++) { if (files2[l].isDirectory()) { Set<String> ts = new LinkedHashSet<String>(); File file = new File(files2[l].getPath(), "StdAfx.cpp"); BufferedReader br = null; FileWriter fw = null; String line; try { br = new BufferedReader(new FileReader(file)); // 打開文件 while ((line = br.readLine()) != null) { matcher.reset(line); // 匹配字符串 if (matcher.find()) { // 如果有匹配的字符串,則輸出 ts.add(line.substring(line.indexOf('\"') + 1, line .lastIndexOf('\"'))); } } Iterator<String> it = ts.iterator(); File file2 = new File(files2[l], "ReadMe.txt"); if (file2.exists()) { fw = new FileWriter(file2); while (it.hasNext()) { fw.write("#include \"" + it.next() + "\"\r\n"); } } } catch (IOException e) { // 沒有打開文件,則產生異常 System.err.println("Cannot read '" + file + "': "

  • e.getMessage()); continue; } finally { try { if (br != null) br.close(); if (fw != null) fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { new InlineExt(); } }

81.GB2312文件轉Unicode格式

82.Java程序打包 / import java.io.; import java.util.; private static String className; private static File myFilePath;/ String path=%%1; ProcessBuilder pb = null; myFilePath = new File(path, "conf.txt"); LinkedList<String> folderList = new LinkedList<String>(); folderList.add(path); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { if (files[i].getName().substring(2).contains(".") && files[i].getName().substring( files[i].getName().lastIndexOf('.')) .equals(".java")) {

try { className = files[i].getName().substring(0, files[i].getName().lastIndexOf('.')); if (!myFilePath.exists()) myFilePath.createNewFile(); FileWriter resultFile = new FileWriter(myFilePath); PrintWriter myFile = new PrintWriter(resultFile); myFile.println("Main-Class:" + className); myFile.flush(); myFile.close(); resultFile.close(); pb = new ProcessBuilder("javac", files[i] .getAbsolutePath()

  • " && jar cmf "
  • myFilePath.getAbsolutePath()
  • " "
  • className
  • ".jar "
  • className
  • ".class"); pb.start(); } catch (IOException e) { e.printStackTrace(); } } } } } folderList = new LinkedList<String>(); folderList.add(path); while (folderList.size() > 0) { File file = new File((String)folderList.poll()); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { folderList.add(files[i].getPath()); } else { if (files[i].getName().substring(2).contains(".") && files[i].getName().substring( files[i].getName().lastIndexOf('.')) .equals(".class")) { files[i].delete(); } } } }

83.UTF-8文件轉Unicode格式 /**

  • utf-8 轉換成 unicode
  • @author fanhui
  • 2007-3-15
  • @param inStr
  • @return */ public static String utf8ToUnicode(String inStr) { char[] myBuffer = inStr.toCharArray();

StringBuffer sb = new StringBuffer(); for (int i = 0; i < inStr.length(); i++) { UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]); if(ub == UnicodeBlock.BASIC_LATIN){ //英文及數字等 sb.append(myBuffer[i]); }else if(ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){ //全角半角字符 int j = (int) myBuffer[i] - 65248; sb.append((char)j); }else{ //漢字 short s = (short) myBuffer[i]; String hexS = Integer.toHexString(s); String unicode = "\u"+hexS; sb.append(unicode.toLowerCase()); } } return sb.toString(); }

84.創建PDF文檔 / http://www.lowagie.com/iText/ http://jaist.dl.sourceforge.net/sourceforge/itext/itext-1.4.jar http://itextdocs.lowagie.com/downloads/iTextAsian.jar http://itextdocs.lowagie.com/downloads/iTextAsianCmaps.jar import java.io.; import com.lowagie.text.; import com.lowagie.text.pdf.PdfWriter;/ Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream(%%1)); //"HelloWorld.pdf" // 添加PDF文檔的一些信息 document.addTitle("Hello World example"); document.addAuthor("Bruno Lowagie"); document.addSubject("This example explains how to add metadata."); document.addKeywords("iText, Hello World, step 3, metadata"); document.addCreator("My program using iText"); document.open(); // 插入一個段落 document.add(new Paragraph("Hello World!"));

} catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); }

// 關閉打開的文檔 document.close();

三、中文問題:

由于iText不支持東亞語言,我們下載了iTextAsian.jar 以后,就可以在PDF里面寫中文:

/**

  • AsianTest.java / import java.io.; import com.lowagie.text.; import com.lowagie.text.pdf.; import java.awt.*;

public class AsianTest{

public static void main(String[] args) {

// 創建一個Document對象 Document document = new Document();

try {

// 生成名為 AsianTest.pdf 的文檔 PdfWriter.getInstance(document, new FileOutputStream("AsianTest.pdf"));

/** 新建一個字體,iText的方法

  • STSongStd-Light 是字體,在iTextAsian.jar 中以property為后綴
  • UniGB-UCS2-H 是編碼,在iTextAsian.jar 中以cmap為后綴
  • H 代表文字版式是 橫版, 相應的 V 代表 豎版 */ BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);

Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);

// 打開文檔,將要寫入內容 document.open();

// 插入一個段落 Paragraph par = new Paragraph("我們",fontChinese);

document.add(par);

} catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); }

// 關閉打開的文檔 document.close(); } }

就可以顯示中文了。

四、其他問題:(應導入相應的包)

  1. 換頁:

document.newPage();

  1. 表格:

// 設置 Table Table aTable = new Table(3); int width[] = {25,25,50}; aTable.setWidths(width); aTable.setWidth(80); // 占頁面寬度 80%

aTable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT); aTable.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE); aTable.setAutoFillEmptyCells(true); //自動填滿 aTable.setPadding(1); aTable.setSpacing(1); aTable.setDefaultCellBorder(0); aTable.setBorder(0);

Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據", fontChinese )); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setRowspan(3); aTable.addCell(cell);

aTable.addCell(new Cell("#1")); aTable.addCell(new Cell("#2")); aTable.addCell(new Cell("#3"));

aTable.addCell(new Cell("#4")); aTable.addCell(new Cell("#5")); aTable.addCell(new Cell("#6"));

document.add(aTable);

  1. 圖片:

// 可以是絕對路徑,也可以是URL Image img = Image.getInstance("logo.gif");

// Image image = Image.getInstance(new URL(http://xxx.com/logo.jpg));

img.setAbsolutePosition(0, 0);

document.add(img);

五、參考文檔:

iText http://www.lowagie.com/iText/

iText API: http://itext.sourceforge.net/docs/

http://www.sentom.net/list.asp?id=42

85.創建Word文檔 / import java.awt.; import java.io.; import com.lowagie.text.; import com.lowagie.text.pdf.; import com.lowagie.text.rtf.; */ public void createDocContext(String file) throws DocumentException, IOException { // 設置紙張大小 Document document = new Document(PageSize.A4); // 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中 RtfWriter2.getInstance(document, new FileOutputStream(file)); document.open(); // 設置中文字體 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 標題字體風格 Font titleFont = new Font(bfChinese, 12, Font.BOLD); // 正文字體風格 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); Paragraph title = new Paragraph("標題"); // 設置標題格式對齊方式 title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title);

String contextString = "iText是一個能夠快速產生PDF文件的java類庫。"

  • " \n"// 換行
  • "iText的java類對于那些要產生包含文本,"
  • "表格,圖形的只讀文檔是很有用的。它的類庫尤其與java Servlet有很好的給合。"
  • "使用iText與PDF能夠使你正確的控制Servlet的輸出。"; Paragraph context = new Paragraph(contextString); // 正文格式左對齊 context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); // 離上一段落(標題)空的行數 context.setSpacingBefore(5); // 設置第一行空的列數 context.setFirstLineIndent(20); document.add(context);

//利用類FontFactory結合Font和Color可以設置各種各樣字體樣式 /**

  • Font.UNDERLINE 下劃線,Font.BOLD 粗體 */ Paragraph underline = new Paragraph("下劃線的實現", FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(0, 0, 255))); document.add(underline);

// 設置 Table 表格 Table aTable = new Table(3); int width[] = {25,25,50}; aTable.setWidths(width);//設置每列所占比例 aTable.setWidth(90); // 占頁面寬度 90%

aTable.setAlignment(Element.ALIGN_CENTER);//居中顯示 aTable.setAlignment(Element.ALIGN_MIDDLE);//縱向居中顯示 aTable.setAutoFillEmptyCells(true); //自動填滿 aTable.setBorderWidth(1); //邊框寬度 aTable.setBorderColor(new Color(0, 125, 255)); //邊框顏色 aTable.setPadding(2);//襯距,看效果就知道什么意思了 aTable.setSpacing(3);//即單元格之間的間距 aTable.setBorder(2);//邊框

//設置表頭 /**

  • cell.setHeader(true);是將該單元格作為表頭信息顯示;
  • cell.setColspan(3);指定了該單元格占3列;
  • 為表格添加表頭信息時,要注意的是一旦表頭信息添加完了之后,
  • 必須調用 endHeaders()方法,否則當表格跨頁后,表頭信息不會再顯示 */ Cell haderCell = new Cell("表格表頭"); haderCell.setHeader(true); haderCell.setColspan(3); aTable.addCell(haderCell); aTable.endHeaders();

Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN); Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據", fontChinese )); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorderColor(new Color(255, 0, 0)); cell.setRowspan(2); aTable.addCell(cell);

aTable.addCell(new Cell("#1")); aTable.addCell(new Cell("#2")); aTable.addCell(new Cell("#3")); aTable.addCell(new Cell("#4")); Cell cell3 = new Cell(new Phrase("一行三列數據", fontChinese )); cell3.setColspan(3); cell3.setVerticalAlignment(Element.ALIGN_CENTER); aTable.addCell(cell3);

document.add(aTable); document.add(new Paragraph("\n")); //添加圖片 Image img=Image.getInstance("d:\img01800.jpg"); img.setAbsolutePosition(0, 0); img.setAlignment(Image.RIGHT);//設置圖片顯示位置 img.scaleAbsolute(12,35);//直接設定顯示尺寸 img.scalePercent(50);//表示顯示的大小為原尺寸的50% img.scalePercent(25, 12);//圖像高寬的顯示比例 img.setRotation(30);//圖像旋轉一定角度 document.add(img);

document.close(); }

/**

  • @param args */ public static void main(String[] args) { CreateWordDemo word = new CreateWordDemo(); String file = "c:/demo1.doc"; try { word.createDocContext(file); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

/ 內部線程類 / class Son extends Thread { private ICallBack event; public Son(ICallBack callback) { event=callback; } public void run() { try { java.text.SimpleDateFormat fmt=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while(true) { Thread.currentThread().sleep(3000); event.output(fmt.format(new java.util.Date())); Thread.currentThread().sleep(3000); } } catch (Exception e) { } } }

/ 回調接口 / interface ICallBack { public void output(); }

86.快速高效的文件加密 / import java.util.; import java.io.; import javax.swing.; */ private static final String CharSet = "0123456789ABCDEF"; private static final int a[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; private static final int b[] = { 3, 3, 3, 5, 5, 3, 3, 3, 5, 3, 5, 3, 7, 3, 11, 7, 11, 3, 5, 3, 3, 3, 7, 5, 5, 3, 7, 3, 11, 13, 3 }; private static final int c[] = { 3, 5, 7, 5, 7, 11, 13, 19, 17, 29, 19, 31, 17, 41, 11, 19, 13, 47, 29, 53, 59, 67, 29, 41, 43, 71, 31, 73, 23, 23, 101 }; private static final int d[] = { 5, 7, 11, 13, 17, 17, 19, 29, 43, 43, 47, 47, 59, 61, 61, 67, 71, 71, 73, 79, 89, 101, 101, 103, 107, 107, 109, 109, 127, 149, 151 }; private static int ka, kb, kc, kd;

private static void init() { Date t1 = new Date(); Date t2 = new Date(79, 8, 23, 8, 30, 0); // "9-23-1979 8:30" int GetDays = 0; int result = 0; Calendar start = new GregorianCalendar(); Calendar end = new GregorianCalendar(); start.setTime(t2); end.setTime(t1); Calendar startCalendar = start; int startYear = start.get(Calendar.YEAR); int startMonth = start.get(Calendar.MONTH); int startDayOfM = start.get(Calendar.DAY_OF_MONTH); int startDayOfY = start.get(Calendar.DAY_OF_YEAR);

Calendar endCalendar = end; int endYear = end.get(Calendar.YEAR); int endMonth = end.get(Calendar.MONTH); int endDayOfM = end.get(Calendar.DAY_OF_MONTH); int endDayOfY = end.get(Calendar.DAY_OF_YEAR); int startLastYear = 0, startLastDayOfY = 0; if (endYear == startYear) { startLastYear = endDayOfY < startDayOfY ? startYear : endYear; startLastDayOfY = endDayOfY < startDayOfY ? startDayOfY : endDayOfY; } else if (endYear < startYear) { startLastYear = startYear; startLastDayOfY = startDayOfY; } else { startLastYear = endYear; startLastDayOfY = endDayOfY; }

if ((endYear == startYear && endDayOfY == startDayOfY) || (startLastYear == startYear && startLastDayOfY == startDayOfY)) { GetDays = result; } else if (startYear == endYear) { GetDays = endDayOfY - startDayOfY; } else { Calendar tmp = Calendar.getInstance(); result = startCalendar.getActualMaximum(Calendar.DAY_OF_YEAR)

  • startDayOfY; for (int i = startYear + 1; i < endYear; i++) { tmp.set(i, tmp.get(Calendar.MONTH), tmp .get(Calendar.DAY_OF_MONTH)); result += tmp.getActualMaximum(Calendar.DAY_OF_YEAR); } result += end.get(Calendar.DAY_OF_YEAR); GetDays = result; } Random rand = new Random(GetDays * 8 - 55); int r = rand.nextInt(GetDaysInMoths(t2.getMonth() + 1)); r=0; ka = a[r]; kb = b[r]; kc = c[r]; kd = d[r]; }

private static int GetDaysInMoths(int nMoths) { switch (nMoths) { case 2: { int nYear = new Date().getYear() + 1900; if ((nYear % 4 == 0) && (nYear % 400) != 0) { return 29; } else { return 28; } } case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; default: return -1; } }

private static int niyuan(int m, int n) { int a, b, c, d, t, yu = 0, shang, mod; a = m; b = n; mod = a; c = 0; d = 1; while (b < 0) b += a; if (a % b == 0 || b % 2 == 0) return 0; while (b != 1) { t = a % b; shang = a / b; a = b; b = t; yu = c - shang * d; c = d; d = yu; } if (yu < 0) yu += mod; return yu; } JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File("E:")); chooser .addChoosableFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".zip") || f.isDirectory(); }

public String getDescription() { return "Compress Files(*.zip)"; } }); chooser .addChoosableFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory(); }

public String getDescription() { return "Text Files(*.txt)"; } }); chooser .addChoosableFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".exe") || f.isDirectory(); }

public String getDescription() { return "Executeable Files(*.exe)"; } }); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { String name = chooser.getSelectedFile().getPath(); JFileChooser chooser2 = new JFileChooser(); chooser2.setCurrentDirectory(new File("E:")); if (name.substring(name.lastIndexOf('.')).equals(".txt")) { char[] sRead = new char[10240]; char[] hexstr = new char[10240]; char[] pbuf = new char[20480]; char[] rRead = new char[20480]; char[] out2 = new char[10240]; chooser2 .addChoosableFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith( ".zip") || f.isDirectory(); }

public String getDescription() { return "Compress Files(*.zip)"; } }); chooser2 .addChoosableFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith( ".exe") || f.isDirectory(); }

public String getDescription() { return "Executeable Files(.exe)"; } }); if (chooser2.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { init(); FileOutputStream fw = null; try { FileInputStream fis = new FileInputStream(chooser .getSelectedFile()); fw = new FileOutputStream(chooser2.getSelectedFile()); byte[] buffer = new byte[20480]; int js = 0, tmp; int aany, ddny; int r00 = ka kc kd; int r01 = -ka kb kc; int r10 = -kb kc kc; int r11 = ka kb kc; int x00 = ka ka kc kd - ka kb kc kc; int x11 = ka kb kc kd - kb kb kc kc; while (x00 % 2 == 0) { x00 /= 2; r00 /= 2; r01 /= 2; } while (x11 % 2 == 0) { x11 /= 2; r10 /= 2; r11 /= 2; } aany = x00; ddny = x11; if (niyuan(1024, aany) != 0 && niyuan(1024, ddny) != 0) { int kn00 = r00 niyuan(1024, x00); int kn01 = r01 niyuan(1024, x00); int kn10 = r10 niyuan(1024, x11); int kn11 = r11 niyuan(1024, x11); ka = kn00; kb = kn01; kc = kn10; kd = kn11; } else { JOptionPane.showMessageDialog(null, "無逆矩陣!"); System.exit(0); } while (ka < 0) ka += 1024; while (kb < 0) kb += 1024; while (kc < 0) kc += 1024; while (kd < 0) kd += 1024; ka %= 1024; kb %= 1024; kc %= 1024; kd %= 1024; try { int byteread; while ((byteread = fis.read(buffer)) != -1) { int nLen = byteread / 2; byte[] sming = new byte[nLen]; String chs = new String(buffer, "US-ASCII"); for (int i = 0; i < nLen; i++) { byte bTmp; if (byteread < 2) bTmp = -1; bTmp = (byte) (CharSet.indexOf(chs .substring(i 2, i 2 + 1)) 16 + CharSet .indexOf(chs.substring(i 2 + 1, i 2 + 2))); sming[i] = bTmp; } if (nLen % 2 != 0) js = 1; for (int i = 0; i < nLen - 1; i += 2) { char c1 = (char) sming[i]; char c2 = (char) sming[i + 1]; tmp = ka c1 + kc c2; while (tmp < 0) tmp += 1024; char s1 = (char) (tmp % 1024); fw.write(s1); tmp = kb c1 + kd c2; while (tmp < 0) tmp += 1024; char s2 = (char) (tmp % 1024); fw.write(s2); } if (js == 1) { char c3 = (char) ((sming[nLen - 1] - 4) % 1024); fw.write(c3); } } fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e1) { e1.printStackTrace(); } if(!chooser2.getFileFilter().accept(chooser2.getSelectedFile())){ if(chooser2.getFileFilter().getDescription().equals("Compress Files(.zip)")) chooser2.getSelectedFile().renameTo(new File(chooser2.getSelectedFile().getAbsolutePath()+".zip")); else if(chooser2.getFileFilter().getDescription().equals("Executeable Files(.exe)")) chooser2.getSelectedFile().renameTo(new File(chooser2.getSelectedFile().getAbsolutePath()+".exe")); }

} } else { chooser2 .setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith( ".txt") || f.isDirectory(); }

public String getDescription() { return "Text Files"; } }); if (chooser2.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

if (chooser2.getSelectedFile() != null) { init(); File oldfile = chooser.getSelectedFile(); FileInputStream inStream = null; FileWriter fw = null; try { if (oldfile.exists()) { inStream = new FileInputStream(oldfile); fw = new FileWriter(chooser2.getSelectedFile()); byte[] sRead = new byte[10240]; int byteread; while ((byteread = inStream.read(sRead)) != -1) { StringBuilder smi = new StringBuilder( byteread 2); int js = 0; if (byteread % 2 != 0) js = 1; for (int i = 0; i < byteread - 1; i += 2) { char c1 = (char) sRead[i]; char c2 = (char) sRead[i + 1]; int tmp = ka c1 + kc c2; while (tmp < 0) tmp += 1024; byte s1 = (byte) (tmp % 1024); int js1 = (int) s1 >> 4 & 0xf; smi.append(CharSet.substring(js1, js1 + 1)); int ks1 = s1 & 0xf; smi.append(CharSet.substring(ks1, ks1 + 1)); tmp = kb c1 + kd * c2; while (tmp < 0) tmp += 1024; byte s2 = (byte) (tmp % 1024); int js2 = (int) s2 >> 4 & 0xf; smi.append(CharSet.substring(js2, js2 + 1)); int ks2 = s2 & 0xf; smi.append(CharSet.substring(ks2, ks2 + 1)); } if (js == 1) { byte s3 = (byte) ((sRead[byteread - 1] - 4) % 1024); int js3 = (int) s3 >> 4 & 0xf; smi.append(CharSet.substring(js3, js3 + 1)); int ks3 = (int) s3 & 0xf; smi.append(CharSet.substring(ks3, ks3 + 1)); } fw.write(smi.toString()); } fw.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { fw.close(); inStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(!chooser2.getFileFilter().accept(chooser2.getSelectedFile())) chooser2.getSelectedFile().renameTo(new File(chooser2.getSelectedFile().getAbsolutePath()+".txt")); } } } }

87.菜單勾選/取消開機自啟動程序

88.菜單勾選/取消自動登錄系統

89.模擬鍵盤輸入字符串 / import java.awt.; import java.awt.event.; throws Exception{/ static Robot robot; static{ try { robot = new Robot(); } catch (AWTException e) {} }

static void sendKey(String ks){ KeyStore k = KeyStore.findKeyStore(ks); if(k!=null){ if(k.upCase) upCase(k.v); else sendKey(k.v); } else{ for(int i=0; i<ks.length(); i++){ char c = ks.charAt(i); if(c>='0'&&c<='9'){ sendKey(c); } else if(c>='a'&&c<='z'){ sendKey(c-32); } else if(c>='A'&&c<='Z'){ upCase(c); } } } } private static void upCase(int kc){ robot.keyPress(KeyEvent.VK_SHIFT); robot.keyPress(kc); robot.keyRelease(kc); robot.keyRelease(KeyEvent.VK_SHIFT); } private static void sendKey(int kc){ robot.keyPress(kc); robot.keyRelease(kc); } static class KeyStore{ //special keys final static KeyStore[] sp = { new KeyStore("{Tab}",KeyEvent.VK_TAB),//tab new KeyStore("{Enter}",KeyEvent.VK_ENTER),//enter new KeyStore("{PUp}",KeyEvent.VK_PAGE_UP),//page up new KeyStore("{<}",KeyEvent.VK_LESS),//< new KeyStore("{Up}",KeyEvent.VK_UP),//up key new KeyStore("{At}",KeyEvent.VK_AT,true),//@ new KeyStore("{Dollar}",KeyEvent.VK_DOLLAR,true),//$ };

String k; int v; boolean upCase; KeyStore(String k,int v){ this(k,v,false); }

KeyStore(String s,int i,boolean up){ k=s; v=i; upCase=up; } static KeyStore findKeyStore(String k){ for(int i=0; i<sp.length; i++){ if(sp[i].k.equals(k)) return sp[i]; } return null; } } Thread.sleep(1000); sendKey("{Tab}");//tab sendKey("{<}");//< sendKey("abcd123AHahahAA");//abcd123AHahahAA sendKey("{At}");//@ sendKey("{Dollar}");//$ sendKey("{Up}");//up arrow

90.提取PDF文件中的文本 //http://incubator.apache.org/pdfbox/ / import java.io.; import org.pdfbox.pdfparser.; import org.pdfbox.pdmodel.; import org.pdfbox.util.;/ public class SimplePDFReader { /**

  • simply reader all the text from a pdf file.
  • You have to deal with the format of the output text by yourself.
  • 2008-2-25
  • @param pdfFilePath file path
  • @return all text in the pdf file */ public static String getTextFromPDF(String pdfFilePath) { String result = null; FileInputStream is = null; PDDocument document = null; try { is = new FileInputStream(pdfFilePath); PDFParser parser = new PDFParser(is); parser.parse(); document = parser.getPDDocument(); PDFTextStripper stripper = new PDFTextStripper(); result = stripper.getText(document); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (document != null) { try { document.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return result; } } 得到PDF的文本內容之后,自己根據文件的格式,取得想要的文本(這里我找的就是文章的標題,在文本中恰巧都是文件的第一行的內容),然后通過java的File相關api,對文件進行更名操作。 import java.io.File; import java.io.FilenameFilter;

public class PaperNameMender { public static void changePaperName(String filePath) { //使用SimplePDFReader得到pdf文本 String ts = SimplePDFReader.getTextFromPDF(filePath); //取得一行內容 String result = ts.substring(0, ts.indexOf('\n')); //得到源文件名中的最后一個逗點.的位置 int index = filePath.indexOf('.'); int nextIndex = filePath.indexOf('.', index + 1); while(nextIndex != -1) { index = nextIndex; nextIndex = filePath.indexOf('.', index + 1); } //合成新文件名 String newFilename = filePath.substring(0, index) + " " + result.trim() + ".pdf"; File originalFile = new File(filePath); //修改文件名 originalFile.renameTo(new File(newFilename)); } }

91.操作內存映射文件 / import java.io.; import java.nio.; import java.nio.channels.; */ private static int length = 0x8FFFFFF; // 128 Mb MappedByteBuffer out = new RandomAccessFile("test.dat", "rw").getChannel() .map(FileChannel.MapMode.READ_WRITE, 0, length); for(int i = 0; i < length; i++) out.put((byte)'x'); for(int i = length/2; i < length/2 + 6; i++) //(char)out.get(i);

92.重定向windows控制臺程序的輸出信息 92.1 取得Runtime.getRuntime().exec("cmd /c dir")的輸入輸出 //import java.io.*; try { String command = "cmd /c dir"; Process proc = Runtime.getRuntime().exec(command); InputStreamReader ir = new InputStreamReader(proc.getInputStream()); LineNumberReader lnr = new LineNumberReader(ir); String line; while( (line = lnr.readLine()) != null) { //line } } catch (IOException e) { e.printStackTrace(); }

92.2 利用ProcessBuilder來創建Process對象,執行外部可執行程序 // //Eg1: // String address = null; // // String os = System.getProperty("os.name"); // System.out.println(os); // // if (os != null) { // if (os.startsWith("Windows")) { // try { // ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all"); // Process p = pb.start(); // // BufferedReader br = new BufferedReader( // new InputStreamReader(p.getInputStream())); // // String line; // while ((line = br.readLine()) != null) { // if (line.indexOf("Physical Address") != -1) { // int index = line.indexOf(":"); // address = line.substring(index + 1); // break; // } // } // br.close(); // address = address.trim(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } else if (os.startsWith("Linux")) { // try { // ProcessBuilder pb = new ProcessBuilder( // // "ifconfig", "/all"); // // Process p = pb.start(); // BufferedReader br = new BufferedReader( // new InputStreamReader(p.getInputStream())); // String line; // while ((line = br.readLine()) != null) { // int index = line.indexOf("硬件地址"); // if (index != -1) { // address = line.substring(index + 4); // break; // } // } // br.close(); // address = address.trim(); // } catch (IOException e) { // e.printStackTrace(); // } // } // }

// //Eg2: // try { // Process proc; // proc = Runtime.getRuntime().exec("cmd.exe"); // BufferedReader read = new BufferedReader(new InputStreamReader(proc // .getInputStream())); // new Thread(new Echo(read)).start(); // // PrintWriter out = new PrintWriter(new OutputStreamWriter(proc // .getOutputStream())); // BufferedReader in = new BufferedReader(new InputStreamReader( // System.in)); // String instr = in.readLine(); // while (!"exit".equals(instr)) { // instr = in.readLine(); // out.println(instr); // out.flush(); // } // // in.readLine(); // read.close(); // out.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // }

} }

class Echo implements Runnable { private BufferedReader read;

public Echo(BufferedReader read) { read = read; }

public void run() { try { String line = read.readLine(); while (line != null) { //line line = read.readLine(); } } catch (IOException ex) { ex.printStackTrace(); } } }

93.序列化 //import java.io.*; class tree implements Serializable{ public tree left; public tree right; public int id; public int level;

private static int count = 0 ;

public tree( int depth) { id = count ++ ; level = depth; if (depth > 0 ) { left = new tree(depth - 1 ); right = new tree(depth - 1 ); } }

public void print( int levels) { for ( int i = 0 ; i < level; i ++ ) System.out.print( " " ); System.out.println( " node " + id);

if (level <= levels && left != null ) left.print(levels);

if (level <= levels && right != null ) right.print(levels); } } try { // / 創建一個文件寫入序列化樹。 / FileOutputStream ostream = new FileOutputStream(%%1); // / 創建輸出流 / ObjectOutputStream p = new ObjectOutputStream(ostream);

/*/ / 創建一個二層的樹。 */ tree base = new tree( 2 );

p.writeObject(base); // 將樹寫入流中。 p.writeObject( " LiLy is 惠止南國 " ); p.flush(); ostream.close(); // 關閉文件。 } catch (Exception ex) { ex.printStackTrace(); } }

94.反序列化 //import java.io.*; class tree implements Serializable{ public tree left; public tree right; public int id; public int level;

private static int count = 0 ;

public tree( int depth) { id = count ++ ; level = depth; if (depth > 0 ) { left = new tree(depth - 1 ); right = new tree(depth - 1 ); } }

public void print( int levels) { for ( int i = 0 ; i < level; i ++ ) System.out.print( " " ); System.out.println( " node " + id);

if (level <= levels && left != null ) left.print(levels);

if (level <= levels && right != null ) right.print(levels); } } try { FileInputStream istream = new FileInputStream(%%1); ObjectInputStream q = new ObjectInputStream(istream);

/*/ / 讀取樹對象,以及所有子樹 */ tree new_tree = (tree)q.readObject();

new_tree.print( 2 ); // 打印出樹形結構的最上面 2級 String name = (String)q.readObject(); System.out.println( " \n " + name); } catch (Exception ex) { ex.printStackTrace(); } }

95.報表相關 //http://www.jfree.org/jfreechart/ / import java.io.; import java.awt.; import org.jfree.chart.; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.;/ String title = "夢澤科技員工學歷情況統計"; DefaultPieDataset piedata = new DefaultPieDataset(); piedata.setValue("大專", 8.1); piedata.setValue("大學", 27.6); piedata.setValue("碩士", 53.2); piedata.setValue("博士及以上", 19.2); piedata.setValue("大專以下", 1.9); JFreeChart chart = ChartFactory.createPieChart(title, piedata, true, true, true); chart.setTitle(new TextTitle(title, new Font("宋體", Font.BOLD, 25))); chart.addSubtitle(new TextTitle("最后更新日期:2005年5月19日", new Font("楷書", Font.ITALIC, 18))); chart.setBackgroundPaint(Color.white); try { ChartUtilities.saveChartAsJPEG(new File("PieChart.jpg"), chart, 360, 300); } catch (IOException exz) { System.err.print("....Cant′t Create image File"); }

96.全屏幕截取 / import java.awt.; import java.awt.image.; import java.io.; import javax.imageio.;/ try{ Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); BufferedImage screenshot = (new Robot()) .createScreenCapture(new Rectangle(0, 0, (int) d.getWidth(), (int) d.getHeight())); // 根據文件前綴變量和文件格式變量,自動生成文件名 String name = %%1 + "."

  • %%2; //"png" File f = new File(name); ImageIO.write(screenshot, %%2, f); } catch (Exception ex) { ex.printStackTrace(); }

97.區域截屏 / import java.awt.; import java.awt.image.; import java.io.; import java.util.; import javax.imageio.; / interface ImageType { public final static String JPG = "JPG"; public final static String PNG = "PNG"; public final static String GIF = "GIF"; } public class ScreenDumpHelper { /** ScreenDumpHelper 靜態對象 / private static ScreenDumpHelper defaultScreenDumpHelper; public static ScreenDumpHelper getDefaultScreenDumpHelper() { if (defaultScreenDumpHelper == null) return new ScreenDumpHelper(); return defaultScreenDumpHelper; } public ScreenDumpHelper() { Dimension dime = Toolkit.getDefaultToolkit().getScreenSize(); setScreenArea(new Rectangle(dime)); } private Rectangle screenArea;

public Rectangle getScreenArea() { return screenArea; }

public void setScreenArea(Rectangle screenArea) { screenArea = screenArea; }

public void setScreenArea(int x, int y, int width, int height) { screenArea = new Rectangle(x, y, width, height); } private BufferedImage getBufferedImage() throws AWTException { BufferedImage imgBuf = null; ; imgBuf = (new Robot()).createScreenCapture(getScreenArea()); return imgBuf; }

/**

  • 將 圖像數據寫到 輸出流中去,方便再處理 *
  • @param fileFormat
  • @param output
  • @return */ public boolean saveToOutputStream(String fileFormat, OutputStream output) { try { ImageIO.write(getBufferedImage(), fileFormat, output); } catch (AWTException e) { return false; // e.printStackTrace(); } catch (IOException e) { return false; // e.printStackTrace(); } return true; }

/**

  • 根據文件格式 返回隨機文件名稱 *
  • @param fileFormat
  • @return */ public String getRandomFileName(String fileFormat) { return "screenDump_" + (new Date()).getTime() + "." + fileFormat; }

/**

  • 抓取 指定區域的截圖 到指定格式的文件中 -- 這個函數是核心,所有的都是圍繞它來展開的 * 圖片的編碼并不是以后綴名來判斷: 比如s.jpg
  • 如果其采用png編碼,那么這個圖片就是png格式的 *
  • @param fileName
  • @param fileFormat
  • @return boolean */ public boolean saveToFile(String fileName, String fileFormat) { if (fileName == null) fileName = getRandomFileName(fileFormat); try { FileOutputStream fos = new FileOutputStream(fileName); saveToOutputStream(fileFormat, fos); } catch (FileNotFoundException e) { System.err.println("非常規文件或不能創建抑或覆蓋此文件: " + fileName); } return true; }

/**

  • 抓取 指定 Rectangle 區域的截圖 到指定格式的文件中 *
  • @param fileName
  • @param fileFormat
  • @param screenArea
  • @return */ public boolean saveToFile(String fileName, String fileFormat, Rectangle screenArea) { setScreenArea(screenArea); return saveToFile(fileName, fileFormat); }

/**

  • 抓取 指定區域的截圖 到指定格式的文件中 *
  • @param fileName
  • @param fileFormat
  • @param x
  • @param y
  • @param width
  • @param height */ public boolean saveToFile(String fileName, String fileFormat, int x, int y, int width, int height) { setScreenArea(x, y, width, height); return saveToFile(fileName, fileFormat); }

/**

  • 將截圖使用 JPG 編碼 *
  • @param fileName */ public void saveToJPG(String fileName) { saveToFile(fileName, ImageType.JPG); }

public void saveToJPG(String fileName, Rectangle screenArea) { saveToFile(fileName, ImageType.JPG, screenArea); }

public void saveToJPG(String fileName, int x, int y, int width, int height) { saveToFile(fileName, ImageType.JPG, x, y, width, height); }

/**

  • 將截圖使用 PNG 編碼 *
  • @param fileName */ public void saveToPNG(String fileName) { saveToFile(fileName, ImageType.PNG); }

public void saveToPNG(String fileName, Rectangle screenArea) { saveToFile(fileName, ImageType.PNG, screenArea); }

public void saveToPNG(String fileName, int x, int y, int width, int height) { saveToFile(fileName, ImageType.PNG, x, y, width, height); }

public void saveToGIF(String fileName) { throw new UnsupportedOperationException("不支持保存到GIF文件"); // saveToFile(fileName, ImageType.GIF); }

/**

  • @param args / public static void main(String[] args) { for (int i = 0; i < 5; i++) ScreenDumpHelper.getDefaultScreenDumpHelper().saveToJPG(null, i 150, i * 150, 400, 300); } }

98.計算文件MD5值 / import java.io.; import java.math.; import java.security.; import java.util.;/ File file=new File(%%1); if (!file.isFile()){ return null; } MessageDigest digest = null; FileInputStream in=null; byte buffer[] = new byte[1024]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer, 0, 1024)) != -1) { digest.update(buffer, 0, len); } in.close(); } catch (Exception e) { e.printStackTrace(); return null; } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); }

99.計算獲取文件夾中文件的MD5值 / import java.io.; import java.math.; import java.security.; import java.util.;/ public static String getFileMD5(File file) { if (!file.isFile()){ return null; } MessageDigest digest = null; FileInputStream in=null; byte buffer[] = new byte[1024]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer, 0, 1024)) != -1) { digest.update(buffer, 0, len); } in.close(); } catch (Exception e) { e.printStackTrace(); return null; } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); } /**

  • 獲取文件夾中文件的MD5值
  • @param file
  • @param listChild ;true遞歸子目錄中的文件
  • @return */ public static Map<String, String> getDirMD5(File file,boolean listChild) { if(!file.isDirectory()){ return null; } //<filepath,md5> Map<String, String> map=new HashMap<String, String>(); String md5; File files[]=file.listFiles(); for(int i=0;i<files.length;i++){ File f=files[i]; if(f.isDirectory()&&listChild){ map.putAll(getDirMD5(f, listChild)); } else { md5=getFileMD5(f); if(md5!=null){ map.put(f.getPath(), md5); } } } return map; } getDirMD5(%%1,%%2);

100.復制一個目錄下所有文件到一個文件夾中 / import java.io.; import java.util.;/ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); LinkedList<String> folderList2 = new LinkedList<String>(); folderList2.add(%%2+ %%1.substring(%%1.lastIndexOf("\"))); while (folderList.size() > 0) { File folders = new File(folderList.peek()); String[] file = folders.list(); File temp = null; try { for (int i = 0; i < file.length; i++) { if (folderList.peek().endsWith(File.separator)) { temp = new File(folderList.peek() + File.separator

  • file[i]); } else { temp = new File(folderList.peek() + File.separator
  • file[i]); } if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream( new File(%%2,temp.getName())); byte[] b = new byte[10240]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if (temp.isDirectory()) {// 如果是子文件夾 for (File f : temp.listFiles()) { if (f.isDirectory()) { folderList.add(f.getPath()); folderList2.add(folderList2.peek()
  • File.separator + f.getName()); } } } } } catch (IOException e) { System.err.println("復制整個文件夾內容操作出錯"); } folderList.removeFirst(); folderList2.removeFirst(); }

101.移動一個目錄下所有文件到一個文件夾中 / import java.io.; import java.util.;/ LinkedList<String> folderList = new LinkedList<String>(); folderList.add(%%1); LinkedList<String> folderList2 = new LinkedList<String>(); folderList2.add(%%2 + %%1.substring(%%1.lastIndexOf("\"))); while (folderList.size() > 0) { File folders = new File(folderList.peek()); String[] file = folders.list(); File temp = null; try { for (int i = 0; i < file.length; i++) { if (folderList.peek().endsWith(File.separator)) { temp = new File(folderList.peek() + File.separator

  • file[i]); } else { temp = new File(folderList.peek() + File.separator
  • file[i]); } if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream( new File(%%2,temp.getName())); byte[] b = new byte[10240]; int len; while ((len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); if (!temp.delete()) //刪除單個文件操作出錯 } if (temp.isDirectory()) {// 如果是子文件夾 for (File f : temp.listFiles()) { if (f.isDirectory()) { folderList.add(f.getPath()); folderList2.add(folderList2.peek()
  • File.separator + f.getName()); } } } } } catch (Exception e) { //復制整個文件夾內容操作出錯 e.printStackTrace(); } folderList.removeFirst(); folderList2.removeFirst(); } File f = new File(%%1); if (!f.delete()) { for (File file : f.listFiles()) { if (file.list().length == 0) { file.delete(); } } }

102.文件RSA高級加密 / import javax.crypto.; import java.security.; import java.security.spec.; import java.security.interfaces.; import java.io.; import java.math.BigInteger; */ /**

  • 生成密鑰對

  • @return KeyPair

  • @throws EncryptException

*/

public static KeyPair generateKeyPair() throws EncryptException {

try {

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",

new org.bouncycastle.jce.provider.BouncyCastleProvider());

final int KEY_SIZE = 1024;//沒什么好說的了,這個值關系到塊加密的大小,可以更改,但是不要太大,否則效率會低

keyPairGen.initialize(KEY_SIZE, new SecureRandom());

KeyPair keyPair = keyPairGen.genKeyPair();

return keyPair;

} catch (Exception e) {

throw new EncryptException(e.getMessage());

}

}

/**

  • 生成公鑰

  • @param modulus

  • @param publicExponent

  • @return RSAPublicKey

  • @throws EncryptException

*/

public static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) throws EncryptException {

KeyFactory keyFac = null;

try {

keyFac = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

} catch (NoSuchAlgorithmException ex) {

throw new EncryptException(ex.getMessage());

}

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));

try {

return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);

} catch (InvalidKeySpecException ex) {

throw new EncryptException(ex.getMessage());

}

} /**

  • 生成私鑰

  • @param modulus

  • @param privateExponent

  • @return RSAPrivateKey

  • @throws EncryptException

*/

public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) throws EncryptException {

KeyFactory keyFac = null;

try {

keyFac = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

} catch (NoSuchAlgorithmException ex) {

throw new EncryptException(ex.getMessage());

}

RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));

try {

return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);

} catch (InvalidKeySpecException ex) {

throw new EncryptException(ex.getMessage());

}

}

/**

  • 加密

  • @param key 加密的密鑰

  • @param data 待加密的明文數據

  • @return 加密后的數據

  • @throws EncryptException

*/

public static byte[] encrypt(Key key, byte[] data) throws EncryptException {

try {

Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

cipher.init(Cipher.ENCRYPT_MODE, key);

int blockSize = cipher.getBlockSize();//獲得加密塊大小,如:加密前數據為128個byte,而key_size=1024 加密塊大小為127 byte,加密后為128個byte;因此共有2個加密塊,第一個127 byte第二個為1個byte

int outputSize = cipher.getOutputSize(data.length);//獲得加密塊加密后塊大小

int leavedSize = data.length % blockSize;

int blocksSize = leavedSize != 0 ? data.length / blockSize + 1 : data.length / blockSize;

byte[] raw = new byte[outputSize * blocksSize];

int i = 0;

while (data.length - i * blockSize > 0) {

if (data.length - i * blockSize > blockSize)

cipher.doFinal(data, i blockSize, blockSize, raw, i outputSize);

else

cipher.doFinal(data, i blockSize, data.length - i blockSize, raw, i * outputSize); //這里面doUpdate方法不可用,查看源代碼后發現每次doUpdate后并沒有什么實際動作除了把byte[]放到ByteArrayOutputStream中,而最后doFinal的時候才將所有的byte[]進行加密,可是到了此時加密塊大小很可能已經超出了OutputSize所以只好用dofinal方法。

i++;

}

return raw;

} catch (Exception e) {

throw new EncryptException(e.getMessage());

}

}

/**

  • 解密

  • @param key 解密的密鑰

  • @param raw 已經加密的數據

  • @return 解密后的明文

  • @throws EncryptException

*/

public static byte[] decrypt(Key key, byte[] raw) throws EncryptException {

try {

Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());

cipher.init(cipher.DECRYPT_MODE, key);

int blockSize = cipher.getBlockSize();

ByteArrayOutputStream bout = new ByteArrayOutputStream(64);

int j = 0;

while (raw.length - j * blockSize > 0) {

bout.write(cipher.doFinal(raw, j * blockSize, blockSize));

j++;

}

return bout.toByteArray();

} catch (Exception e) {

throw new EncryptException(e.getMessage());

}

}

File file = new File("test.html");

FileInputStream in = new FileInputStream(file);

ByteArrayOutputStream bout = new ByteArrayOutputStream();

byte[] tmpbuf = new byte[1024];

int count = 0;

while ((count = in.read(tmpbuf)) != -1) {

bout.write(tmpbuf, 0, count);

tmpbuf = new byte[1024];

}

in.close();

byte[] orgData = bout.toByteArray();

KeyPair keyPair = RSAUtil.generateKeyPair();

RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();

RSAPrivateKey priKey = (RSAPrivateKey) keyPair.getPrivate();

byte[] pubModBytes = pubKey.getModulus().toByteArray();

byte[] pubPubExpBytes = pubKey.getPublicExponent().toByteArray();

byte[] priModBytes = priKey.getModulus().toByteArray();

byte[] priPriExpBytes = priKey.getPrivateExponent().toByteArray();

RSAPublicKey recoveryPubKey = RSAUtil.generateRSAPublicKey(pubModBytes,pubPubExpBytes);

RSAPrivateKey recoveryPriKey = RSAUtil.generateRSAPrivateKey(priModBytes,priPriExpBytes);

byte[] raw = RSAUtil.encrypt(priKey, orgData);

file = new File("encrypt_result.dat");

OutputStream out = new FileOutputStream(file);

out.write(raw);

out.close();

byte[] data = RSAUtil.decrypt(recoveryPubKey, raw);

file = new File("decrypt_result.html");

out = new FileOutputStream(file);

out.write(data);

out.flush();

out.close();

103.計算文件大小 / import java.io.; private final long KB=1024; private final long MB=1024KB; private final long GB=1024MB; */ //文件屬性 File file=new File(%%1); //如果文件存在而且是文件,直接返回文件大小 if(file.exists()&&file.isFile()) { long filesize= file.length(); String showsize; if(filesize>=GB) showsize=filesize/GB+" GB"; else if(filesize>=MB) showsize=filesize/MB+" MB"; else if(filesize>=KB) showsize=filesize/KB+" KB"; else if(filesize>1) showsize=filesize/GB+" Bytes"; else showsize="1 Byte"; String %%2=showsize; }

104.計算文件夾的大小 / import java.io.; import java.math.; //bt字節參考量 private static final long SIZE_BT=1024L; //KB字節參考量 private static final long SIZE_KB=SIZE_BT1024L; //MB字節參考量 private static final long SIZE_MB=SIZE_KB1024L; //GB字節參考量 private static final long SIZE_GB=SIZE_MB1024L; //TB字節參考量 private static final long SIZE_TB=SIZE_GB1024L; private static final int SACLE=2; //文件大小屬性 private static long longSize;/ private void getFileSize(File file) { // 獲得文件目錄下文件對象數組 File[] fileArray = file.listFiles(); // 如果文件目錄數組不為空或者length!=0,即目錄為空目錄 if (fileArray != null && fileArray.length != 0) { // 遍歷文件對象數組 for (int i = 0; i < fileArray.length; i++) { File fileSI = fileArray[i]; // 如果是目錄遞歸遍歷 if (fileSI.isDirectory()) { // 遞歸遍歷 getFileSize(fileSI); } // 如果是文件 if (fileSI.isFile()) { longSize += fileSI.length(); } } } else { // 如果文件目錄數組為空或者length==0,即目錄為空目錄 longSize = 0; } } // 文件存在而且是目錄,遞歸遍歷文件目錄計算文件大小 File file = new File(folderPath); if (file.exists() && file.isDirectory()) { getFileSize(file);// 遞歸遍歷 } String %%2="0 Byte"; if (longSize == 1) { %%2="1 Byte"; } else if (longSize >= 2 && longSize < SIZE_BT) { %%2=longSize + " Bytes"; } else if (longSize >= SIZE_BT && longSize < SIZE_KB) { %%2=longSize / SIZE_BT + " KB"; } else if (longSize >= SIZE_KB && longSize < SIZE_MB) { %%2=longSize / SIZE_KB + " MB"; } else if (longSize >= SIZE_MB && longSize < SIZE_GB) { BigDecimal longs = new BigDecimal(Double.valueOf(longSize + "") .toString()); BigDecimal sizeMB = new BigDecimal(Double.valueOf(SIZE_MB + "") .toString()); String result = longs.divide(sizeMB, SACLE, BigDecimal.ROUND_HALF_UP).toString(); // double result=longSize/(double)SIZE_MB; %%2=result + " GB"; } else { BigDecimal longs = new BigDecimal(Double.valueOf(longSize + "") .toString()); BigDecimal sizeMB = new BigDecimal(Double.valueOf(SIZE_GB + "") .toString()); String result = longs.divide(sizeMB, SACLE, BigDecimal.ROUND_HALF_UP).toString(); %%2=result + " TB"; }

105.快速獲得當前程序的驅動器、路徑、文件名和擴展名 String %%2=%%1.substring(0,%%1.lastIndexOf(":")); String %%5=%%1.substring(0,%%1.lastIndexOf("\")); String %%4=%%1.substring(%%1.lastIndexOf("\")+1); String %%3=%%1.substring(%%1.lastIndexOf('.'));

106.磁盤剩余空間計算 //import java.io.*; File file = new File(%%1); //"C:" long %%2 = file.getFreeSpace(); //file.getTotalSpace()

107.獲取當前程序進程ID java是在jvm上跑的,jvm本身作為系統的一個進程在跑, 所以平時見到的都是java多線程,沒有進程間通信, 為什么呢,就是因為java中沒有進程這一說法, 除非指的是程序本身這個進程。這個進程的話需要用到系統命令或者底層c語言才能夠得到。呃,本人jni不是太熟悉,可以參考 http://hi.baidu.com/lff0305/blog/item/4f301a7b20d3d3f20bd187e3.html, linux下直接調用ps-ef再用管道提取吧。

108.全盤搜索文件 import java.io.*; //ArrayList fileNames=new ArrayList(); void listDirectory(String dir) { File f=new File(dir); File[] files=f.listFiles(); if(files==null) return; for(int i=0;i<files.length;i++) { if(files[i].isDirectory()) { System.out.println("-----------"+files[i]+" is a directory has more files bellow:"); listDirectory(files[i].getAbsolutePath()); //listDirectory(files[i].getName()); //listDirectory(files[i].toString()); }

else { System.out.println(files[i]); } } } File[] roots=File.listRoots(); for(int i=0;i<roots.length;i++) { System.out.println(roots[i]); } } lrt.listDirectory("D:"+File.separator);</pre>

 本文由用戶 ne3g 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!