如何将文件读到byte[]中? 如何将一个文件a.xml读到byte[]中?谢谢。--------------------------------------------- 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果dom4j实现不了,改用IO操作试试,先转成String再转成byte[]没做过 如何一次性读到byte[],如果文件有几百兆,怎么办? 只要内存够大,应该能放进byte[]里。 如果是500M的文件,new byte[?]时中括号里该写多大? 先转成String,然后用String.getBytes(); 读入1M 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; } LZ用的是按字节读,读的方法很多的,可以按行读,按字符读,按指定长度读等等http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html byte[] a=new byte[4611859200];是450M的字节,无法定义。 你可以以随机方式读取文件内容,每次少读点放到StringBuffer中,这样就可以解决你一次性定义byte[] a=new byte[4611859200];的问题,分而治之,最后你得到一个StringBuffer,然后转成String,再转成LZ要的byte[] 呵呵,我看lz是在做webservice吧,如果文件太大的,你存放在xml中,那加载到byte[]中,内存就要你的文件的容量,不建议这样做建议文件传输的时候把文件分割传输,这种才是正确的方法 不是吧,每次读多少个byte[],循环读,直到把大文件读完 几百M的XML文件?难道你想直接读入内存?你可以定义一个固定大小的byte数据,当接到一定量的时候就写入到文件中说明白点就好 像是通过Socket传送文件。代码可以在网上找一下,这方面的东西比较多 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(); } } 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; } 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(); } } 楼上的人很无聊啊,只要是他回的贴子就是把别人的前面写的代码Copy一份来骗分? 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();看具体场合,文件过大容易内存溢出。 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就是你要读取的了。 29楼的是没有问题的,得到的就是byte[]数组。 java se java.library.path到底在哪里? 难道真没人能解决吗?! 这个程序为什么不会死锁,很郁闷!问谁能解答. 这个for语句怎么理解? 请问:file搜索怎样支持dos提示符*,有没有现成的方法可以?谢谢 读取数据时遇到的一个怪问题!!就高手进进看看吧!我已经找几个小时但还是没有结果!! 关于在linux用vi编写java后? 怎样中断程序? 我安装一个DRIVER的问题,分不是问题,我一贯大方 感觉java鼠标移动或者拖动事件得到的点太少了。 关于内部类一个小练习的疑惑
没做过
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;
}
http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
建议文件传输的时候把文件分割传输,这种才是正确的方法
难道你想直接读入内存?你可以定义一个固定大小的byte数据,当接到一定量的时候就写入到文件中说明白点就好 像是通过Socket传送文件。代码可以在网上找一下,这方面的东西比较多
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();
}
}
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;
}
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();
}
}
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();看具体场合,文件过大容易内存溢出。
{
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就是你要读取的了。