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 pictures with Java

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the "Java how to achieve picture replication" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "Java how to achieve picture replication" article can help you solve the problem.

Public class TestBytesInputStream {public static void main (String [] args) {/ / copy a.jpg image byte [] datas=fileToBytesArray ("D:\\ a.jpg"); byteArrayToFile (datas, "D:\\ b.jpg");} / / 1. The picture is read into the byte array / / 1. Picture to program-FileInputStream// 2. Program to byte array-byteArrayOutputStream public static byte [] fileToBytesArray (String path) {/ / create source and destination File src = new File (path); byte [] dest = null;// select stream InputStream is = null;ByteArrayOutputStream baos = null;try {/ / you can also use is = new BufferedInputStream (FileInputStream (src)); is = new FileInputStream (src) You can also use baos = new BufferedOutputStream (teArrayOutputStream ()); baos = new ByteArrayOutputStream (); / / 3. Segmented read byte [] flush = new byte [1024 * 10]; / / buffer container int len =-1 position while ((len = is.read (flush))! =-1) {baos.write (flush, 0, len); / / write to byte array} baos.flush (); return baos.toByteArray () } catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {try {if (null! = is) {is.close ();}} catch (IOException e) {e.printStackTrace () } return null;} public static void byteArrayToFile (byte [] src,String path) {File dest=new File (path); InputStream is=null;OutputStream os=null;try {is= new ByteArrayInputStream (src); os= new FileOutputStream (dest); byte [] flush = new byte [5]; int len =-1sat while ((len = is.read (flush))! =-1) {os.write (flush, 0, len) }} catch (IOException e) {e.printStackTrace ();} finally {try {os.close ();}} catch (IOException e) {e.printStackTrace ();}

It is worth reminding that when is.read () does not pass in a parameter, the copied picture cannot be opened because it can only be read byte by byte, so it is best to pass in an array.

Two places to improve performance (1. The use of buffer containers (equivalent to pickup trucks) 2. You can also use byte buffered streams (equivalent to large trucks)

This is the end of the content about "how to copy pictures with Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Internet Technology

Wechat

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

12
Report