In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use byte stream to copy files in java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
I used to write it at school in order to prepare for a certificate exam. There is no technical content, mainly familiar with the operation of basic controls in j2se, as well as event binding, and so on.
Example:
Import java.io.*; import java.util.Scanner; / * java uses byte stream to copy image * @ author Administrator * @ time June 9, 2011 19:55:10 * / public class MyTest2 {private String filename; / / file name private double filesize; / / file size public static void main (String agrs []) {MyTest2 tt2=new MyTest2 () String frompath= "E:\\ qq.png"; String topath= "F:\\ qq.png"; if (tt2.CheckFile (frompath, topath)) {tt2.CopyFile (frompath, topath);}} / copy file public void CopyFile (String frompath,String topath) {File file1=new File (frompath); / / Source file path File file2=new File (topath) / / destination file path filename=file1.getName (); filesize= (file1.length ()) / 1024swap 1024; System.out.println ("* file attributes *"); System.out.println ("source file path:" + frompath) System.out.println ("destination file path:" + topath); System.out.println ("file name:" + filename); System.out.println ("file size:" + filesize+ "MB"); int ch=0; try {FileInputStream fin=new FileInputStream (file1); FileOutputStream fout=new FileOutputStream (file2); ch=fin.read () System.out.println ("start copying!") ; long startTime=System.currentTimeMillis (); / / get the start time while (chained color color 1) {fout.write (ch); ch=fin.read ();} long endTime=System.currentTimeMillis () / / get the end time System.out.println ("Program run time: + (endTime-startTime) +" ms "); System.out.println (" copy completed! ") ; / / close the stream fin.close (); fout.close ();} catch (Exception e) {System.err.println ("Error:" + e);}} / / verify whether the file exists public boolean CheckFile (String frompath,String topath) {File file1=new File (frompath) / / Source file path File file2=new File (topath); / / destination file path if (! file1.exists ()) {/ / the file does not exist System.out.println ("Source file does not exist, please check that the path is correct!") ; return false;} else {if (file2.exists ()) {System.out.println ("destination file already exists, please select overwrite / cancel?") ; System.out.println ("1: overwrite 2: cancel"); try {Scanner sc=new Scanner (System.in); int a=sc.nextInt () If (astat1) {System.out.println ("you entered 1, the operation will continue and the target file will be overwritten.") ; return true;} else if (aversion 2) {System.out.println ("you entered 2, the operation will be canceled.") ; return false;} else {System.out.println ("invalid input. ; "); CheckFile (frompath, topath); return false;}} catch (Exception ee) {System.out.println (" invalid input. ; "); CheckFile (frompath, topath); return false;} return false;}}
This is the end of this article on "how to use byte stream to copy files in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.