如何将一个文件a.xml读到byte[]中?
谢谢。

---------------------------------------------

解决方案 »

  1.   

    如果dom4j实现不了,改用IO操作试试,先转成String再转成byte[]
    没做过
      

  2.   

    如何一次性读到byte[],如果文件有几百兆,怎么办?
      

  3.   

    只要内存够大,应该能放进byte[]里。
      

  4.   

    如果是500M的文件,new byte[?]时中括号里该写多大?
      

  5.   

    先转成String,然后用String.getBytes();
      

  6.   

    读入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;
    }
      

  7.   

    LZ用的是按字节读,读的方法很多的,可以按行读,按字符读,按指定长度读等等
    http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html
      

  8.   

    byte[] a=new byte[4611859200];是450M的字节,无法定义。
      

  9.   

    你可以以随机方式读取文件内容,每次少读点放到StringBuffer中,这样就可以解决你一次性定义byte[] a=new byte[4611859200];的问题,分而治之,最后你得到一个StringBuffer,然后转成String,再转成LZ要的byte[]
      

  10.   

    呵呵,我看lz是在做webservice吧,如果文件太大的,你存放在xml中,那加载到byte[]中,内存就要你的文件的容量,不建议这样做
    建议文件传输的时候把文件分割传输,这种才是正确的方法
      

  11.   

    不是吧,每次读多少个byte[],循环读,直到把大文件读完
      

  12.   

    几百M的XML文件?
    难道你想直接读入内存?你可以定义一个固定大小的byte数据,当接到一定量的时候就写入到文件中说明白点就好 像是通过Socket传送文件。代码可以在网上找一下,这方面的东西比较多
      

  13.   

    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();
    }
    }


      

  14.   

     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;
        }
      

  15.   

    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();
                }
            }
            
      

  16.   

    楼上的人很无聊啊,只要是他回的贴子就是把别人的前面写的代码Copy一份来骗分?
      

  17.   

    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();看具体场合,文件过大容易内存溢出。
      

  18.   

    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就是你要读取的了。
      

  19.   

    29楼的是没有问题的,得到的就是byte[]数组。