java file read write_java的read file 和write file

1.读文件

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

public class Readfile {

/*

读取文件流

*/

public BufferedReader bufferreader(String fileName) {

BufferedReader reader = null; //使用缓冲区的方法将数据读入到缓冲区中

try {

reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));

} catch (Exception e) {

e.printStackTrace();

}

return reader;

}

/*

读取文件中的某一行数据

*/

public String readfile(BufferedReader reader, int lineNumber) {

String line = null; //定义行数

try {

int num = 0;

while (true) {

num++;

line = reader.readLine();

if (num == lineNumber) {

break;

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (Exception e) {

e.printStackTrace();

}

}

return line;

}

}

public static void main(String args[]){

Readfile f=new Readfile();

BufferedReader br= f.bufferreader("add2.txt");

System.out.println(f.readfile(br,3));//获取某一行的数据

}

}

2.写文件

import java.io.*;

import java.math.BigInteger;

import java.security.MessageDigest;

import java.text.DecimalFormat;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

import java.util.*;

import java.util.concurrent.CountDownLatch;

public class Writefile2 {

private static String[] id={"LB13","RR13","LR14","LR15","LR12",

"LL13","LL14","LL12","LB14","LB12"};

private static int tmp=0;

private static FileWriter fw;

private static FileWriter fw2;

/*

/*

获取需要生成的文件的大小

*/

public BigInteger getsize(String size){

//System.out.println(size.substring(0,size.length()-1));

int x=Integer.valueOf(size.substring(0,size.length()-1));

BigInteger resultlen=new BigInteger("0");

if(size.endsWith("m")||size.endsWith("M")) {

resultlen = BigInteger.valueOf(x).multiply(BigInteger.valueOf(1024)).multiply(BigInteger.valueOf(1024));

}else if(size.endsWith("g")||size.endsWith("G")){

resultlen = BigInteger.valueOf(x).multiply(BigInteger.valueOf(1024)).multiply(BigInteger.valueOf(1024)).multiply(BigInteger.valueOf(1024));

}else if(size.endsWith("K")||size.endsWith("k")){

resultlen = BigInteger.valueOf(x).multiply(BigInteger.valueOf(1024));

}

return resultlen;

}

public synchronized static int getuuid()

{

tmp=tmp+1;

return tmp;

}

/*

如果数据量不大的情况下,10G以下,可以使用该方法

file:文件名字

code:插入的内容

size是文件大小,可以是1G,1k,1m。如果想表示100B, 请用0.1K

*/

public boolean writefile(int size) {

DecimalFormat count=new DecimalFormat("00000000000");

//经过测试:ufferedOutputStream执行耗时:1,1,1 毫秒

int tmp_len=0;

long begin0 = System.currentTimeMillis();

try {

StringBuffer data = new StringBuffer();

String uuid=null;

String uuid2=null;

MessageDigest md =MessageDigest.getInstance("MD5");

LocalDate localdate=LocalDate.now();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

String date=localdate.format(formatter);

Random rd=new Random();

int z=0;

for(int i=0;i

uuid=count.format(Integer.parseInt(String.valueOf(getuuid()))+1);

md.update(uuid.getBytes());

uuid2=(md.digest().toString()).substring(3);

while(true) { //为了保证结果一定是有8位长度

if (uuid2.length()!= 8) {

uuid2 = (md.digest().toString()).substring(3);

}else{

break;

}

}

z= rd.nextInt(10);

data.append("1|"); // 分区号

data.append("B201912138999"); //批次号

data.append("|C|1030844001362|"); //

data.append(date+id[z]+uuid2+uuid); //流水号

data.append("|CNY|1.01|0110|00|C1030844001362|00|D1ANA102Y05ME003000062|1|1|1|1|1|1|CMB10|"); //

data.append("2019-12-13-23.17.12.63504");//

data.append("|1|ANA200X05ME007000E67|ANAVCK00|");//

data.append(date);

data.append("|231712|1|A|1|1|");

data.append("\r\n");

fw.write(data.toString());

data.delete(0, data.length());

}

return true;

} catch (Exception e1) {

e1.printStackTrace();

return false;

} finally{

long end0 = System.currentTimeMillis();

System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 毫秒");

}

}

public boolean writefile2(int size) {

DecimalFormat count=new DecimalFormat("00000000000");

//经过测试:ufferedOutputStream执行耗时:1,1,1 毫秒

int tmp_len=0;

long begin0 = System.currentTimeMillis();

try {

StringBuffer data2 = new StringBuffer();

String uuid=null;

String uuid2=null;

MessageDigest md =MessageDigest.getInstance("MD5");

LocalDate localdate=LocalDate.now();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

String date=localdate.format(formatter);

Random rd=new Random();

int z=0;

for(int i=0;i

uuid=count.format(Integer.parseInt(String.valueOf(getuuid()))+1);

md.update(uuid.getBytes());

uuid2=(md.digest().toString()).substring(3);

while(true) { //为了保证结果一定是有8位长度

if (uuid2.length()!= 8) {

uuid2 = (md.digest().toString()).substring(3);

}else{

break;

}

}

z= rd.nextInt(10);

data2.append("1|"); // 分区号

data2.append("B201912138999"); //批次号

data2.append("|C1030844001362|");

data2.append(date+id[z]+uuid2+uuid); //流水号

//data2.append(date+"LR13"+uuid2+uuid); //流水号

data2.append("|CNY|0.12|0125|00|Q1030844001574|06|1|102100099996|C1030844001362|00|1|308584000013|1|"); //

data2.append("2019-12-13-23.17.12.63504");//

data2.append("|1|ANA200X05ME007000EUK|1|20191213|233601|1|A|2019121300000336507760041111111|");//

data2.append("|1|2019121300000336507760041111111|1|1|1|1|");

data2.append("\r\n");

fw2.write(data2.toString());

data2.delete(0, data2.length());

}

return true;

} catch (Exception e1) {

e1.printStackTrace();

return false;

} finally{

long end0 = System.currentTimeMillis();

System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 毫秒");

}

}

public static void main(String args[]){

/*

file:文件名字

code需要生成的内容

size :代表生成的文件的大小,单位只能是:G,K,M ,如果想表示B,请用0.1K

*/

Scanner scan=new Scanner(System.in);

int size=5000000;

int threadnum=2;

final CountDownLatch cd=new CountDownLatch(threadnum);

try {

fw = new FileWriter(new File("zxm.dat"),true); // 为了这张表数据ANA_VDT_CMB_T

//fw2 = new FileWriter(new File("zxm2.dat"),true); // 为了这张表数据ANA_VDT_CMB_T

} catch (IOException e) {

e.printStackTrace();

}

for(int j=0;j

System.out.println("开启线程:"+String.valueOf(j));

new Thread(new Runnable() {

public void run() {

boolean result=false;

boolean result2=false;

while (true) {

result= new Writefile2().writefile(size);

//result2= new Writefile2().writefile2(size);

if(result){

System.out.println("执行写文件成功");

break;

}

if(!result){

System.out.println("写文件失败");

break;

}

}

cd.countDown();

}

}).start();

}

try {

cd.await();

if(fw!=null){

fw.close();

}

if(fw2!=null){

fw2.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值