When I use
System.IO.File.WriteAllBytes("\\server\\tmp\\" + FileName, fileData);
It always seems to add "C:" to the beginning so it tries to save to c:\server\temp...
Is there a way around this?
Let’s consider a few different ways to convert a byte array to a file. To download the source code for this article, you can visit our GitHub repository. The BinaryWriter is simply a class that helps to store strings of different encodings as well as raw binary data into files or memory locations.
This article shows a few ways to save a byte [] into a file. For JDK 1.7 and above, the NIO Files.write is the simplest solution to save byte [] to a file. // bytes = byte [] Path path = Paths.get ( "/path/file" ); Files.write (path, bytes); FileOutputStream is the best alternative.
Let’s write a method to save a byte array using the BinaryWriter class: We pass the FileStream class into the BinaryWriter object and then call on the BinaryWriter to help us write the data into the stream. The FileStream can manipulate data in a class itself.
I believe this is because the double backslash isn't escaped.
Try this instead:
System.IO.File.WriteAllBytes(@"\\server\tmp\" + FileName, fileData);
Your current path evaluates to \server\tmp\...
which will default to c:\server\tmp\...
.
To make a UNC path, you'll need an extra escaped directory-separator:
System.IO.File.WriteAllBytes("\\\\server\\tmp\\" + FileName, fileData);
or you can use a string-literal instead:
System.IO.File.WriteAllBytes(@"\\server\tmp\" + FileName, fileData);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With