package com.lovo.test;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class FileStream {


public static void main(String[] args) {

InputStream in = null;
OutputStream out = null;

try {
in = new FileInputStream("f:/web78.rar");
out = new FileOutputStream("d:/web78.rar");

byte [] b = new byte[1024];
while((in.read(b)) != -1){
out.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} }}如果我以1024个字节的字节数组传输的话,文件的大小会因为它不是1024的倍数而发生改变。
想请问一下,如何让文件传输后依然能够不改变大小。

解决方案 »

  1.   

    package weeksummer;import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;public class ByteDemo { /**
     * @param args
     */
     public static void main(String[] args) {
            
            InputStream in = null;    
            OutputStream out = null;
            
            try {
                in = new FileInputStream("f:/1.jpg");
                out = new FileOutputStream("d:/1.jpg");
                int c=0;
                byte [] b = new byte[1024];
                while((c=in.read(b)) != -1){
                    out.write(b, 0, c);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }     } }
      

  2.   

    在fedora14下用eclipse运行楼主代码,当读入源文件为此程序源码文件FileStream.java时,新生成的文件与FileStream.java内容一致,可以用文本编辑器打开阅读。但若新建一个文本文件new.txt作为程序读入的源文件时,文本编辑器显示无法识别字符编码。同样的程序在windows7下运行的时候上面两种情况都能正常运行。
    求解释,求解决方案(还是用FileInputStream来读取,改用FileReader这种方式已试过,能解决这个问题)。
    源码贴出import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;public class FileStream {    
        
        
        public static void main(String[] args) {
            
            InputStream in = null;    
            OutputStream out = null;
            
            InputStream in1 = null;    
            OutputStream out1 = null;
            try {
                in = new FileInputStream("/home/wangjie/桌面/FileStream.java");
                out = new FileOutputStream("/home/wangjie/桌面/newFile");  //newFile能正常阅读
                
              byte [] b = new byte[1024];
                while((in.read(b)) != -1){
                    out.write(b);
                } 
                
                in1=new FileInputStream("/home/wangjie/桌面/test.txt");
                out1=new FileInputStream("/home/wangjie/桌面/newFile2.txt");
                  while((in1.read(b)) != -1){
                    out1.write(b);
                } 
                //test.txt文件内容为“hello”
               //newFile2内容无法阅读,文本编辑器显示无法识别编码
       
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                try {
                    in.close();
                    out.close();
                   in1.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }    }}
      

  3.   

    四楼  怎么会酱紫????
    坐等 WELL_MAN
      

  4.   

    in = new FileInputStream("f:/web78.rar");
    out = new FileOutputStream("d:/web78.rar");
                
    int temp = 0; //每个字节的进行复制
    while((temp = in.read()) != -1){
         out.write(temp);
    }
      

  5.   


    public static void main(String[] args) {
            
            InputStream in = null;    
            OutputStream out = null;
            
            try {
                in = new FileInputStream("f:/web78.rar");
                out = new FileOutputStream("d:/web78.rar");
                
                byte [] b = new byte[1024];
                int len;
                while((len=in.read(b,0,1024)) != -1){
                    out.write(b,0,len);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }    }
      

  6.   

    三楼 是数组的问题哦。
    你只有一个数组b[] 会导致混乱。
    Java codeimport java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;public class FileStream {    
        
        
        public static void main(String[] args) {
            
            InputStream in = null;    
            OutputStream out = null;
            
            InputStream in1 = null;    
            OutputStream out1 = null;
            try {
                in = new FileInputStream("/home/wangjie/桌面/FileStream.java");
                out = new FileOutputStream("/home/wangjie/桌面/newFile");  //newFile能正常阅读
                
              byte [] b = new byte[1024];//  这里只有一个数组导致混乱
                while((in.read(b)) != -1){
                    out.write(b);
                } 
                
                in1=new FileInputStream("/home/wangjie/桌面/test.txt");
                out1=new FileInputStream("/home/wangjie/桌面/newFile2.txt");
                  while((in1.read(b)) != -1){
                    out1.write(b);
                } 
                //test.txt文件内容为“hello”
               //newFile2内容无法阅读,文本编辑器显示无法识别编码
       
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                try {
                    in.close();
                    out.close();
                   in1.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }    }}
      

  7.   

    简单给你写成这样
    byte by [] = new byte[1024];
    int len;
    while((len=reader.read(by))!=-1){
      out.write(by,0,len);
    }  懂了?