文件读取、获取随机数、转换中文时间的方法

来源:互联网 发布:大数据相关课程 编辑:程序博客网 时间:2024/06/06 01:39

1.按顺序读取文件最后N行

/** * 读取文件最后N行 *  * 根据换行符判断当前的行数, 使用统计来判断当前读取第N行 *  * PS:输出的List是倒叙,需要对List反转输出 *  * @param file *            待文件 * @param numRead *            读取的行数 * @return List<String> */public static String readLastNLine(File file, long numRead) {// 定义结果集String rel = "";// 行数统计long count = 0;// 排除不可读状态if (!file.exists() || file.isDirectory() || !file.canRead()) {return null;}// 使用随机读取RandomAccessFile fileRead = null;try {// 使用读模式fileRead = new RandomAccessFile(file, "r");// 读取文件长度long length = fileRead.length();// 如果是0,代表是空文件,直接返回空结果if (length == 0L) {return rel;} else {// 初始化游标long pos = length - 1;while (pos > 0) {pos--;// 开始读取fileRead.seek(pos);// 如果读取到\n代表是读取到一行if (fileRead.readByte() == '\n') {// 使用readLine获取当前行String line = fileRead.readLine();// 保存结果rel = line + "\n" + rel;// 行数统计,如果到达了numRead指定的行数,就跳出循环count++;if (count == numRead) {break;}}}if (pos == 0) {fileRead.seek(0);rel = fileRead.readLine() + "\n" + rel;}}} catch (IOException e) {e.printStackTrace();} finally {if (fileRead != null) {try {// 关闭资源fileRead.close();} catch (Exception e) {}}}return rel;}


2.按照顺序读取文件,可以指定从第几行开始读取并且读取几行

** * 顺序 *@author LH *@data 2016年8月16日 * @param file * @param offset * @param numRead * @return */public static String readLineAsc(File file, int offset,int numRead) {StringBuilder sb = new StringBuilder();int begin =  offset;int end = offset+numRead-1;long count = 0;if (!file.exists() || file.isDirectory() || !file.canRead()) {return null;}RandomAccessFile fileRead = null;try {fileRead = new RandomAccessFile(file, "r");long length = fileRead.length();//计算begin和endif (length == 0L) {return sb.toString();} else {long index = 0;String[] lines = new String[numRead];int i = 0;while(index <length ){fileRead.seek(index++);if(fileRead.readByte() == '\n'  ){count++;if(count >= begin && count <= end){lines[i] = fileRead.readLine();i++;}if(count > end){break;}}}for(int k =0;k<lines.length;k++){sb.append(lines[k]==null?"":new String(lines[k].getBytes("ISO-8859-1"), "utf-8")+"\n");}}} catch (IOException e) {e.printStackTrace();} finally {if (fileRead != null) {try {// 关闭资源fileRead.close();} catch (Exception e) {}}}return sb.toString();}

3.按照倒叙读取文件,可以指定从第几行开始读取并且读取几行

/** * 倒序 *@author LH *@data 2016年8月16日 * @param file * @param offset * @param numRead * @return */public static String readLineDesc(File file, int offset,int numRead) {StringBuilder sb = new StringBuilder();int begin =  offset;int end = offset+numRead-1;long count = 0;if (!file.exists() || file.isDirectory() || !file.canRead()) {return null;}RandomAccessFile fileRead = null;try {fileRead = new RandomAccessFile(file, "r");long length = fileRead.length();//计算begin和endif (length == 0L) {return sb.toString();} else {long index = length-1;String[] lines = new String[numRead];int i = 0;while(index >= 0){fileRead.seek(index--);if(fileRead.readByte() == '\n'  ){count++;if(count >= begin && count <= end){lines[i] = fileRead.readLine();i++;}if(count > end){break;}}}for(int k = lines.length -1;k >=0;k--){sb.append(lines[k]==null?"":new String(lines[k].getBytes("ISO-8859-1"), "utf-8")+"\n");}}} catch (IOException e) {e.printStackTrace();} finally {if (fileRead != null) {try {// 关闭资源fileRead.close();} catch (Exception e) {}}}return sb.toString();}


4.将带有数字时间的字符串转为中文字符的时间

public static String timeTran(String times){        long time = Long.parseLong(times);        if(time > (1000 * 60 * 60 * 24)){            long day = time / (1000 * 60 * 60 * 24);            long hour = time/(1000 * 60 * 60) - day * 24;            long minute = time/(1000 * 60) - day * 24 * 60 - hour * 60;            long second = time/1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;            return day + "天" + hour + "小时" + minute + "分钟" + second + "秒";        }else if(time > (1000 * 60 * 60)){            long hour = time/(1000 * 60 * 60);            long minute = time/(1000 * 60) - hour * 60;            long second = time/1000 - hour * 60 * 60 - minute * 60;            return hour + "小时" + minute + "分钟" + second + "秒";        }else if(time > (1000 * 60)){            long minute = time/(1000 * 60);            long second = time/1000 - minute * 60;            return minute + "分钟" + second + "秒";        }        return time/1000 + "秒";    }


5.获取指定范围的随机数

/**     * 获取指定范围内的随机数     *@author LH     *@data 2016年11月11日     * @param min     * @param max     * @param number     * @return     */    public static int getRandom(int min, int max)    {    Random random = new Random();    int s = random.nextInt(max) % (max - min + 1) + min;    return s;    }

6.获取指定范围内的随机数 并且不等于指定的数字

/**     * 获取指定范围内的随机数 并且不等于指定的数字     *@author LH     *@data 2016年11月11日     * @param min     * @param max     * @param number     * @return     */    public static int getRandom(int min, int max,int number)    {    Random random = new Random();    int s = random.nextInt(max) % (max - min + 1) + min;    if(number!=s){    return s;    }else{    //递归    return getRandom( min,  max, number);    }    }

7.获取指定范围内的随机数 并且不等于指定的数字集合 

/**     * 获取指定范围内的随机数 并且不等于指定的数字集合      *@author LH     *@data 2016年11月11日     * @param min     * @param max     * @param number     * @return     */    public static String getRandom(int min, int max,List<String> numberList)    {    Random random = new Random();    int s = random.nextInt(max) % (max - min + 1) + min;    String ranString=String.valueOf(s);    if(null !=numberList && numberList.size()>0){    if(!numberList.contains(ranString)){    return ranString;    }else{    //递归获取    return getRandom( min,  max, numberList);    }    }else{    return ranString;    }    }




0 0