写大文件内存溢出的问题

还则我 2007-06-06 03:35:04
如题,
怎样写出一个100M左右的文件,而不溢出内存?
...全文
511 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
iambic 2007-06-07
  • 打赏
  • 举报
回复
100M也溢出?
danielluo 2007-06-07
  • 打赏
  • 举报
回复
flush 不行吗?
anhy 2007-06-07
  • 打赏
  • 举报
回复
如果文件太大,最好边读边写
还则我 2007-06-06
  • 打赏
  • 举报
回复
谢谢大家,刚才我又找一了一下问题,我是从数据库里面读出大量数据然后写到文件中去的.
内存溢出的时候,出现在读数据的时候,

像这种情况怎么处理?有什么好的方案?
believefym 2007-06-06
  • 打赏
  • 举报
回复
public class LargeFile {

public static void main(String[] args) throws Exception ...{
long length = 0x8ffffff;
MappedByteBuffer out = new RandomAccessFile("G:\a.dat", "rw").getChannel()
.map(FileChannel.MapMode.READ_WRITE, 0, length);
for (int i = 0; i < length; i++)
out.put((byte) 'x');
System.out.println("Finished writing");
for (int i = 0; i < 10; i++)
System.out.println((char) out.get(i)); // read file
}
}
iwillrockyou 2007-06-06
  • 打赏
  • 举报
回复
帮顶~
eduyu 2007-06-06
  • 打赏
  • 举报
回复
up
believefym 2007-06-06
  • 打赏
  • 举报
回复
MappedByteBuffer out = new RandomAccessFile(file, "r")
.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
believefym 2007-06-06
  • 打赏
  • 举报
回复
long length=0xffffff;
String file = "G:\\a.dat";
MappedByteBuffer out = new RandomAccessFile(file, "r")
.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, length);
for (int i = 0; i < length; i++)
out.put((byte) 'x');
System.out.println("Finished writing");
for (int i = 0; i < 10; i++)
System.out.println((char) out.get(i)); // read file
吴恒 2007-06-06
  • 打赏
  • 举报
回复
用bufferredreader会溢出吗?楼主试试。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧