程序是这样的把D:/test/in.txt中的数据通过getIn(List<Byte> list, FileInputStream fin)方法放到一个list<Byte>的容器中
public class FileInputStreamDemo {
/**
* 用来读取文件数据
* 存入到list容器中
*/
public List<Byte> getIn(List<Byte> list, FileInputStream fin) {
try {
int temp=fin.read();
while (temp != -1) {
list.add((byte)temp);
}
/*
* while (fin.read() != -1) {
list.add((byte)fin.read());
*/
System.out.println(list);
} catch (IOException e1) {
e1.printStackTrace();
}finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(list);
return list;
} /**
* 执行数据的copy
*
* @param list
* @param fout
*/
public void toCopyStream(List<Byte> list, FileOutputStream fout) {
byte[] b = new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
b[i] = list.get(i);
}
try {
fout.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream("D:/test/in.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fout = new FileOutputStream("D:/test/out.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
List<Byte> list = new ArrayList<Byte>();
FileInputStreamDemo fisd = new FileInputStreamDemo();
fisd.getIn(list, fin);
System.out.println(list);
fisd.toCopyStream(list, fout);
}}错误是:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
但是当我把getIn(List<Byte> list, FileInputStream fin)中的
int temp=fin.read();
while (temp != -1) {
list.add((byte)temp);}
换成注释中的/*
* while (fin.read() != -1) {
list.add((byte)fin.read());
*/
时没有错误了,但是又出现了这种情况;当文件D:/test/in.txt中放aaa时又
list打印出来是[97,-1]跪求高手,我该如何改啊!!!!!!!!!
public class FileInputStreamDemo {
/**
* 用来读取文件数据
* 存入到list容器中
*/
public List<Byte> getIn(List<Byte> list, FileInputStream fin) {
try {
int temp=fin.read();
while (temp != -1) {
list.add((byte)temp);
}
/*
* while (fin.read() != -1) {
list.add((byte)fin.read());
*/
System.out.println(list);
} catch (IOException e1) {
e1.printStackTrace();
}finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(list);
return list;
} /**
* 执行数据的copy
*
* @param list
* @param fout
*/
public void toCopyStream(List<Byte> list, FileOutputStream fout) {
byte[] b = new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
b[i] = list.get(i);
}
try {
fout.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream("D:/test/in.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fout = new FileOutputStream("D:/test/out.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
List<Byte> list = new ArrayList<Byte>();
FileInputStreamDemo fisd = new FileInputStreamDemo();
fisd.getIn(list, fin);
System.out.println(list);
fisd.toCopyStream(list, fout);
}}错误是:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
但是当我把getIn(List<Byte> list, FileInputStream fin)中的
int temp=fin.read();
while (temp != -1) {
list.add((byte)temp);}
换成注释中的/*
* while (fin.read() != -1) {
list.add((byte)fin.read());
*/
时没有错误了,但是又出现了这种情况;当文件D:/test/in.txt中放aaa时又
list打印出来是[97,-1]跪求高手,我该如何改啊!!!!!!!!!
解决方案 »
- 请教同志们一个问题,如何在java中模拟SQL中的like?
- point类、circle类和cycliner类之间的继承问题
- java.awt.Robot发出组合键ctrl+z怎么实现
- 用java怎么实现LHA压缩!??代码和算法都行
- 谁能介绍几本好书看
- Jsp中的session问题
- java编写的软件里面能否置入一个flash控件?
- 在线求救!Apache的这个错误意思是什么?如何解决?谢谢!
- java多线程问题,请教大神,多谢
- DatagramSocket.receive无限阻塞,占用线程,导致一段时间过后线程全部被占用
- 新手问题,求助
- java文件jar运行在windows和linux下当前路径不一样
while (temp != -1) {
list.add((byte)temp);
}
你这句话,循环如何结束呢?最终必然是list用完所有可用内存然后挂掉。
while (t != -1) {
list.add((byte)t);
t=fin.read();
}
我刚才改出来了!
高兴啊
结贴le.