java读取写入文件几种方式效率比较
来源:互联网 发布:h型钢重量计算软件 编辑:程序博客网 时间:2024/04/30 15:59
public class ReadTxtJson {
public static String readTxtFile(String FileName) throws Exception {
BufferedInputStream bufferedInputStream = null;
ByteArrayOutputStream memStream = null;
byte[] data = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(FileName));
memStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(buffer)) != -1){
memStream.write(buffer, 0, len);
}
data = memStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(memStream != null){
memStream.close();
}
if(bufferedInputStream != null){
bufferedInputStream.close();
}
} catch (IOException e) {
e.getStackTrace();
}
}
String s = new String(data);
if(s != null){
bufferedWriter(s,"D:\\FtpFile\\test1.txt");
}
return new String(data);
}
/**
* 以行为单位读写文件内容
*
* @param filePath
*/
public static String readTxtFileJson(String filePath) throws Exception{
File file = new File(filePath);
InputStreamReader read = null;
StringBuffer sb = null;
try {
//判断文件是否存在
if(file.isFile() && file.exists()){
read = new InputStreamReader(new FileInputStream(file),"utf-8");
BufferedReader bufferedReader = new BufferedReader(read);
sb = new StringBuffer();
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
sb.append(lineTxt);
}
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
e.getStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
e.getStackTrace();
}
}
}
if(sb != null){
bufferedWriter(sb.toString(),"D:\\FtpFile\\test2.txt");
}
return sb != null ? sb.toString() : null; // GsonUtil.transJsonStrToObject(sb.toString(), KubeData.class)
}
/**
* 缓冲字符写入文件,写字符串,数组或字符数据
* @param content
* @throws Exception
*/
public static void bufferedWriter(String content,String filePath) throws Exception{
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(new File(filePath).getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(bw != null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件输出流,必须将数据转换为字节,并保存到文件
* @param content
* @throws Exception
*/
public static void fileOutputStream(String content,String filePath) throws Exception{
FileOutputStream fop = null;
try {
fop = new FileOutputStream(new File(filePath));
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 测试1:
* 文件大小:2m
* 读取:readTxtFileJson 行读,写入:bufferedWriter 缓冲字符写入
* 用时:51秒
*
* 读取:readTxtFile 缓冲读取,写入:bufferedWriter 缓冲字符写入
* 用时:31秒
*
* 测试2:
* 文件大小:10m
* 读取:readTxtFileJson 行读,写入:fileOutputStream 文件输出流写入
* 用时:501秒
*
* 读取:readTxtFile 缓冲读取,写入:fileOutputStream 文件输出流写入
* 用时:172秒
*
* 文件大小:10m
* 读取:readTxtFileJson 行读,写入:bufferedWriter 缓冲字符写入
* 用时:293秒
*
* 读取:readTxtFile 缓冲读取,写入:bufferedWriter 缓冲字符写入
* 用时:132秒
*
* 总结:
* 不按格式读取效率高写入文件后大小比源文件小:readTxtFile 缓冲读取,bufferedWriter 缓冲字符写入
* 按格式读取效率偏低(是第一种方式的一倍左右)写入文件后大小比源文件大小相当:readTxtFileJson 行读 ,bufferedWriter 缓冲字符写入
* @param args
*/
public static void main(String[] args) {
try {
long date1 = System.currentTimeMillis();
String s = readTxtFileJson("D://FtpFile//get_services.txt");
//System.out.println(s);
System.out.println(System.currentTimeMillis()-date1);
// if(Util.isNotNull(kubeData)){
// System.out.println(kubeData.getKind()+"=="+kubeData.getApiVersion());
// }
long date2 = System.currentTimeMillis();
String s1 = readTxtFile("D://FtpFile//get_services.txt");
//System.out.println(s1);
System.out.println(System.currentTimeMillis()-date2);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String readTxtFile(String FileName) throws Exception {
BufferedInputStream bufferedInputStream = null;
ByteArrayOutputStream memStream = null;
byte[] data = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(FileName));
memStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(buffer)) != -1){
memStream.write(buffer, 0, len);
}
data = memStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(memStream != null){
memStream.close();
}
if(bufferedInputStream != null){
bufferedInputStream.close();
}
} catch (IOException e) {
e.getStackTrace();
}
}
String s = new String(data);
if(s != null){
bufferedWriter(s,"D:\\FtpFile\\test1.txt");
}
return new String(data);
}
/**
* 以行为单位读写文件内容
*
* @param filePath
*/
public static String readTxtFileJson(String filePath) throws Exception{
File file = new File(filePath);
InputStreamReader read = null;
StringBuffer sb = null;
try {
//判断文件是否存在
if(file.isFile() && file.exists()){
read = new InputStreamReader(new FileInputStream(file),"utf-8");
BufferedReader bufferedReader = new BufferedReader(read);
sb = new StringBuffer();
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
sb.append(lineTxt);
}
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
e.getStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
e.getStackTrace();
}
}
}
if(sb != null){
bufferedWriter(sb.toString(),"D:\\FtpFile\\test2.txt");
}
return sb != null ? sb.toString() : null; // GsonUtil.transJsonStrToObject(sb.toString(), KubeData.class)
}
/**
* 缓冲字符写入文件,写字符串,数组或字符数据
* @param content
* @throws Exception
*/
public static void bufferedWriter(String content,String filePath) throws Exception{
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(new File(filePath).getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(bw != null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件输出流,必须将数据转换为字节,并保存到文件
* @param content
* @throws Exception
*/
public static void fileOutputStream(String content,String filePath) throws Exception{
FileOutputStream fop = null;
try {
fop = new FileOutputStream(new File(filePath));
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 测试1:
* 文件大小:2m
* 读取:readTxtFileJson 行读,写入:bufferedWriter 缓冲字符写入
* 用时:51秒
*
* 读取:readTxtFile 缓冲读取,写入:bufferedWriter 缓冲字符写入
* 用时:31秒
*
* 测试2:
* 文件大小:10m
* 读取:readTxtFileJson 行读,写入:fileOutputStream 文件输出流写入
* 用时:501秒
*
* 读取:readTxtFile 缓冲读取,写入:fileOutputStream 文件输出流写入
* 用时:172秒
*
* 文件大小:10m
* 读取:readTxtFileJson 行读,写入:bufferedWriter 缓冲字符写入
* 用时:293秒
*
* 读取:readTxtFile 缓冲读取,写入:bufferedWriter 缓冲字符写入
* 用时:132秒
*
* 总结:
* 不按格式读取效率高写入文件后大小比源文件小:readTxtFile 缓冲读取,bufferedWriter 缓冲字符写入
* 按格式读取效率偏低(是第一种方式的一倍左右)写入文件后大小比源文件大小相当:readTxtFileJson 行读 ,bufferedWriter 缓冲字符写入
* @param args
*/
public static void main(String[] args) {
try {
long date1 = System.currentTimeMillis();
String s = readTxtFileJson("D://FtpFile//get_services.txt");
//System.out.println(s);
System.out.println(System.currentTimeMillis()-date1);
// if(Util.isNotNull(kubeData)){
// System.out.println(kubeData.getKind()+"=="+kubeData.getApiVersion());
// }
long date2 = System.currentTimeMillis();
String s1 = readTxtFile("D://FtpFile//get_services.txt");
//System.out.println(s1);
System.out.println(System.currentTimeMillis()-date2);
} catch (Exception e) {
e.printStackTrace();
}
}
0 0
- java读取写入文件几种方式效率比较
- java IO写入文件效率——几种方法比较
- JAVA几种读取文件的方式
- java读取文件的几种方式
- java读取文件的几种方式
- java读取文件几种方式
- java读取文件的几种方式
- java读取文件内容几种方式
- java向txt中写入字符串的几种方式以及效率
- java 向txt中写入字符串的几种方式效率测试代码
- 向文件中写入数据读取数据的几种方式
- Java从控制台读取数据,输入流System.in的几种方法效率比较
- Java:io流写入文件的四种实现方式的效率对比
- 几种读取属性文件的JAVA实现方式
- 几种读取属性文件的JAVA实现方式
- Java读取文件内容的几种方式集合
- java几种读取.properties文件的方式
- Java读取文件内容的几种方式总结
- mysql5.1升级5.6步骤以及出现问题解决方法
- SDUT OJ 3402数据结构实验之排序五:归并求逆序数
- ThinkPHP中,display和assign用法
- codeforces 701 C. They Are Everywhere (尺取法)
- LayoutParams——基础概念与使用
- java读取写入文件几种方式效率比较
- 51node-1120 机器人走方格(卡特兰数列+Lucas定理)
- 特征工程中常用的技术
- Android中如何去组Json格式的数据
- Node.Js Path模块-文件或文件夹路径字符串操作
- 38. Count and Say
- set 写法
- python执行文件
- 文件分割