หลังจากหายหน้าหายตาไปหลายวันไม่ได้ไปไหนนะครับแต่ว่าช่วงนี้งานส่วนของ Asterisk ที่เน้น ruby เป็นหลังต้องทำให้ค้องแบ่งเวลาในการเขียนบล๊อกทั้งหมดไปทำในส่วนนี้สำหรับวันที่วันเสาร์เรียน scjp เสร็จเลยมีอารมณ์อยากที่จะเขียนบล๊อก่อจากวันก่อนครับ สำหรับวันนี้จะเขียนเกี่ยวกับโค้ดของ softskill ต่อละกันนะครับ สำหรับวันนี้โจทย์จะเป็นไปในลักษณะให้เราอ่าน ไฟล์จาก input.txt ซึ่งมี path ของ file อยู่และให้อ่านอีกไฟล์คือไฟล์ rules.txt เพื่อทำการตวรจสอบว่าไฟล์นั้นผ่านกฏต่างๆ ที่ได้กำหนดไว้ในไฟล์ rules.txt หรือไม่หากว่าตรงให้เอาเฉพาะไฟล์ที่อณุญาตเท่านั้นครับ เรามาดูกันเลยดีกว่าครับ โดยในที่นี้ผมจะใช้ Pathmanager ที่ได้เขียนไว้ในการจัดการเรื่อง permission ครับ สำหรับไฟล์ใหม่ที่เขียนขึ้นตามนี้ครับ
package softwareskill; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; /** * * @author mLri */ public class SecueManager { private String rule; private String input; private String output; PathManager pathManager; public static void main(String[] args) throws FileNotFoundException, IOException { SecueManager secueManager = new SecueManager("/Users/mLri/rule.txt"); secueManager.processFile("/Users/mLri/input.txt", "/Users/mLri/output.txt"); } public SecueManager(String rule) throws FileNotFoundException, IOException { this.rule = rule; this.pathManager = new PathManager(); this.readRule(); } public void processFile(String input,String output) throws FileNotFoundException, IOException{ this.input = input; this.output = output; this.pathProcess(); } private void readRule() throws FileNotFoundException, IOException { File file = new File(rule); BufferedReader fileBuffer = new BufferedReader(new FileReader(file)); String lineBuffer; while ((lineBuffer = fileBuffer.readLine()) != null) { if (lineBuffer.startsWith("+")) { pathManager.addAcceptedRule(lineBuffer.substring(1).toString()); //System.out.println("is complete addAcceptRule is " + lineBuffer.substring(1)); } else if (lineBuffer.startsWith("-")) { pathManager.addDeniedRule(lineBuffer.substring(1).toString()); //System.out.println("is complete addDeniedRule is " + lineBuffer.substring(1)); } } fileBuffer.close(); System.out.println("readRule is Successful"); } private void pathProcess() throws FileNotFoundException, IOException { File file = new File(input); BufferedReader fileBuffer = new BufferedReader(new FileReader(file)); ArrayList allowFile = new ArrayList(); String lineBuffer; while ((lineBuffer = fileBuffer.readLine()) != null) { if (pathManager.isAllow(lineBuffer)) { //System.out.println("add line " + lineBuffer); allowFile.add(lineBuffer); } } fileBuffer.close(); this.pathWrite(allowFile); } private void pathWrite(ArrayList allowFile) throws IOException { FileWriter file = new FileWriter(output,true); for(String accept:allowFile){ file.write(accept+"\n"); } file.close(); System.out.println("File output is Suscessful in "+output); } }
สำหรับไฟล์ SecueManager จะเป็นตัวที่ทำหน้าที่ในการอ่านไฟล์และเขียนไฟล์ ส่วนไฟล์ที่ทำการ check จริงๆจะเป็นไฟล์ PathManager ครับ
Related posts:
- Java Communications API on Windows ...
- Set Path On Ubuntu สำหรับบทความนี้เป็นวิธีการ set path ใน ubuntu สำหรับคนที่ยังทำไม่ค่อยจะเป็นนะครับวิธีการมี 2 วิธีคือการเปิด terminal มาแล้ว...
Related posts brought to you by Yet Another Related Posts Plugin.
