java学习(154):文件复制
生活随笔
收集整理的這篇文章主要介紹了
java学习(154):文件复制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import javax.imageio.IIOException;
import java.io.*;
import java.util.Date;//字符輸入流
public class FileManagerChar {public static void readCharFile(File file){FileReader fileReader=null;//文本輸入流if(file.exists()){try {fileReader = new FileReader( file );//基于目標存在的文本文檔輸出流char[] chs=new char[50];//字符零時緩沖區int count=0;//存儲實際讀取的字符數量while((count=fileReader.read(chs,0,chs.length))!=-1){String s=new String( chs,0,count );System.out.println( s );}}catch (IOException e){e.printStackTrace();}finally {try {fileReader.close();}catch(IOException e){e.printStackTrace();}}}}//使用文本緩沖流讀取文件public static void useBufferReader(File file){FileReader read=null;//基于文件的普通輸入流BufferedReader br=null;//基于某個reader建立的字符緩沖流if(file.exists()){try {read=new FileReader( file );//基于文件建立普通文本輸入流br=new BufferedReader( read );//基于某個read建立文本緩沖流char[] chs=new char[25];int count=0;while ((count=br.read(chs,0,chs.length))!=-1){String s=new String( chs,0,count );System.out.println( s );}}catch (IOException e){e.printStackTrace();}finally {try {br.close();read.close();System.out.println( "關閉成功" );}catch (IOException e){e.printStackTrace();}}}}//字節輸出流public static void binaryOutStream(String filePath){String str="start=E:\\BaiduNetdiskDownload\\baidu6\\1.mp4";byte[] bys=str.getBytes();//將字符串轉換為字節數組OutputStream out=null;try {out = new FileOutputStream( filePath);out.write(bys);}catch (IOException e){e.printStackTrace();}finally {try {out.close();System.out.println( "資源關閉" );}catch (IOException e){e.printStackTrace();}}}//使用字節緩沖輸出流public static void useBufferedOutput(File file){OutputStream out=null;BufferedOutputStream bs=null;String str="日照香爐生紫煙,\n遙看瀑布掛前川。\n飛流直下三千尺,\n以適應河洛就停";byte[] bys=str.getBytes();if(file.exists()){try {System.out.println( file.getAbsolutePath() );out = new FileOutputStream( file.getAbsoluteFile()+"/李白詩.doc" );bs=new BufferedOutputStream( out );//基于某個outputstream建立緩沖輸出流bs.write( bys ,0,bys.length);//寫入目標文件}catch (IOException e){e.printStackTrace();}finally {try {bs.close();out.close();}catch (IOException e) {e.printStackTrace();}}}}//字符輸出流bufferwrite//file文件存儲的目錄//filename 文件名稱//content 文件內容public static void useBufferedWriter(File fir,String fileName,String content){File file=null;Writer writer=null;BufferedWriter bw=null;if(fir.exists()){file=new File(fir,fileName );char chs[]=content.toCharArray();try {writer=new FileWriter( file );bw=new BufferedWriter( writer );//基于Writer實例創建字符緩沖流bw.write(chs);//將char型數組所有內容寫入到目標文件中}catch (IOException e){e.printStackTrace();}finally {try {bw.close();writer.close();}catch (Exception e){e.printStackTrace();}}}else{//創建目錄后寫入內容System.out.println( "目標文件目錄沒找到" );}}//文件復制public static void copyFile(File target,File dir){InputStream in=null;OutputStream out=null;File copyFile=null;//目標寫的文件對象if(target.exists()){//判斷目標文件是否存在if(!dir.exists()){dir.mkdirs();//如果目標文件不存在,創建目錄}try {in = new FileInputStream( target );//基于文件建立輸入流String fileName=target.getName();//獲取文件源名稱//避免文件重名copyFile=new File(dir+"/"+new Date().getTime()+fileName);//基于目標寫入文件對象out=new FileOutputStream( copyFile );//基于目標文件建立輸出流byte[] bys=new byte[1024];//臨時存儲字節數據的緩沖區int count=0;//記錄讀取內容的臨時變量while ((count=in.read(bys,0,bys.length))!=-1){System.out.println( "文件賦值讀寫中,請稍后----" );out.write( bys,0,count );//將臨時緩沖區內容寫入到目標文件中}System.out.println( "目標賦值完成" );}catch (IOException e){e.printStackTrace();}finally {try {out.close();in.close();}catch (IOException e){e.printStackTrace();}}}}//emp序列化對象//target//序列化對象的目標文件//java序列化,將員工對象保存到文件中public static void javaSer(Employeee emp,File target){OutputStream out=null;ObjectOutputStream oos=null;//序列化輸出流if(emp!=null){try {out = new FileOutputStream( target );oos = new ObjectOutputStream( out );oos.writeObject( emp );//將序列化對象保存到文件中}catch (IOException e){e.printStackTrace();}finally {try {oos.close();out.close();}catch (Exception e){e.printStackTrace();}}}}//反序列化public static Employeee deser(File target) {InputStream in = null;ObjectInputStream ois = null;Employeee emp = null;if (target.exists()) {try {in = new FileInputStream( target );ois = new ObjectInputStream( in );//進行反序列化Object obj = ois.readObject();emp = obj != null ? (Employeee) obj : null;//如果不為空進行類型轉換} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}finally {try {ois.close();in.close();}catch (IOException e){e.printStackTrace();}}}return emp;//返回對象}
}
測試類
import java.io.File;public class test99 {public static void main(String[] args){File target=new File( "E:/files2/歌謠.docx" );File dir=new File( "d:/files/" );FileManagerChar.copyFile( target,dir);} }運行結果
總結
以上是生活随笔為你收集整理的java学习(154):文件复制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ceph 存储 对比_分布式存储系统 C
- 下一篇: 前端封装接口弹出错误_项目实践:Spri