package readImgUrl;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class ClassifyUrl { private static int HASHLEN = 100; private static String file_dir = "D:\\学习\\实验室项目\\ImageNet图片爬取\\classify_url\\"; private static String src_file = "D:\\学习\\实验室项目\\ImageNet图片爬取\\fall11_urls.txt"; public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
classify_url("D:\\学习\\实验室项目\\ImageNet图片爬取\\fall11_urls.txt");
// rank_filedata("2"); // String s = judgeFileCode(src_file);
// String s = codeString(src_file);
// System.out.println(s);
} /**
* 对一个文件进行排序
*/
public static void rank_filedata(String filename){
String path1 = file_dir+filename+".txt";
String path2 = file_dir+filename+"_"+".txt";
List<String> list = reader_list(path1);
System.out.println(list.size());
// 排序,通过泛型和匿名类来实现
Collections.sort(list, new Comparator<String>() {
public int compare(String s1, String s2) {
String h1 = s1.split(" ")[1];
String h2 = s2.split(" ")[1];
return h1.compareTo(h2);
}
});
writer_list(list, path2);
}
/**
* 读取文件,返回list
* @param path
* @return
*/
public static List reader_list(String path){
List<String> lineList = new ArrayList();
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = reader.readLine();
while(null != line){
lineList.add(line);
line = reader.readLine();
}
reader.close();
return lineList;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
/**
* 将List写入文件
* @param line
*/
public static void writer_list(List list, String path){
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
for(int i=0; i<list.size(); i++){
String line = (String)list.get(i);
writer.write(line+"\r\n");
}
writer.close(); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 从文件中逐行读取数据,分类写入0-99个文件
*/
public static void classify_url(String path){
try {
BufferedReader reader ;
String filecode = judgeFileCode(path);
reader = new BufferedReader(new InputStreamReader(new FileInputStream(path),filecode));
// BufferedReader reader = new BufferedReader(new FileReader(path));
String line = reader.readLine();
int line_num = 0;
// while(line_num<4101000){
// reader.readLine();
// line_num++;
// }
while(null != line){
try {
String host = new URL(line.split(" ")[1]).getHost();
int type = hash(host.toCharArray());
// writer(type+"", line);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
line = reader.readLine();
line_num++;
if(line_num%100==0){
// System.out.println(line_num);
char [] cc = line.toCharArray();
for(char c: cc){
if(isCnorEn(c)){
System.out.println(line);
break;
}
}
// break;
}
}
reader.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 判断是中文还是英文字符
*/
static boolean isCnorEn(char c) {
if ((c >= 0x0391 && c <= 0xFFE5) // 中文字符
|| (c >= 0x0000 && c <= 0x00FF)) // 英文字符
return true;
return false;
// if ((c >= 0x0391 && c <= 0xFFE5) // 英文字符
// ) //
// return true;
// return false;
}
/**
* 给定一个字符串,返回hash后的int值
* @param word
* @return
*/
public static int hash(char[] word) {
int index = 0;
int i=0;
while(i<word.length) {
index += index * 31 + word[i];
i++;
}
return Math.abs(index % HASHLEN);
}
/**
* 将line写入filename中(文件不存在则先建立)
* @param filename
* @param line
*/
public static void writer(String filename, String line){
String path = file_dir+filename+".txt";
try {
File file = new File(path);
if(!file.isFile()){
file.createNewFile();
}
String filecode = judgeFileCode(src_file);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path, true), "GBK");
// BufferedWriter writer = new BufferedWriter(new FileWriter(path, true));
if(null != line){
writer.write(line+"\r\n");
}
writer.close(); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} public static String judgeFileCode(String path){
try {
File file = new File(path);
InputStream in= new java.io.FileInputStream(file);
byte[] b = new byte[3];
in.read(b);
in.close();
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
// System.out.println(file.getName() + ":编码为UTF-8");
return "UTF-8";
}
else{
// System.out.println(file.getName() + ":可能是GBK,也可能是其他编码");
return "GBK";
}
} catch (Exception e) {
// TODO: handle exception
}
return null;
} /**
* 判断文件的编码格式
* @param fileName :file
* @return 文件编码格式
* @throws Exception
*/
public static String codeString(String fileName) throws Exception{
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
String code = null;
//其中的 0xefbb、0xfffe、0xfeff、0x5c75这些都是这个文件的前面两个字节的16进制数
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
case 0x5c75:
code = "ANSI|ASCII" ;
break ;
default:
code = "GBK";
} return code;
} }

使用hash拆分文件的更多相关文章

  1. Linux split拆分文件

    200 ? "200px" : this.width)!important;} --> 介绍 split可以将一个大文件拆分成指定大小的多个文件,并且拆分速度非常的快,拆分一 ...

  2. linux_shell_拆分文件_多进程脚本

    [需求场景]:一个10000w行的文件处理  ,多进程处理  比如启动100个进程同时处理. [方法]:拆分文件(split) ,制作shell脚本  执行后台进程 [demo]: 假设处理程序为   ...

  3. linux 拆分文件

    split [OPTION]... [INPUT [PREFIX]] :根据行或者大小拆分文件 split file_name :默认把文件file_name拆分成xaa,xab,xac,...... ...

  4. linux拆分文件

    1.先看下文件总的行数: wc -l filename 我们现在来看看它具体的参数该怎么用: split支持自定义输出文件大小和输出文件行数两种模式,此外还可以定义每一行最大的值. -l 按输出文件行 ...

  5. split - 拆分文件

    拆分文件 # 每个文件的行数为1000行 split -l 1000 test.txt # 将test文件拆分,20M一个文件 split -b 20M test.txt test文件拆分,并且文件名 ...

  6. casperjs在拆分文件后的中文乱码问题的解决

    windows环境. capserjs的中文乱码使用phantom.outputEncoding="GBK";即可解决. 但当我们脚本很大,需要拆分时(参考http://docs. ...

  7. java 按内容拆分文件

    文件内容为: BC************* **************** *************** BC************* **************** *********** ...

  8. python_基础学习_02_拆分文件(spilt)

    做爬虫经常会有这样的引用场景 ,原始网页存储格式为  url+\t+ html php 有个explode的 拆分文本行方法,比较方便直接接收列值 list($url,$html)=explode(& ...

  9. RandomAccessFile拆分合并文件

    import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java. ...

随机推荐

  1. iOS获取APP的版本号和名称

    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...

  2. JS常用函数原理的实现

    本文针对目前常见的面试题,实现了相应方法的核心原理,部分边界细节未处理.后续也会持续更新,希望对你有所帮助. 1.实现一个call函数 // 思路:将要改变this指向的方法挂到目标this上执行并返 ...

  3. deep_learning_Function_softmax_cross_entropy_with_logits

    [TensorFlow]tf.nn.softmax_cross_entropy_with_logits的用法 [TensorFlow]tf.nn.softmax_cross_entropy_with_ ...

  4. Dubbo 04 服务化最佳实现流程

    Dubbo 04 服务化最佳实践 分包 建议将服务接口.服务模型.服务异常等均放在 API 包中,因为服务模型和异常也是 API 的一部分,这样做也符合分包原则:重用发布等价原则(REP),共同重用原 ...

  5. Backtracking(一)

    LeetCode中涉及到回溯的题目有通用的解题套路: 46. permutations 这一类回溯题目中的基础中的基础,无重复数字枚举: /* Given a collection of distin ...

  6. 【LOJ 6695】天气之子

    找规律题的典范? OEIS裸题 考场上让你用 OEIS 吗 题意 link 题解 \(n\le 5\) 打表 \(n\le 10^5\) 发现不能直接求最优解,于是二分答案. 验证答案时,先把前 \( ...

  7. npm安装node-sass报msbuild相关错误的解决办法

    转自:https://blog.csdn.net/Amio_/article/details/87931733 https://www.cnblogs.com/diffx/p/10510868.htm ...

  8. HashMap,LinkedHashMap,TreeMap的有序性

    HashMap 实际上是一个链表的数组.HashMap 的一个功能缺点是它的无序性,被存入到 HashMap 中的元素,在遍历 HashMap 时,其输出是无序的.如果希望元素保持输入的顺序,可以使用 ...

  9. 在Nginx中使用Godaddy的SSL证书

    首先在Godaddy付款购买SSL证书,成功之后打开管理面板,找到刚购买的SSL证书,点击新建证书,这个时候Godaddy会让提供CSR文件内容,可以通过下面的命令行生成csr内容: openssl ...

  10. python时间 time模块和datetime模块

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...