日期:2014-05-20  浏览次数:20918 次

将大文件输入到内存,内存溢出怎么解决?
这是我编写一段文件分割器代码,当我写入小的文件时,可以分割的,但是大的文件放进去,就内存溢出,求指教
package com.chenyan;

import java.io.*;


public class FenGe {
private String path;
private String shuchu;
private int n;

public FenGe(String path,String shuchu,int n){
this.path=path;
this.shuchu=shuchu;
this.n=n;
}

public void fun() throws IOException{
File file =new File(path);
long s=file.length();
System.out.println(s);
byte[] bytes=toByteArray(file);//文件的字节数
int count=bytes.length/n;//被分后每个文件的大小
String name=file.getName();//得到文件名称
String[] strs=name.split("\\.");
for(int i=0;i<n;i++){
String mingzi=strs[0]+"_"+i;
String houzhui = strs[1];
mingzi=mingzi.concat(".");
name=mingzi.concat(houzhui);
int begin=i*count;
if(i==n-1){
count=bytes.length-i*count;
}
write(shuchu+"\\"+name,bytes,begin,count);
}
}

private void write(String path, byte[] bytes, int begin, int count) throws IOException {
OutputStream out=new FileOutputStream(path);
out.write(bytes,begin,count);
out.close();
}

private static byte[] toByteArray(File file) throws IOException {
InputStream in=new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte[] buff=new byte[1024];
int cnt;
byte[] bytes=new byte[(int)file.length()];
while((cnt=in.read(buff))!=-1){
out.write(buff,0,cnt);
}
bytes=out.toByteArray();
in.close();
out.close();
return bytes;
}
}
这是报错Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2786)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
at com.chenyan.FenGe.toByteArray(FenGe.java:51)
at com.chenyan.FenGe.fun(FenGe.java:21)
at com.chenyan.MyFrame$5.actionPerformed(MyFrame.java:290)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)