`

java练手全程录4-处理工具类代码实现

阅读更多

2012-2-7

 

继续昨天的日志。贴出工具类的代码实现。

 

工具类名定义为UtilFile

再定义四个方法

public static void changeChineseToHexInFile(String filePath, SwitchHexAndChineseView frame) throws IOException

public static boolean changeChineseToHexInString(StringBuilder input)

public static void changeHexToChineseInFile(String filePath, SwitchHexAndChineseView frame) throws IOException

public static boolean changeHexToStringInString(StringBuilder input)

 

方法说明

changeChineseToHexInFile 是用来对文件进行中文转unicode处理,至少要传一个文件名或路径。

对于第二个参数的说明,至于后面的frame是一个swing的jframe对象,这是为了做GUI界面而后加入的,为的就是如果传入的是路径,则对路径内部的文件处理时,处理一个文件后就在GUI界面里写出一个操作提示。

这里有多种方案如:

1.就是我当前使用的,在工具类中传入一个GUI对象,就可以在工具类中对GUI进行操作。因为这里的工具类是前几天写好的,不想大改,只要加入一个新参数就能GUI的项目中马上起作用。

2.方法参数这里只传一个文件名而不是路径,这样就可以在GUI的方法体中调用工具类对单个文件进行处理,处理完后再在GUI方法体内进行GUI操作。

 

changeChineseToHexInString 是用来对字符串进行中文转unicode处理,传入的是一个字符串。

changeHexToChineseInFile和changeHexToStringInString 是反过来的

 

 

方法实现

 

 public static void changeHexToChineseInFile(String filePath, final SwitchHexAndChineseView frame) throws IOException {
        final File file = new File(filePath);
        //*** 如果是文件夹则查其子文件夹中的文件
        if (file.isDirectory()) {
            //使用文件过滤器来过滤文件,只取子文件夹中的.java/.properties或路径
            File[] files = file.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.isDirectory() || pathname.getName().endsWith(".java") || pathname.getName().endsWith(".properties");
                }
            });
            //对字文件夹中的内容进行递归调用
            for (File f : files) {
                changeHexToChineseInFile(f.getAbsolutePath(), frame);
            }
        } else {
            //如果是文件,则以固定的utf8编码读取文件
            FileInputStream fr = new FileInputStream(file);
            InputStreamReader is = new InputStreamReader(fr, "utf8");
            char[] cc = new char[(int) file.length()];
            int readnum = is.read(cc);
            StringBuilder sb = new StringBuilder(readnum);
            sb.append(cc, 0, readnum);

            //对字符串进行转换处理,如果文件里没有要转换的字符则就不必对原文件进行覆盖
            boolean isChange = changeHexToStringInString(sb);
            fr.close();

            if (isChange) {
                //如果有转换,则以固定的utf8码对原文件进行覆盖
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "utf8");
                osw.write(sb.toString().trim());
                osw.close();

                //每处理完一个文件,则在awt线程中对GUI界面进行操作,在窗口中输出处理过的文件绝对路径全名
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        frame.setResultView(file.getAbsolutePath() + "\n");
                    }
                });
            }
        }
    }

 

 

 public static void changeChineseToHexInFile(String filePath, final SwitchHexAndChineseView frame) throws IOException {
        final File file = new File(filePath);
        //*** 如果是文件夹则查其子文件夹中的文件
        if (file.isDirectory()) {
            File[] files = file.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.isDirectory() || pathname.getName().endsWith(".java") || pathname.getName().endsWith(".properties");
                }
            });
            //对子文件进行递归调用
            for (File f : files) {
                changeChineseToHexInFile(f.getAbsolutePath(), frame);
            }
        } else {
            //以固定utf8读取文件内容
            FileInputStream fr = new FileInputStream(file);
            InputStreamReader is = new InputStreamReader(fr, "utf8");
            char[] cc = new char[(int) file.length()];
            int readnum = is.read(cc);
            StringBuilder sb = new StringBuilder(readnum);
            sb.append(cc, 0, readnum);

            //处理内容
            boolean isChange = changeChineseToHexInString(sb);
            fr.close();

            if (isChange) {
                //覆盖原文件
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "utf8");
                osw.write(sb.toString().trim());
                osw.close();
                
                //在awt线程中操作GUI
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        frame.setResultView(file.getAbsolutePath() + "\n");
                    }
                });
            }
        }
    }

 可以看到这两个方法大部分代码都相同,还可以再重构。如再分一个方法要加emun参数,这两个方法分别再调第三个方法。这里时间不多就不在gui的工具类里做了,但我会在自己的实现工具类里再优化的。

 

 

 public static boolean changeChineseToHexInString(StringBuilder input) {
        char each = '\u0000';
        boolean isChange = false;
        int inputLength = input.length();
        //定义一个临时变量
        StringBuilder sb = new StringBuilder(inputLength);
        for (int i = 0; i < inputLength; i++) {
            each = input.charAt(i);
            if (String.valueOf(each).matches("[^\\u0000-\\u00FF]")) {
                isChange = true;
                //将中文转成16进制
                sb.append("\\u").append(Integer.toHexString(each));
            } else {
                sb.append(each);
            }
        }
        if (isChange) {
            //如果有变化,就将原内容替换
            input.replace(0, inputLength, sb.toString());
        }
        return isChange;
    }

 参数使用StringBuilder是考虑性能问题,且这是个对象引用,可以直接对对象进行操作。引用中间变量sb是为了解决在循环中的正则长度匹配问题。如果不使用中间变量,而直接使用参数在循环体中进行replace替换,那么每替换一次则字符的总长度会加5,因为16进制是6位字符,而中文只是一个字符。对于正则来说在循环时如果长度发生变化则可能会抛出异常。

 

 public static boolean changeHexToStringInString(StringBuilder input) {
        boolean isChange = false;
        //使用正则找出\u0000这种格式,且不能是\\u0000这各格式的
        Matcher matcher = Pattern.compile("((?<!\\\\)\\\\u([0-9A-Fa-f]{4}))", Pattern.MULTILINE | Pattern.UNICODE_CASE).matcher(input);
        while (matcher.find()) {
            isChange = true;
            //每找到一处,就从找到的下标开始进行替换
            input.replace(matcher.start(), matcher.end(), String.valueOf((char) Integer.parseInt(matcher.group(2), 16)));
            //替换后字符长度发生了变量,则要相应的更改正则的匹配范围,否则会出错
            matcher.region(matcher.start() - (matcher.end() - matcher.start()), input.length());
        }
        return isChange;
    }

 这里的stringbuilder就更重要了,如果你只使用string则每次replace都会创建一个新对象,因为string是不变长度的,你改了字符,长度变了,那它就会创建新string对象对于性能来说是影响很大的

 

主要代码就是这么多,GUI代码太多了,就不贴了,直接上传成品,为了方便使用,我直接把GUI打包成了exe。运行环境是要jre1.6及以上的。小工具一个,没时间去做更好的用户体验,凑合着用吧

 

  • 大小: 23.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics