java创建byte数组_java.创建一个byte数组,long length = file.length(); byte[] bytes =

导航:网站首页 >

java.创建一个byte数组,long length = file.length(); byte[] bytes =

时间:2017-12-17

java.创建一个byte数组,long length = file.length(); byte[] bytes = new byte[(int)length]; 结果导致

问题错误为:java.lang.OutOfMemoryError:Java heap space

\x05at com.hitrust.FCBCNB.service.impl.MONServiceImpl.getBytesFromFile(MONServiceImpl.java:76)

\x05at com.hitrust.FCBCNB.service.impl.MONServiceImpl.spliteBytes(MONServiceImpl.java:97)

\x05at com.hitrust.FCBCNB.service.impl.MONServiceImpl.getFileLine(MONServiceImpl.java:59)

\x05at com.hitrust.FCBCNB.service.impl.MONServiceImpl.upload(MONServiceImpl.java:45)

\x05at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

\x05at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

\x05at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

\x05at java.lang.reflect.Method.invoke(Method.java:597)

\x05at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)

\x05at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)

\x05at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)

\x05at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)

\x05at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)

\x05at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)

\x05at $Proxy6.upload(Unknown Source)

\x05at com.hitrust.FCBCNB.test.MonTest.testA(MonTest.java:30)

\x05at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

\x05at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

\x05at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

\x05at java.lang.reflect.Method.invoke(Method.java:597)

\x05at junit.framework.TestCase.runTest(TestCase.java:154)

\x05at junit.framework.TestCase.runBare(TestCase.java:127)

\x05at junit.framework.TestResult$1.protect(TestResult.java:106)

\x05at junit.framework.TestResult.runProtected(TestResult.java:124)

\x05at junit.framework.TestResult.run(TestResult.java:109)

\x05at junit.framework.TestCase.run(TestCase.java:118)

\x05at junit.framework.TestSuite.runTest(TestSuite.java:208)

\x05at junit.framework.TestSuite.run(TestSuite.java:203)

\x05at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)

\x05at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

\x05at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)

\x05at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)

请问该如何解决,这里的length大小为150994943.

相关问题:

匿名网友:

好恐怖的长度

大家还关注:

问题推荐

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这个方法的步骤如下: 1. 创建一个 FileInputStream 对象,使用文件路径作为参数来创建。这个对象负责从文件中读取数据。 2. 获取文件的大小,以便创建一个合适大小的 byte 数组来存储文件内容。可以使用 File 类的 length() 方法获取文件大小。 3. 创建一个 byte 数组,大小为文件大小。 4. 调用 FileInputStream 的 read() 方法,将文件内容读取到 byte 数组中。 5. 关闭 FileInputStream 对象。 6. 返回 byte 数组。 以下是一个示例实现: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FileUtils { public static byte[] readToBytes(File file) throws IOException { // 创建 FileInputStream 对象 FileInputStream fileInputStream = new FileInputStream(file); // 获取文件大小 long fileSize = file.length(); // 创建一个 byte 数组,大小为文件大小 byte[] bytes = new byte[(int) fileSize]; // 读取文件内容到 byte 数组中 int bytesRead = fileInputStream.read(bytes); // 确保已读取整个文件内容 while (bytesRead < fileSize) { int remainingBytes = (int) fileSize - bytesRead; int bytesToRead = Math.min(remainingBytes, 4096); bytesRead += fileInputStream.read(bytes, bytesRead, bytesToRead); } // 关闭 FileInputStream fileInputStream.close(); // 返回 byte 数组 return bytes; } } ``` 注意,这个实现方法中的 while 循环用于确保已读取整个文件内容。由于 read() 方法不一定能够在一次调用中读取整个文件内容,因此需要循环读取,直到已读取整个文件为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值