• 读取Java文件到byte数组的三种方式


    [java]view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. packagezs;
    2. importjava.io.BufferedInputStream;
    3. importjava.io.ByteArrayOutputStream;
    4. importjava.io.File;
    5. importjava.io.FileInputStream;
    6. importjava.io.FileNotFoundException;
    7. importjava.io.IOException;
    8. importjava.io.RandomAccessFile;
    9. importjava.nio.ByteBuffer;
    10. importjava.nio.MappedByteBuffer;
    11. importjava.nio.channels.FileChannel;
    12. importjava.nio.channels.FileChannel.MapMode;
    13. publicclassFileUtils{
    14. publicbyte[]getContent(StringfilePath)throwsIOException{
    15. Filefile=newFile(filePath);
    16. longfileSize=file.length();
    17. if(fileSize>Integer.MAX_VALUE){
    18. System.out.println("filetoobig...");
    19. returnnull;
    20. }
    21. FileInputStreamfi=newFileInputStream(file);
    22. byte[]buffer=newbyte[(int)fileSize];
    23. intoffset=0;
    24. intnumRead=0;
    25. while(offset<buffer.length
    26. &&(numRead=fi.read(buffer,offset,buffer.length-offset))>=0){
    27. offset+=numRead;
    28. }
    29. //确保所有数据均被读取
    30. if(offset!=buffer.length){
    31. thrownewIOException("Couldnotcompletelyreadfile"
    32. +file.getName());
    33. }
    34. fi.close();
    35. returnbuffer;
    36. }
    37. /**
    38. *thetraditionalioway
    39. *
    40. *@paramfilename
    41. *@return
    42. *@throwsIOException
    43. */
    44. publicstaticbyte[]toByteArray(Stringfilename)throwsIOException{
    45. Filef=newFile(filename);
    46. if(!f.exists()){
    47. thrownewFileNotFoundException(filename);
    48. }
    49. ByteArrayOutputStreambos=newByteArrayOutputStream((int)f.length());
    50. BufferedInputStreamin=null;
    51. try{
    52. in=newBufferedInputStream(newFileInputStream(f));
    53. intbuf_size=1024;
    54. byte[]buffer=newbyte[buf_size];
    55. intlen=0;
    56. while(-1!=(len=in.read(buffer,0,buf_size))){
    57. bos.write(buffer,0,len);
    58. }
    59. returnbos.toByteArray();
    60. }catch(IOExceptione){
    61. e.printStackTrace();
    62. throwe;
    63. }finally{
    64. try{
    65. in.close();
    66. }catch(IOExceptione){
    67. e.printStackTrace();
    68. }
    69. bos.close();
    70. }
    71. }
    72. /**
    73. *NIOway
    74. *
    75. *@paramfilename
    76. *@return
    77. *@throwsIOException
    78. */
    79. publicstaticbyte[]toByteArray2(Stringfilename)throwsIOException{
    80. Filef=newFile(filename);
    81. if(!f.exists()){
    82. thrownewFileNotFoundException(filename);
    83. }
    84. FileChannelchannel=null;
    85. FileInputStreamfs=null;
    86. try{
    87. fs=newFileInputStream(f);
    88. channel=fs.getChannel();
    89. ByteBufferbyteBuffer=ByteBuffer.allocate((int)channel.size());
    90. while((channel.read(byteBuffer))>0){
    91. //donothing
    92. //System.out.println("reading");
    93. }
    94. returnbyteBuffer.array();
    95. }catch(IOExceptione){
    96. e.printStackTrace();
    97. throwe;
    98. }finally{
    99. try{
    100. channel.close();
    101. }catch(IOExceptione){
    102. e.printStackTrace();
    103. }
    104. try{
    105. fs.close();
    106. }catch(IOExceptione){
    107. e.printStackTrace();
    108. }
    109. }
    110. }
    111. /**
    112. *MappedFilewayMappedByteBuffer可以在处理大文件时,提升性能
    113. *
    114. *@paramfilename
    115. *@return
    116. *@throwsIOException
    117. */
    118. publicstaticbyte[]toByteArray3(Stringfilename)throwsIOException{
    119. FileChannelfc=null;
    120. try{
    121. fc=newRandomAccessFile(filename,"r").getChannel();
    122. MappedByteBufferbyteBuffer=fc.map(MapMode.READ_ONLY,0,
    123. fc.size()).load();
    124. System.out.println(byteBuffer.isLoaded());
    125. byte[]result=newbyte[(int)fc.size()];
    126. if(byteBuffer.remaining()>0){
    127. //System.out.println("remain");
    128. byteBuffer.get(result,0,byteBuffer.remaining());
    129. }
    130. returnresult;
    131. }catch(IOExceptione){
    132. e.printStackTrace();
    133. throwe;
    134. }finally{
    135. try{
    136. fc.close();
    137. }catch(IOExceptione){
    138. e.printStackTrace();
    139. }
    140. }
    141. }
    142. }
  • 相关阅读:
    模拟退火
    斜率优化DP
    LuoguP2292 L语言
    百度云真是地址解析,满速下载
    Android开发之Activity横竖屏切换生命周期重建问题
    Android开发之延时执行
    Android开发之import org.apache.http
    Android Studio快捷键
    Android开发之应用程序的安装
    Activity去Title的几种方式
  • 原文地址:https://www.cnblogs.com/xue88ming/p/7183006.html
Copyright © 2020-2023  润新知