我做了一个读取文本文件的程序,代码如下:
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[65536];
int len;
while ((len = bis.read(b)) != -1) {
str = new String(b, 0, len, "GBK"); }
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我定义了byte[65536]的动态数组,现在要读入的文件要大于65536,而且大小不固定,
我该如何分配byte?java不支持动态数组,有其他方法可以实现吗?谢谢
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[65536];
int len;
while ((len = bis.read(b)) != -1) {
str = new String(b, 0, len, "GBK"); }
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我定义了byte[65536]的动态数组,现在要读入的文件要大于65536,而且大小不固定,
我该如何分配byte?java不支持动态数组,有其他方法可以实现吗?谢谢
解决方案 »
- 求助JBOSS4.2.3下JMS简单例子问题。
- javadoc生成Api时:com.sun.tools.javadoc.ClassDocImpl
- 老师要求做一个日期查询,不知道有没错误。
- 一个java程序,大家帮忙下.在线等待
- 请教 grid 布局遇到的问题
- 一个字符串是null和是""有什么区别?
- 小女子有难!请各位兄堂出手相救!
- 一个好好玩的东西
- mssql的JDBC那有得下,听说微软站上的那个只能连自己的SQL,有没有完全版的呀,在哪可以下?
- Applet里引用swing组件会有问题吗?
- short s1 = 1;s1 = s1+1;是否有错误? short s1 = 1;s1 += 1;是否有错误?
- 学习JAVA WEB的 一般步骤是什么啊?我现在学了j2se 看网上说JDSC Spring Hibniter 还有好多,感觉比较乱,
你只要把读入的字符串相加就可以了。
而这个byte数组的长度可以固定,
你已经用了while循环,当循环结束,所有的内容就都读到了。
内容为12345678900987654321然后代码这样写:import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;/**
* @author bzwm
*
*/
public class ReadFile {
public static void main(String args[]) {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("test.txt")); byte[] b = new byte[10];
int len;
String str = "";
while ((len = bis.read(b)) != -1) {
str += new String(b, 0, len, "GBK");
}
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}