用Java程序代替Notepad++的文字处理和Powershell

藏色散人
发布: 2020-03-02 17:29:25
转载
3096 人浏览过

用Java程序代替Notepad++的文字处理和Powershell

由于Notepad++里面的文字处理有些复杂,我在想能不能用一个Java程序来统一处理呢?

尝试了一下,发现不太复杂,Java果然处理各种事情都比较方便。程序如下:

注意在RegExp里面,必须用四个反斜杠\\\\代表一个反斜杠\

package com.pwc;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WechatMomentDownload {
    
    public static String jsonFile = "C:\\Download\\exported_sns_gary.json";
    public static String outputFolder = "C:\\Download\\gary_pic\\";
    public static void main(String[] args) throws IOException {
        BufferedReader reader = null;
        String sLine = "";
        String sContent = "";
        String sLink = "";
        URL url = null;
        String outputFile = "";
        ArrayList<String> aLink = new ArrayList<String>();
        String Regex1 = "CDATA\\[http\\:\\\\\\/\\\\\\/(sh)?mmsns([^]]*)\\/0\\]";
        
        reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(new File(jsonFile)), "UTF-8"));
        if (reader != null) {
            while ((sLine = reader.readLine()) != null) {
                sContent = sContent + sLine;
            }
            reader.close();
        }
        
        Pattern pattern = Pattern.compile(Regex1);
        Matcher matcher = pattern.matcher(sContent);
        int count = 0;
        
        System.out.println("Start processing...");
        while(matcher.find()) {
            count++;
            sLink = sContent.substring(matcher.start() + 6, matcher.end() - 1);
            sLink = sLink.replaceAll("\\\\/", "/");
            aLink.add(sLink);
         }
        
        System.out.println(count + " pictures were found");
        
        for (String sLinkTemp : aLink) {
            url = new URL(sLinkTemp);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            
            outputFile = outputFolder + count + ".jpg";
            FileOutputStream fileOutputStream = new FileOutputStream(new File(outputFile));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            System.out.println("Downloading " + sLinkTemp + " to file " + outputFile);
            
            byte[] buffer = new byte[1024];
            int length;
 
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        
            count--;
        }
        
        System.out.println("End of processing...");
    }
}
登录后复制

更多notepad教程,请访问notepad使用教程栏目:https://www.php.cn/tool/notepad/

以上是用Java程序代替Notepad++的文字处理和Powershell的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:segmentfault.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!