求教关于java 字节流的问题

a8836246 2013-11-25 05:28:30
首先说说需要的功能,
对一个文件进行解析,需要每1024个字节作为一部分,
能够实现读取文件中任意第几个1024个字节的字符串。
对于1024字节的字符串以每个字节为一个元素存储为String []。

下面是我的实现算法

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

public class Filedecode {
public static void main(String[] args) {
//读取1.txt文件,每1024字节作为一部分,从第12个1024字节开始,一直读取8个。
readFile("D:\\1.txt",12,8);
}

public static ArrayList<String[]> readFile(String path, int startindex,
int length) {

ArrayList<String[]> list = null;
try {
File file = new File(path);
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file));
list = new ArrayList<String[]>(length);
byte[] a = new byte[1024];
String[] str = new String[1024];
int count = -1;
int com = 0;
int end = startindex + length;
while ((count = in.read(a)) != -1 && com < end) {

if (startindex == com) {
//将字节数组转字符
str = StringTool.byteTohex(a);
list.add(str);
str = null;
com++;
startindex++;
} else {
com++;
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return list;

}

}



用我上面的方法实现有两个问题,

1. list中存放的字符不能太多,多了就会报内存溢出,

2. 也会出现性能问题,如果要读第1000个1024字节的字符串,
需要先把前面的999个都读一遍,当要读取的文件很大时,读取速度非常慢。


//求教各位有没有更好的算法实现我的功能,本人是新人积分不多,只有60分,都给大家。
...全文
296 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
a8836246 2013-11-28
  • 打赏
  • 举报
回复
引用 9 楼 sca4441479 的回复:
既然楼主的边读取边组装放List有问题,那就试试一次性把内容读到内存,在按需求处理吧 1,每次读取1024个字节,放到StringBuilder里,追加表示符"\n"、'#'什么的,你任意,只要保证不会出现在文本里的特殊字符就行 2,根据上面定义特殊字符用split分组,split的字符串数组的长度作为循环的次数 3,根据split后的每个字符串做处理,也就是用charAt得到每一个字符放入String数组 4,将每个String数组放入list
您好,您这样好像最后还是要放到list,只要放的东西一多同样会出现内存不足的问题
a8836246 2013-11-26
  • 打赏
  • 举报
回复
今天用skip试了一下,但当要跳过的字节数大于long限制时,便不行, 请问这个该如何解决?真是麻烦各位了。
a8836246 2013-11-26
  • 打赏
  • 举报
回复
引用 4 楼 u012265257 的回复:
好厉害啊,我看着都有点吃力,不是很懂
不能怪您,是我没有把问题描述清楚
a8836246 2013-11-26
  • 打赏
  • 举报
回复
引用 5 楼 u012265257 的回复:
[quote=引用 2 楼 a8836246 的回复:] 多谢提醒,应该能解决第2个问题。
我把你的程序复制下来运行后出现---Filedecode.java:31: 找不到符号 符号: 变量 StringTool 位置: 类 Filedecode str = StringTool.byteTohex(a); 这是为什么??[/quote] 你好这个方法是我自己写的,只是把字节转成字符
sca4441479 2013-11-26
  • 打赏
  • 举报
回复
既然楼主的边读取边组装放List有问题,那就试试一次性把内容读到内存,在按需求处理吧 1,每次读取1024个字节,放到StringBuilder里,追加表示符"\n"、'#'什么的,你任意,只要保证不会出现在文本里的特殊字符就行 2,根据上面定义特殊字符用split分组,split的字符串数组的长度作为循环的次数 3,根据split后的每个字符串做处理,也就是用charAt得到每一个字符放入String数组 4,将每个String数组放入list
小明再现江湖 2013-11-25
  • 打赏
  • 举报
回复
引用 2 楼 a8836246 的回复:
多谢提醒,应该能解决第2个问题。
我把你的程序复制下来运行后出现---Filedecode.java:31: 找不到符号 符号: 变量 StringTool 位置: 类 Filedecode str = StringTool.byteTohex(a); 这是为什么??
小明再现江湖 2013-11-25
  • 打赏
  • 举报
回复
好厉害啊,我看着都有点吃力,不是很懂
小明再现江湖 2013-11-25
  • 打赏
  • 举报
回复
引用 1 楼 huxiweng 的回复:
InputStream有个skip方法,就是忽略多少个字节。不要重复读。阅读下面的API skip public long skip(long n) throws IOException Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped. The skip method of InputStream creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method. Parameters: n - the number of bytes to be skipped. Returns: the actual number of bytes skipped. Throws: IOException - if an I/O error occurs.
好多帖子都能看到你,你怎么不帮我解决下问题,我发帖了《程序运行时总是显示有异常,求修改》
a8836246 2013-11-25
  • 打赏
  • 举报
回复
多谢提醒,应该能解决第2个问题。
teemai 2013-11-25
  • 打赏
  • 举报
回复
InputStream有个skip方法,就是忽略多少个字节。不要重复读。阅读下面的API skip public long skip(long n) throws IOException Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped. The skip method of InputStream creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method. Parameters: n - the number of bytes to be skipped. Returns: the actual number of bytes skipped. Throws: IOException - if an I/O error occurs.

62,614

社区成员

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

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