一、
1、本地磁盘读取大文件(可能是本地记事本打不开的文本文件),并获取需要的某个数据,显示一共有多少条数据----大数据文本浏览器
public static int getLine() throws FileNotFoundException, IOException
{ //获得行数
int line=0;
String path="F:\\data\\kaifang.txt";
InputStream fin=new FileInputStream(path);//创建文件读取流
Scanner sc=new Scanner (fin,"GBK");//扫描文件
while(sc.hasNextLine())
{
String rdstr=sc.nextLine();
if(rdstr.contains("嘿嘿"))
{
System.out.println(rdstr);//打印 可以打印某一行的数据内容
}
line++;
}
fin.close();
sc.close();
return line; //可以返回数据的行数
}
2、加载到内存,实现快速查找 二次赋值实现索引快速读取读取文件
生成索引用到了随机存取文件RandomAccessFile及方法 raf.seek(index[line]);
public static void init( int [] index) throws FileNotFoundException, IOException
{
System.out.println("init start");
int line=0;
String path="F:\\data\\kaifang.txt";
InputStream fin=new FileInputStream(path);//创建文件读取流
Scanner sc=new Scanner (fin,"GBK");//扫描文件
while(sc.hasNextLine() )
{
if(line<index.length)//避免越界
{
String rdstr=sc.nextLine();
index[line]=rdstr.length();//获取每一行长度
line++;
}
else
{
break;
}
}
//11 12 15 14 19 21
//11 23 38 52 71 92
//完成索引
for(int j=1;j<index.length;j++)
{
index[j]+=index[j-1];
}
fin.close();
sc.close();
System.out.println("init end");
}
//数据显示
public static void show(int [] index,int line ) throws FileNotFoundException, IOException
{
System.out.println("show start");
String path="F:\\data\\kaifang.txt";
RandomAccessFile raf=new RandomAccessFile(path,"r");//只读的打开随机访问数据流
raf.seek(index[line]);//移动文件指针
for(int i=0;i<5;i++) //一次显示5条数据
{
String str1=new String(raf.readLine().getBytes("ISO-8859-1"),"GBK");
System.out.println(str1);
}
raf.close();
System.out.println("show end");
}
编写测试类
public static void main(String[] args) throws FileNotFoundException, IOException
{
//20151574
//System.out.println(getLine());
int [] index =new int[20151574];
init(index);//初始化
while(true)
{
String inputstr=JOptionPane.showInputDialog("输入你要查看的行数:");
int line=Integer.parseInt(inputstr);//数据转换,输入默认是字符串
show( index,line );
}
}
数组index[]两次赋值:
索引一:当前行的字节数
索引二:当前行前边所有行的字节数
可以search()或者show()方法中,可以如上RandomAccessFile辅助进行索引查询,也可以直接进行遍历查找。
上读的是int数组读的时长度,下着直接读的是内容。
二、磁盘本地实现某一数据或者多个数据在数量级文本文件中的查找
public static void main(String[] args) throws FileNotFoundException
{
String [] QQ=new String[] { "1111","222","222","3333","444","555","666","777"};
String path="F:\\data\\1E~001.txt";
InputStream fin=new FileInputStream (path);
Scanner sin=new Scanner(fin);
while(sin.hasNextLine())
{
String readstr=sin.nextLine();
for(String myqq:QQ)
{
if(readstr.contains(myqq))
{
System.out.println(readstr);
}
}
}
}
三 、将文本文件加载内存,查询某一个数据的详细内容
public static void init(String [] strs) throws FileNotFoundException, IOException
{
System.out.println("init start");
String path="F:\\data\\1E~001.txt";
InputStream fin=new FileInputStream(path);//创建文件读取流
Scanner sc=new Scanner (fin,"GBK");//扫描文件
int i=0;
while(sc.hasNextLine() )
{
if(i< strs.length)
{
String rdstr=sc.nextLine();
strs[i]=rdstr;
i++;
}
else
{
break;
}
}
fin.close();
sc.close();
System.out.println("init end");
}
public static void search(String str, String [] strs)
{
for(String strall:strs)
{
if(strall.contains(str))
{
System.out.println(strall);
}
}
}
public static void main(String[] args) throws IOException
{
final int N=10;
String [] strs=new String[N];
init( strs) ;
while(true)
{
String inputstr=JOptionPane.showInputDialog("输入你查询的数据");
search(inputstr,strs);
}
}
.