Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to copy files through character stream in java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report