神雕侠侣网络文件排序与替换
来源:互联网 发布:java 声明base64 编辑:程序博客网 时间:2024/05/17 06:09
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;
}
}
- 神雕侠侣网络文件排序与替换
- 神雕侠侣
- 神雕侠侣
- 神雕侠侣
- 神雕侠侣
- VB6 文件复制与替换
- MFC文件查找与替换
- 神雕侠侣-程英
- 听《神雕侠侣》
- Vim多文件查找与替换
- shell 多文件查找与替换
- shell 多文件查找与替换
- 静态文件版本号替换与压缩
- 文件与外部排序
- 文件替换
- 文件替换
- 《神雕侠侣》之我见
- 跟着神雕侠侣游九寨沟
- ORACLE 表空间扩展方法
- Android倒计时功能的实现(CountDownTimer)
- linux乱码问题:LANG变量的秘诀
- 产品开发第一步-架构设计
- 快速排序(递归实现)
- 神雕侠侣网络文件排序与替换
- fl2440 的菜鸟学习之路1
- Linux基础知识---Shell编程之正则表达式
- java JNDI介绍
- 四个开源商业智能平台比较(二)
- HDOJ 1063 Exponentiation(小数高精度)
- 详解java中staitc关键字
- Mac OS 系统 做 USB bootable 启动盘
- java.util.Date和java.sql.Date的区别和相互转化