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的倍数而发生改变。
想请问一下,如何让文件传输后依然能够不改变大小。
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();
}
} } }
求解释,求解决方案(还是用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();
}
} }}
坐等 WELL_MAN
out = new FileOutputStream("d:/web78.rar");
int temp = 0; //每个字节的进行复制
while((temp = in.read()) != -1){
out.write(temp);
}
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();
}
} }
你只有一个数组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();
}
} }}
byte by [] = new byte[1024];
int len;
while((len=reader.read(by))!=-1){
out.write(by,0,len);
} 懂了?