|
首頁 | 討論區 | 最新話題 | 搜尋 | XML | 登入 |
![]() |
Blueimp » 列出所有討論區 » 討論區: JavaTech |
![]() |
|
此話題中所有文章數: 1 [ 話題狀態: 一般 ] | |
|
Java ParseText 測試中package test.fin.tools;import java.io.*; public class ParseText { public ParseText() { } private static String getCommand(BufferedReader br, String prompt) throws Exception{ System.out.print(prompt); return br.readLine(); } public static void main(String[] args) { if ( args.length == 0 ) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { args = new String[3]; args[0] = getCommand(br,"KEY:"); args[1] = getCommand(br,"File name:"); args[2] = getCommand(br,"MODE:"); } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } } else if ( args.length != 3 ) { System.out.println("ParseText KEY Filename MODE"); System.out.println(" MODE : "); System.out.println(" 1 : encode "); System.out.println(" 2 : decode "); System.exit(1); } // TextProcessor processor = null ; if ( args[2].equals("1") ) { processor = new TextEncoder(); } else { processor = new TextDecoder(); } processor.fileName = args[1] ; if ( processor.parse(args[0]) ) { System.out.println("ok!"); } } } // *************************** package test.fin.tools; public class TextDecoder extends TextProcessor{ public TextDecoder() { } public boolean parse(String key) { java.io.File file = this.checkFileExist(); if ( file == null ) return false; byte[] keys = key.getBytes(); int keyCount = 0 ; java.io.FileInputStream fis = null ; try { // fis = new java.io.FileInputStream(file); java.io.FileOutputStream fos = new java.io.FileOutputStream(this.fileName+".decode",false); // byte[] buffer = new byte[1] ; int length = 0 ; while ( (length = fis.read(buffer)) != -1 ) { for ( int i = 0 ; i < length ; i++ ) { //System.out.print(buffer+"+"+keys[(keyCount % keys.length)]+"="); buffer -= keys[(keyCount++ % keys.length)]; //System.out.println(buffer); } fos.write(buffer,0,length); } fis.close(); fos.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } public static void main(String[] args) { TextDecoder test = new TextDecoder(); test.fileName = "d:\\temp\\a.txt.encode"; test.parse("test"); } } // ********************* package test.fin.tools; import java.io.*; public class TextEncoder extends TextProcessor{ public TextEncoder() { } public boolean parse(String key) { java.io.File file = this.checkFileExist(); if ( file == null ) return false; byte[] keys = key.getBytes(); int keyCount = 0 ; java.io.FileInputStream fis = null ; try { // fis = new java.io.FileInputStream(file); java.io.FileOutputStream fos = new java.io.FileOutputStream(this.fileName+".encode",false); // byte[] buffer = new byte[1] ; int length = 0 ; while ( (length = fis.read(buffer)) != -1 ) { for ( int i = 0 ; i < length ; i++ ) { //System.out.print(buffer+"+"+keys[(keyCount % keys.length)]+"="); buffer += keys[keyCount++ % keys.length]; //System.out.println(buffer); } fos.write(buffer,0,length); } fis.close(); fos.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } public static void main(String[] args) { TextEncoder test = new TextEncoder(); test.fileName = "d:\\temp\\a.txt" ; test.parse("test"); } } // *********************** package test.fin.tools; abstract class TextProcessor { public String fileName = null ; public TextProcessor() { } protected java.io.File checkFileExist() { if ( this.fileName == null ) { System.out.println("Please input filename!"); return null; } java.io.File file = new java.io.File(this.fileName) ; if ( !file.isFile() ) { System.out.println(this.fileName + " isn't exist !"); return null; } return file; } abstract public boolean parse(String key) ; } ---------------------------------------- [編輯文章 1 次, 最後修改: jieh 於 2025/5/2 下午 05:16:19] |
|||
|