神雕侠侣网络文件排序与替换

来源:互联网 发布:java 声明base64 编辑:程序博客网 时间:2024/05/17 06:09
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class IOutils {
    private static final Logger LOGGER = LoggerFactory.getLogger(IOutils.class);


    public static BufferedReader getBufferedReaderByUrl(String urlString) {
        /*read file from URL*/
        BufferedReader bf = null;
        try {
            URL fileUrl = new URL(urlString);
            bf = new BufferedReader(new InputStreamReader(fileUrl.openStream()));
        } catch (MalformedURLException e) {
            LOGGER.error("cann't connect to URL:" + urlString);
        } catch (IOException e) {
            LOGGER.error("cann't connect to URL:" + urlString);
        }
        return bf;
    }


    public static BufferedWriter getBufferedWriterByPath(String filePath) throws Exception {
        BufferedWriter bufferedWriter = null;
        try {


            String url = IOutils.class.getResource("/").getFile() + filePath;
            bufferedWriter = new BufferedWriter(new FileWriter(url));
        } catch (Exception e) {
            LOGGER.error("Exception: write file error!");
        }
        return bufferedWriter;
    }


    public static void closeIgnoreException(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                LOGGER.error("IOException:close file error!");
            }
        }
    }

}


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.*;
import java.util.Arrays;

public class Question2 {
    private static final Logger LOGGER = LoggerFactory.getLogger(Question2.class);
    private static final String NATURAL_ORDER = "natureOrder";
    private static final String INDEX_ORDER = "indexOrder";
    private static final String CHAR_ORDER = "charOrder";
    private static final int ARRAY_SIZE = 6110;


    private String[] naturalOrderRep = new String[ARRAY_SIZE];/*自然顺序替换数组*/
    private String[] indexOrderRep = new String[ARRAY_SIZE];/*索引顺序词条数组*/
    private String[] charOrderRep = new String[ARRAY_SIZE];/*字符顺序词条数组*/


    private static final String INPUT_File_NAME1 = "http://fresh.qunar.com/sites/task3.txt";
    private static final String INPUT_File_NAME2 = "http://fresh.qunar.com/sites/task3_prop.txt";
    private static final String OUTPUT_File_NAME = "/sdxl.txt";


    public static void main(String[] args) throws Exception {
        Question2 question2 = new Question2();
        question2.execute();
    }


    public void execute() throws Exception {
        orderFile();
        BufferedReader File1BufferReader = IOutils.getBufferedReaderByUrl(INPUT_File_NAME1);
        BufferedWriter outPutBuffWriter = IOutils.getBufferedWriterByPath(OUTPUT_File_NAME);
        String line = File1BufferReader.readLine();
        try {
            while (line != null) {
                outPutBuffWriter.write(replaceWord(line));
                line = File1BufferReader.readLine();
                if (line != null)
                    outPutBuffWriter.write("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {


            IOutils.closeIgnoreException(outPutBuffWriter);
            IOutils.closeIgnoreException(File1BufferReader);
        }
    }


    private void orderFile() throws IOException {
        /*对task3_prop.txt进行排序后存储到各替换数组中*/
        BufferedReader File2BufferReader = IOutils.getBufferedReaderByUrl(INPUT_File_NAME2);
        String line;
        int index = 0;
        while ((line = File2BufferReader.readLine()) != null) {
            int tabIndex = line.indexOf('\t');
            String word = line.substring(tabIndex + 1, line.length());
            Integer wordIndex = Integer.parseInt(line.substring(0, tabIndex));
            naturalOrderRep[index] = word;
            indexOrderRep[wordIndex] = word;
            index++;
        }
        System.arraycopy(naturalOrderRep, 0, charOrderRep, 0, naturalOrderRep.length);
        Arrays.sort(charOrderRep);
    }


    private String replaceWord(String line) throws IOException {
        int dollar = line.indexOf('$');


        if (dollar != -1) {
            int left = -1;
            int right = -1;
            left = line.indexOf('(', dollar);
            if (left != -1) {
                right = line.indexOf(')');
            }


            int num = Integer.valueOf(line.substring(left + 1, right));
            String type = line.substring(dollar + 1, left);
            String str = line.substring(dollar, right + 1);


            if (type.equals(NATURAL_ORDER)) {
                line = line.replace(str, naturalOrderRep[num]);
            } else if (type.equals(INDEX_ORDER)) {
                line = line.replace(str, indexOrderRep[num]);
            } else if (type.equals(CHAR_ORDER)) {
                line = line.replace(str, charOrderRep[num]);
            } else {
                line = line.replace(str, charOrderRep[charOrderRep.length - num - 1]);
            }
        }
        return line;
    }
}

0 0
原创粉丝点击