美文网首页
java io流实现复制文件

java io流实现复制文件

作者: Lawrence__ | 来源:发表于2017-12-14 09:13 被阅读0次

package com.fxt.model;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import org.apache.axis.encoding.Base64;

public class UtilFile {

public static String getFileByteString(File file) throws Exception{

InputStream in = new FileInputStream(file);

long length =  file.length();

byte[] bytes = new byte[(int)length];

int offset = 0;

int numRead = 0;

while(offset < bytes.length - offset && (numRead =in.read(bytes, offset, bytes.length - offset)) >=0){

offset += numRead;

}

if(offset < bytes.length){

System.out.println("不能完全读取文件__"+file.getName());

}

in.close();

String encodedFileString = Base64.encode(bytes);

return encodedFileString;

}

}

package com.fxt.model;

import java.io.File;

import java.io.FileOutputStream;

import org.apache.axis.encoding.Base64;

public class UntilFileClient {

public static void main(String[] args) throws Exception {

File f = new File("D:\\4.png");

String str = UtilFile.getFileByteString(f);

byte[] bytes = Base64.decode(str);

// 写入新文件

FileOutputStream out = new FileOutputStream("D:\\"+"wj2.jpg");

out.write(bytes);

out.flush();

out.close();

}

}

相关文章

网友评论

      本文标题:java io流实现复制文件

      本文链接:https://www.haomeiwen.com/subject/xwehwxtx.html