如何将文件读到byte[]中?

sAS112UUUYY111 2010-02-20 04:03:21
如何将一个文件a.xml读到byte[]中?
谢谢。



---------------------------------------------
...全文
625 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
wind214 2010-02-25
  • 打赏
  • 举报
回复
29楼的是没有问题的,得到的就是byte[]数组。
sAS112UUUYY111 2010-02-24
  • 打赏
  • 举报
回复
这些是读取文件的方法。
focusforce 2010-02-23
  • 打赏
  • 举报
回复
public class UseByteArrayOutputStream
{
public static void main(String[] args) throws IOException
{
URL url = UseByteArrayOutputStream.class.getResource("a.xml");
File file = new File(url.getFile());
InputStream inPutStream = new FileInputStream(file);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int len;
byte[] b = new byte[1024];
while ((len = inPutStream.read(b)) != -1)
{
bytestream.write(b, 0, len);
}
byte[] bytes = bytestream.toByteArray();
bytestream.close();
System.out.println(new String(bytes));
}
}

类中bytes就是你要读取的了。
focusforce 2010-02-23
  • 打赏
  • 举报
回复
byte [] b ;
Reader is = new FileReader("a.xml");
BufferedReader br = new BufferedReader(is);
StringBuffer sb = new StringBuffer();
String s1 = null;
while ((s1 = br.readLine()) != null)
{
sb.append(s1);
}
br.close();
is.close();
b= sb.toString().getBytes();

看具体场合,文件过大容易内存溢出。
kyo19 2010-02-23
  • 打赏
  • 举报
回复
楼上的人很无聊啊,只要是他回的贴子就是把别人的前面写的代码Copy一份来骗分?
sAS112UUUYY111 2010-02-22
  • 打赏
  • 举报
回复
引用 17 楼 czmchen 的回复:
呵呵,我看lz是在做webservice吧,如果文件太大的,你存放在xml中,那加载到byte[]中,内存就要你的文件的容量,不建议这样做
建议文件传输的时候把文件分割传输,这种才是正确的方法


是的,但是传递过来的是一个对方给的文件,自己无法决定它的大小
happybelladavid 2010-02-22
  • 打赏
  • 举报
回复
if(client == null) return;

DataInputStream in = null;
DataOutputStream writer = null;

try{
//3、访问Socket对象的getInputStream方法取得客户端发送过来的数据流
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));

String fileName = in.readUTF(); //取得附带的文件名

if(filepath.endsWith("/") == false && filepath.endsWith("\\") == false){
filepath += "\\";
}
filepath += fileName;

//4、将数据流写到文件中
writer = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filepath)))));

int bufferSize = 8192;
byte[] buf = new byte[bufferSize];

int read = 0;
while((read=in.read(buf)) != -1){
writer.write(buf, 0, read);
}

writer.flush();
System.out.println("数据接收完毕");

}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
in.close();
writer.close();
client.close();
}catch(IOException e){
e.printStackTrace();
}
}

happybelladavid 2010-02-22
  • 打赏
  • 举报
回复
public byte[] inputStream(File file){
byte[] a=new byte[1048576];

FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
kyo19 2010-02-22
  • 打赏
  • 举报
回复


if(client == null) return;

DataInputStream in = null;
DataOutputStream writer = null;

try{
//3、访问Socket对象的getInputStream方法取得客户端发送过来的数据流
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));

String fileName = in.readUTF(); //取得附带的文件名

if(filepath.endsWith("/") == false && filepath.endsWith("\\") == false){
filepath += "\\";
}
filepath += fileName;

//4、将数据流写到文件中
writer = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filepath)))));

int bufferSize = 8192;
byte[] buf = new byte[bufferSize];

int read = 0;
while((read=in.read(buf)) != -1){
writer.write(buf, 0, read);
}

writer.flush();
System.out.println("数据接收完毕");

}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
in.close();
writer.close();
client.close();
}catch(IOException e){
e.printStackTrace();
}
}


kyo19 2010-02-22
  • 打赏
  • 举报
回复
几百M的XML文件?
难道你想直接读入内存?你可以定义一个固定大小的byte数据,当接到一定量的时候就写入到文件中

说明白点就好 像是通过Socket传送文件。代码可以在网上找一下,这方面的东西比较多
xiaozhu86 2010-02-22
  • 打赏
  • 举报
回复
xml?还500M的。。。这个。。。真。。。无语啊。。。
非典型射手 2010-02-22
  • 打赏
  • 举报
回复
ByteArrayInputStream
czmchen 2010-02-21
  • 打赏
  • 举报
回复
呵呵,我看lz是在做webservice吧,如果文件太大的,你存放在xml中,那加载到byte[]中,内存就要你的文件的容量,不建议这样做
建议文件传输的时候把文件分割传输,这种才是正确的方法
yishiwucheng 2010-02-21
  • 打赏
  • 举报
回复
这样比较浪费内存吧,妈的,还短。。。。。
liguang168 2010-02-21
  • 打赏
  • 举报
回复
new byte[int];


???
yayoho 2010-02-21
  • 打赏
  • 举报
回复
不是吧,每次读多少个byte[],循环读,直到把大文件读完
梦_枫 2010-02-21
  • 打赏
  • 举报
回复
这种做法不多吧。。。。。。。。。。。。。
如果要这么做的话,,可以用断点续传的方式试试。。。。
小贝壳666 2010-02-20
  • 打赏
  • 举报
回复
引用 12 楼 sas112uuuyy111 的回复:
byte[] a=new byte[4611859200];是450M的字节,无法定义。

你可以以随机方式读取文件内容,每次少读点放到StringBuffer中,这样就可以解决你一次性定义byte[] a=new byte[4611859200];的问题,分而治之,最后你得到一个StringBuffer,然后转成String,再转成LZ要的byte[]
liu4545533 2010-02-20
  • 打赏
  • 举报
回复
这样实现起来应该是挺困难,而且在实际应用中的意义也不大
sAS112UUUYY111 2010-02-20
  • 打赏
  • 举报
回复
byte[] a=new byte[4611859200];是450M的字节,无法定义。
加载更多回复(11)

62,624

社区成员

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

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