加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

数据处理小例子

发布时间:2020-12-14 03:33:07 所属栏目:大数据 来源:网络整理
导读:昨天的interview遇到的一道题,现在记录下我的代码备以后参考。 题目就不写了,就是文件输入输出,排序。 大致的程序实现如下: ?try { ? ? ? ? ? ? ? ? File file=new File("D://read.txt"); ? ? ? ? ? ? ? ? if(file.isFile() file.exists()){ //判断文件

昨天的interview遇到的一道题,现在记录下我的代码备以后参考。

题目就不写了,就是文件输入输出,排序。大致的程序实现如下:


?try { ? ? ? ? ? ? ? ? File file=new File("D://read.txt"); ? ? ? ? ? ? ? ? if(file.isFile() && file.exists()){ //判断文件是否存在 ? ? ? ? ? ? ? ? Long fileLengthLong = file.length(); ? ? ? ? ? ? ? ? ? ? ?byte[] fileContent = new byte[fileLengthLong.intValue()]; ? ? ? ? ? ? ? ? ? ? ?try { ? ? ? ? ? ? ? ? ? ? ?FileInputStream inputStream = new FileInputStream(file); ? ? ? ? ? ? ? ? ? ? ?inputStream.read(fileContent); ? ? ? ? ? ? ? ? ? ? inputStream.close(); ? ? ? ? ? ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? ? ? ? // TODO: handle exception ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ?String string = new String(fileContent); ? ? ? ? ? ? ? ? ? ? ?ByteArrayOutputStream out = new ByteArrayOutputStream(); ? ? ? ? ? ? ? ? ? ? ?MyComparator com = new MyComparator(); ? ? ? ? ? ? ? ? ? ? String [] nums = string.split("s+"); ? ? ? ? ? ? ? ? ? ? Arrays.sort(nums,com); ? ? ? ? ? ? ? ? ? ? for (String num : nums) { ? ? ? ? ? ? ? ? ? ? ?? out.write((num+ "rn").getBytes("GB2312")); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ?FileOutputStream output=new FileOutputStream("D://write.txt"); ? ? ? ? ? ? ? ? ? ?output.write(out.toByteArray()); ? ? ? ? ? ? ? ? ? ?output.close(); ? ? ? ? }else{ ? ? ? ? ? ? System.out.println("找不到指定的文件"); ? ? ? ? } ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? System.out.println("读取文件内容出错"); ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } ? ?private static class MyComparator implements Comparator<String> { @Override public int compare(String o1,String o2) { return Integer.valueOf(o1).compareTo(Integer.valueOf(o2)); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读