大数据开发预览器

/**
 * 大数据预览器.java
 * fileRead
 * Copyright (c) 2018, IT版权所有.
 * @author   IT_BULL                      
*/

package fileRead;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;

import javax.swing.JOptionPane;

/**
 * @author   IT_BULL                      
 * @Date     2018年6月17日      
 */
public class 大数据预览器 {

    public static void main(String[] args) throws Exception {
        String path = "G:\\不服来战\\1\\企业级实战java大数据视频Bigdata1212\\day1\\data\\string.txt";

        //统计文件行数
        int count =  CountFileLine(path);//3168
        
        //创建一个行数大小的数组
        //读取文件 读取一行统计字符长度 存入  文件长度数组
        int[] array = countLength(count , path);
        
        //在文件长度数组长度的基础上 ,创建一个索引数组
        //索引第一个元素 是 文件长度数组的第一个元素 ,
        //第二个元素 是文件长度的第一个和第二个长度之和
        System.out.println(Arrays.toString(array));
        
        int[] index = createIndex(array,count);
        
        System.out.println("------------------------");        
        System.out.println(Arrays.toString(index));
        
        //弹出框 接收输入查询行数 
        inputLine(index, path);
        //利用 文件随机读写进行 定位 快速预览
    }
    
    /**
     * @param index  生成好的索引数组
     * @param path   文件路径
     * @throws Exception 
    */
    private static void inputLine(int[] index , String path ) throws Exception {
        while(true)
        {
            try {
                    String lines = JOptionPane.showInputDialog("请输入要预览文件的行数:");
                    int line = Integer.parseInt(lines);
                    //因为数组下标从 0 开始
                    if(line > 0) {line = line - 1 ;}
                    //显示数据
                    showData(line , index , path);             
            }catch (Exception e) {
                System.out.println("你是认真的吗?");
            }        }    }
    
    //预览大数据
    private static void showData(int line, int[] index, String path) throws Exception {
        
        Scanner sc = new Scanner(new File(path));
        
        //创建一个文件随机读写对象        
        RandomAccessFile raf = new RandomAccessFile(new File(path) , "r");
        //移动文件指针  数组中的元素是文件中行数的长度之和
        
        // 防御式 编程  防止空指针 
        int j = 2;
        if(index.length - line > j  &&  line < index.length ) {
            raf.seek(index[line]);
        }else {
            raf.seek( index.length - j );
        }
        //预览行数为10 行
        for (int i = 0; i < j ; i++) {
                String  str = new String( raf.readLine().getBytes("ISO-8859-1"),"utf-8");
                System.out.println(str);
        }            }
    
    //创建索引数组
    private static int[] createIndex(int[] array,int count) {
        for (int i = 1; i < array.length; i++) {
            array[i] += array[i - 1];
        }
        return array;
    }

    //统计文件每一行的长度,返回一个数组
    private static int[] countLength(int count ,String path) throws Exception {
        int [] array = new int[count] ;
        
        Scanner sc = new Scanner(new File(path));
        int i = 0;
        while(sc.hasNextLine()) {
            array[i] = sc.nextLine().length();
            i++;    
        }
        return array;    
    }

    private static int CountFileLine(String path) throws Exception {
        
        int i = 0;
        Scanner sc = new Scanner(new File(path));
        while(sc.hasNextLine()) {
            sc.nextLine();
            i++;
        }
        return i;
    }
}

 

posted @ 2018-06-19 22:32  马鞍山  阅读(231)  评论(0编辑  收藏  举报