
18 out = new FileOutputStream(f)19 out.write(content.getBytes())20 out.close()21 long end1 = System.currentTimeMillis()22 FileOutputStreamTime += end1 - begin123 24 //Test BufferedOutputStream 25 long begin2 = System.currentTimeMillis()26 outStr = new FileOutputStream(f)27 buf = new BufferedOutputStream(outStr)28 buf.write(content.getBytes())29 buf.flush()30 buf.close()31 long end2 = System.currentTimeMillis()32 BufferedOutputStreamTime += end2 - begin233 34 //Test FileWriter 35 long begin3 = System.currentTimeMillis()36 // the second parameter "true",Whether or not a file will be covered 37 // or appended. 38 fw = new FileWriter(f)39 // fw = new FileWriter("d:/test/testwrite/add2.txt",true)40 fw.write(content)41 fw.close()42 long end3 = System.currentTimeMillis()43 FileWriterTime += end3 - begin344 } catch (Exception e) { 45 e.printStackTrace()46 } finally { 47 try { 48 fw.close()49 buf.close()50 outStr.close()51 out.close()52 } catch (Exception e) { 53 e.printStackTrace()54 } 55 } 56 } 57 58 public static void read(String filePath){ 59 FileInputStream in = null60 BufferedInputStream buf = null61 FileReader reader = null62 BufferedReader br = null63 StringBuffer sb = new StringBuffer()64 65 try { 66 //Test FileInputStream 67 long begin1 = System.currentTimeMillis()68 File f = new File(filePath)69 in = new FileInputStream(f)70 int len1 = 51271 byte[] bytes1 = new byte[len1]72 73 while ((len1 = in.read(bytes1, 0, len1)) != -1) { 74 if(len1 <512){ 75 byte[] tmpBuf = new byte[len1]76 System.arraycopy(bytes1, 0, tmpBuf, 0, len1)
77 sb.append(new String(tmpBuf))78 tmpBuf = null79 }else{ 80 sb.append(new String(bytes1))81 } 82 } 83 84 in.close()85 long end1 = System.currentTimeMillis()86 FileInputStreamTime += end1 - begin187 88 //Test BufferedInputStream 89 long begin2 = System.currentTimeMillis()90 int len2 = 51291 byte[] bytes2 = new byte[len2]92 buf = new BufferedInputStream(new FileInputStream(f))93 while ((len2 = buf.read(bytes2, 0, len2)) != -1) { 94 if(len2 <512){ 95 byte[] tmpBuf = new byte[len2]96 System.arraycopy(bytes2, 0, tmpBuf, 0, len2)
97 sb.append(new String(tmpBuf))98 tmpBuf = null99 }else{100 sb.append(new String(bytes2))101 }102 }103 104 buf.close()105 long end2 = System.currentTimeMillis()106 BufferedInputStreamTime += end2 - begin2107 108 //Test FileReader109 long begin3 = System.currentTimeMillis()110 reader = new FileReader(f)111 br = new BufferedReader(reader)112 String str113 while ((str = br.readLine()) != null) {114 sb.append(str)115 }116 br.close()117 reader.close()118 long end3 = System.currentTimeMillis()119 FileReaderTime += end3 - begin3120 121 } catch (Exception e) {122 e.printStackTrace()123 } finally {124 try {125 br.close()126 reader.close()127 in.close()128 buf.close()129 } catch (Exception e) {130 e.printStackTrace()131 }132 }133 }
int readSize = 0byte buffer[] = new byte[1024]
while ((readSize = is.read(buffer, 0, 1024)) != -1) {
os.write(buffer, 0, readSize)
os.flush()
}
你看,写多大不就你说了算么(read里控制的)
java写大文件并没有什么不妥,java本身的速度也还是可以的 。往往是硬盘本身的速度限制了写入速度。
如果不是java一般的思路是将 文件 分片 写入 不同的 盘片上,就是说通过硬盘本身的多磁头并发处理,然后再将文件合并(将各个文件碎片连在一起),但是java好像不能处理到这个层面。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)