java获取字符串行数据_java快速获取文件行数方法及简单实现字符串类型转换为其他数据类型...

package filesplit;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.LineNumberReader;

public class LineCount {

public

static void main(String[] args) throws IOException {

LineCount app = new LineCount();

app.compareBufferAndLineNumber();

}

public void

compareBufferAndLineNumber() throws IOException {

String fileName =

"F:\\mywork\\filesplit\\AllLog";

long time = System.currentTimeMillis();

System.out.println("LineNumberReader"

+ this.getTotalLines(fileName));

System.out.println(System.currentTimeMillis() - time);

time = System.currentTimeMillis();

System.out.println("BufferedReader"

+ this.getTotalLines2(fileName));

System.out.println(System.currentTimeMillis() - time);

time = System.currentTimeMillis();

System.out.println("BufferedInputStream"

+ this.count(fileName));

System.out.println(System.currentTimeMillis() - time);

// 从输出结果来看,反而是BufferedInputStream是最快的。

// LineNumberReader8663721

// 6203

// BufferedReader8663721

// 6016

// BufferedInputStream8663720

// 1609

}

private int

getTotalLines(String fileName) throws IOException {

FileReader in = new FileReader(fileName);

LineNumberReader reader = new LineNumberReader(in);

String strLine = reader.readLine();

int totalLines = 0;

while (strLine != null) {

totalLines++;

strLine = reader.readLine();

}

reader.close();

in.close();

return totalLines;

}

对于用java读取文件数据进行处理时,推荐使用这种方法;

因为常常从文件读取的数据并非我们直接可以处理的数据,如java一般采用字节流和字符流读取和写入;

当要处理浮点型时、整型、等其他非字符串和字节型时,在处理之前,就必须把读取或写入的数据

进行数据转换;

采用下面的方法,可以方便读非字符串和字节数组的数据,可以很快的进行数据类型的转换;

可以在写入数据的时候,写一个数据,再换一行,在写入一个数据,重复此过程直到

所有数据都被写入到文件;

读数据的时候,首先先获取文件的总行数,然后开辟一个对应数据类型的动态空间;把每次读取的每行

数据转换为要处理的数据类型;

package com.wjr.readdata;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

public class DemoReadData {

public static void main(String args[]) throws Exception{

File mFile = new

File("f:"+File.separator+

"ECGSampleData"+File.separator+"out1.txt");

//声明一个BufferReader对象

BufferedReader bReader = null;

//用字符流对象实例化声明的BufferReader对象

bReader = new BufferedReader(new

FileReader(mFile));

//声明一个记录文件行数的变量

int readline = 0;

//声明一个保存文件每行数据的String变量

String readString =

bReader.readLine();

while(readString != null)//获取文件行数

{

readline++;

readString =

bReader.readLine();

}

bReader.close();

//进行字符串与浮点类型数据转换时,先关闭刚操作的文件

//定义一个浮点型数组对象

float readData[] = new float[readline];

//重新打开要读取数据的文件

bReader = new BufferedReader(new

FileReader(mFile));

//判断文件是否支持重定位操作

if(bReader.markSupported())

{

readline = 0;

//把读取数据的指针操作符定位到文件的起始点

bReader.mark(readline);

//判断文件是否准备好读

if(bReader.ready())

{

//读取第一行数据

readString =

bReader.readLine();

//当读到文件的结尾时,readString为null,跳出while循环

while(readString != null)

{

//把字符串数据转换为浮点型数据

readData[readline] = Float.parseFloat(readString);

//读取下一行数据

readString = bReader.readLine();

//记录行数的变量加1

readline++;

}

//打印文件的行数

System.out.print("the file line

is---->" +

readline);

//打印文件的最后一个数据,可以打开文件,验证读取的数据是否正确

System.out.println("the last content of array

is--->"+ readData[readline -

1]);

}

}

//最后关闭bufferReader流操作

bReader.close();

}

}

以上是个人的方法,如果读者有更好的方法,期待留言, 给一个相互学习的机会!

public int

count(String filename) throws IOException {

InputStream is = new BufferedInputStream(new

FileInputStream(filename));

byte[] c = new byte[1024];

int count = 0;

int readChars = 0;

while ((readChars = is.read(c)) != -1) {

for (int i = 0; i < readChars; ++i) {

if (c[i] == '\n')

++count;

}

}

is.close();

return count;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值