Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说java快速读取文件,希望能够帮助你!!!。
java读取文件的方式有多种,有些慢,有些快,当读取大量的文件时,速度尤为重要。以下是鄙人做项目时发现读取文件过慢时,对两种读取文件方式快慢的测试。
开门见山:这是快速读取的代码:
static public String readFile(File file) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
// 把每次读取的内容写入到内存中,然后从内存中获取
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 只要没读完,不断的读取
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
// 得到内存中写入的所有数据
byte[] data = outputStream.toByteArray();
fileInputStream.close();
return new String(data);
//return new String(data, "GBK");//以GBK(什么编码格式)方式转
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
这是比较慢读取的代码:
static public String getContent(File file) {
StringBuffer result = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
result.append(s).append("\n");
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
这是不可取的读取的代码:
static public String getContent(File file) {
//在着再次强调一个“+”号,数据多的时间绝对不能用“+”
String result = "";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
result = result + "\n" + s;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
测试结果:
测试类:
package com.atgeretg.test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class Test {
public static void main(String[] args) {
findFile("J:\\lhc");
}
static public void findFile(String path) {
// Dao dao = new Dao();
File folder = new File(path);// 获取文件夹路径
if (folder.isDirectory()) {
String[] nameS = folder.list();// 获取文件名
String pathWindows = folder.getAbsolutePath();
System.out.println("path = " + pathWindows);
String[] cont = new String[nameS.length];
long millisRead = System.currentTimeMillis();
for (int i = 0; i < nameS.length; i++) {
String filePath = pathWindows + "\\" + nameS[i];
//System.out.println(filePath);
File f = new File(filePath);
String content = readFile(f);
// getNeedData(content);
cont[i] = content;
}
System.out.println("read 快的 time = "
+ (System.currentTimeMillis() - millisRead)+"ms; 共:"+nameS.length+"个文件");
millisRead = System.currentTimeMillis();
for (int i = 0; i < nameS.length; i++) {
String filePath = pathWindows + "\\" + nameS[i];
//System.out.println(filePath);
File f = new File(filePath);
String content = getContent1(f);
// getNeedData(content);
cont[i] = content;
}
System.out.println("read 没有加号的 time = "
+ (System.currentTimeMillis() - millisRead)+"ms; 共:"+nameS.length+"个文件");
millisRead = System.currentTimeMillis();
for (int i = 0; i < nameS.length; i++) {
String filePath = pathWindows + "\\" + nameS[i];
//System.out.println(filePath);
File f = new File(filePath);
String content = getContent(f);
// getNeedData(content);
cont[i] = content;
}
System.out.println("read 带有加号的 time = "
+ (System.currentTimeMillis() - millisRead)+"ms; 共:"+nameS.length+"个文件");
}
}
static public String getContent(File file) {
String result = "";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
result = result + "\n" + s;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
static public String getContent1(File file) {
StringBuffer result = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), Charset.forName("GBK")));// 构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
result.append(s).append("\n");
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
// result.getBytes(charset)
return result.toString();
}
static public String readFile(File file) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
// 把每次读取的内容写入到内存中,然后从内存中获取
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 只要没读完,不断的读取
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
// 得到内存中写入的所有数据
byte[] data = outputStream.toByteArray();
fileInputStream.close();
//return new String(data);
return new String(data, "GBK");//以GBK方式转,不会出现中文乱码就不在加,加上速度会相对慢一些
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
static public String readFile1(File file) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
// 把每次读取的内容写入到内存中,然后从内存中获取
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 只要没读完,不断的读取
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
// 得到内存中写入的所有数据
byte[] data = outputStream.toByteArray();
fileInputStream.close();
return new String(data);
// return new String(data, "GBK");//以GBK方式转,不会出现中文乱码就不在加,加上速度会相对慢一些
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
下一篇