traverse a dir, search some text and do some thing

import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
         
public class traverseDir {
    public static void main(String[] args) {
        if(args.length != 2) {
            System.out.println("Invalid parameters!");
            return;
        }
        String scanDir = args[0];
        String packageSuffix = args[1];
        try {
            traverseDir td = new traverseDir();
            lf.listFile(scanDir, packageSuffix);
        } catch (ArrayIndexOutOfBoundsException ea) {
            System.out.println("Usage: ListFiles <source dir> <target file>");
        } catch (Exception e) {
            System.out.println("IO error!/r/n"+e.toString());
        }
    }

    public void listFile(String rp, String modifyTag){
        File file=new File(rp);
        File list[]=file.listFiles();
        for(int i=0;i<list.length;i++) {
            try {
                if (list[i].isDirectory()) {
                    new traverseDir().listFile(list[i].toString(), modifyTag);
                }
                else {
                    if(isJavaFile(list[i].toString())) {
//                        replace(modifyTag, list[i].toString());                //read a file and do our work here.
                    }

                }
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("Access deny"+list[i].getAbsolutePath());
            }
        }
    }

    public void replace(String strReplacement, String filePath) throws FileNotFoundException, UnsupportedEncodingException {
        File file = new File(filePath);
        file.setWritable(true);

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        StringBuffer strBuf = new StringBuffer();

        try {
            String line = null;

            while((line = br.readLine()) != null) {
                //System.out.println(line);
                //import com.android.google.R.
                Pattern pattern = Pattern.compile("^[ ]*import//scom.android.google.R[//s]*;[//s]*$");
                Matcher m = pattern.matcher(line);
                if(m.find()) {
                    //System.out.println("found: " + line);
                    line = "import com.android.google.R."+strReplacement+".R;";    //  replace the found line here.
                }

                strBuf.append(line+"/r/n");
            }

            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        FileOutputStream wf = new FileOutputStream(file);
        try {
            wf.write(strBuf.toString().getBytes());
        } catch(IOException e) {
            e.printStackTrace();
        }

    }

    public boolean isJavaFile(String filePath) {
        if(filePath.trim().endsWith(".java")) {
            return true;
        }
        return false;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【Solution】 Here is a C++ implementation of the solution to the problem: ```cpp #include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: TreeNode* treeToDoublyList(TreeNode* root) { if (!root) { return NULL; } stack<TreeNode*> s; TreeNode* p = root; TreeNode* pre = NULL; TreeNode* head = NULL; while (p || !s.empty()) { while (p) { s.push(p); p = p->left; } p = s.top(); s.pop(); if (pre) { pre->right = p; p->left = pre; } else { head = p; } pre = p; p = p->right; } head->left = pre; pre->right = head; return head; } }; void printBST(TreeNode* root) { if (root) { printBST(root->left); cout << root->val << " "; printBST(root->right); } } void printList(TreeNode* head) { TreeNode* p = head; do { cout << p->val << " "; p = p->right; } while (p != head); cout << endl; } void printListReverse(TreeNode* head) { TreeNode* p = head->left; do { cout << p->val << " "; p = p->left; } while (p != head->left); cout << endl; } int main() { TreeNode* root = new TreeNode(4); root->left = new TreeNode(2); root->right = new TreeNode(5); root->left->left = new TreeNode(1); root->left->right = new TreeNode(3); Solution s; TreeNode* head = s.treeToDoublyList(root); cout << "Binary Search Tree: "; printBST(root); cout << endl; cout << "Doubly Linked List: "; printList(head); cout << "Doubly Linked List in Reverse Order: "; printListReverse(head); return 0; } ``` 【Explanation】 To convert a binary search tree into a sorted circular doubly linked list, we can use a stack to perform an inorder traversal of the binary search tree. During the traversal, we maintain a pointer to the previous node visited and link the current node to the previous node. After the traversal, we link the head and tail of the linked list to form a circular doubly linked list. Finally, we can print the binary search tree, traverse the doubly linked list, and output all elements in positive and reverse order. 【Example】 Input: ``` 4 / \ 2 5 / \ 1 3 ``` Output: ``` Binary Search Tree: 1 2 3 4 5 Doubly Linked List: 1 2 3 4 5 Doubly Linked List in Reverse Order: 5 4 3 2 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值