How to copy files through character stream in java
This article will explain in detail how java realizes file copy through character stream. 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.
File copying through character stream
Only text files can be copied using character streams
/ * * copy files through character stream * * @ param sourcePath source file path * @ param targetPath destination file path * / public static void copyFileByReaderAndWriter (String sourcePath, String targetPath) {/ / source file path File source = new File (sourcePath); / / destination file path File target = new File (targetPath) / / cannot copy if (! source.exists ()) {return;} / / if the source file does not exist, create if (! target.getParentFile (). Exists ()) {target.getParentFile (). Mkdirs ();} FileReader in = null; FileWriter out = null if the destination file directory does not exist Try {/ / character input stream and character output stream in = new FileReader (source); out = new FileWriter (target); char [] c = new char [1024]; int temp = 0; / / read 1024 characters while ((temp = in.read (c))! =-1) {/ / output to file out.write (c, 0, temp) }} catch (IOException e) {e.printStackTrace ();} finally {/ / close stream try {if (in! = null) {in.close ();} if (out! = null) {out.close ();}} catch (IOException e) {e.printStackTrace () } this is the end of the article on "how java can copy files through character stream". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.