Get the App
SLTechnology News&Howtos  ›  Development  › 

How to copy a file to another directory with code

Shulou Source: shulou.com Published: 2022-06-03 05:55:06 09月17日 Update

This article mainly introduces "how to copy a file to another directory with code". In daily operation, I believe many people have doubts about how to copy a file to another directory with code. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to copy a file to another directory with code". Next, please follow the editor to study!

Public Map imageUpload (String path, String sensorNo,String presetNo, String channelNo)

Map map = new HashMap ()

/ / read the picture

File fileImage = new File (path); / / E:\ picture\ 1\ 20210409\ 66666.png

FileInputStream fis = null

Try {

Fis = new FileInputStream (fileImage); / / create an input stream object

} catch (FileNotFoundException E1) {

Log.error ("read file error {}", E1)

}

String fileDocment = null; / / write path

If (System.getProperty ("os.name"). ToLowerCase (). Contains ("windows")) {

BaseSysconfig baseSysconfig = baseSysconfigRepository.findByParamCode ("ElectronicfenceW")

FileDocment = baseSysconfig.getCurrentlyValue () + File.separator + "zhqb_electronicfence"

+ File.separator + sensorNo.replace ("", ") + File.separator

+ presetNo.replace ("", ") + File.separator

+ channelNo.replace (",")

} else {

/ / stitching upload path / datacenter/zhqb_electronicfence/ camera number / preset bit number / channel number / picture name

BaseSysconfig baseSysconfig = baseSysconfigRepository.findByParamCode ("ElectronicfenceL")

FileDocment = baseSysconfig+ File.separator

+ sensorNo.replace ("", ") + File.separator

+ presetNo.replace ("", ") + File.separator

+ channelNo.replace (",")

}

String filepath = fileDocment + File.separator + fileImage.getName ()

/ / write path

File fileParent = new File (fileDocment); / / New file directory

Create a path if if (! fileParent.exists ()) {/ / does not exist

FileParent.mkdirs ()

}

File fileElectronicFence = new File (filepath); / / directory to be written by filePath

FileOutputStream fos = null

If (! fileElectronicFence.exists ()) {

Try {

FileElectronicFence.createNewFile ()

Fos = new FileOutputStream (filepath)

Byte [] buf = new byte [1024]; / / amount of file data per read

Int len =-1

While ((len = (fis.read (buf)! =-1) {

Fos.write (buf)

Fos.flush ()

}

} catch (IOException e) {

Log.error ("create file error {}", e)

} finally {

Try {

Fos.flush ()

Fos.close ()

Fis.close ()

} catch (IOException e) {

Log.error ("close OutputStream fail {}", e)

}

}

}

Map.put ("status", "sucess")

Map.put ("path", filepath)

Return map

}

At this point, the study of "how to copy a file to another directory with code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Files directories code copies paths learning pictures more help utility next objects cameras data articles methods theories knowledge articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi macOS Microsoft Shulou Information