In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use ByteArrayOutputStream to write data to a local file", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use ByteArrayOutputStream to write data to a local file" this article.
ByteArrayOutputStream writes data to a local file
To do an one-time verification in a project, you need to write the verified data into the form and upload it. Coincidentally, the server Down. As a novice intern, in order to test whether there is anything wrong with his code and output results, under the guidance of his colleagues, he chose to output the form to the local first.
So in Baidu crazy search for "output stream", "output file to local", "file stream", etc., after multi-party collection, exploration and processing, finally added a small piece of code
/ / Test-Import the form into the local file FileOutputStream fileOutputStream = null; try {fileOutputStream = new FileOutputStream ("C:\\ Users\\ Administrator.DESKTOP-SFAEOA8\\ Desktop\\ consistencyCheckCommodityRuleTemplate.xlsx"); fileOutputStream.write (byteArrayOutputStream.toByteArray ());} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () }
The following is part of the code for uploading Excel. Note the method of uploading the final file to dfs to output the desired results in the prepared file C:\\ Users\\ Administrator.DESKTOP-SFAEOA8\\ Desktop\\ consistencyCheckCommodityRuleTemplate.xlsx
Public void uploadExcel () {/ / get check data List consistencyCheck = getConsistencyCheck (); / / create template information DataModel dataModel = setDataModel ();. (omit ten thousand words here) / / create ExcelData ExcelData excelData = setExcelData () . (ten thousand words are also omitted here) / / create log data ConsistencyCheckLog consistencyCheckLog = setConsistencyCheckLog ();. (omit ten thousand words here) / / create excel ByteArrayOutputStream byteArrayOutputStream = createExcel (dataModel, excelData); / / Test-Import the form into the local file FileOutputStream fileOutputStream = null Try {fileOutputStream = new FileOutputStream ("C:\ Users\\ Administrator.DESKTOP-SFAEOA8\\ Desktop\\ consistencyCheckCommodityRuleTemplate.xlsx"); fileOutputStream.write (byteArrayOutputStream.toByteArray ());} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () } / / upload excel (note it first if you cannot upload it) / / uploadFileToDfs (consistencyCheckLog, byteArrayOutputStream);}
In this way, OK is added, only the part of the test is added, and the heart of cabbage is put down after getting the data
Let's get to know ByteArrayOutPutStream.
The ByteArrayOutputStream class creates a buffer of the byte type array inside the program when it creates its instance, and then writes or reads the byte type data to the array by using the instances of ByteArrayOutputStream and ByteArrayInputStream.
The byte array output stream creates a byte array buffer in memory, and all data sent to the output stream is stored in the byte array buffer
After you have successfully created a byte array output stream object, you can refer to the methods in the following list for writing or other operations on the stream
The ordinal method describes that 1public void reset () resets the count field of the output stream of this byte array to zero, discarding all data output that has been accumulated so far in the output stream. 2public byte [] toByteArray () creates a newly allocated byte array. The size of the array and the size of the current output stream, which is a copy of the current output stream. 3public String toString () converts the contents of the buffer to strings and bytes to characters based on the platform's default character encoding. 4public void write (int w) writes the specified bytes to this byte array output stream. 5public void write (byte [] b, int off, int len) writes the len bytes from the offset off in the specified byte array to this byte array output stream. 6public void writeTo (OutputStream outSt) writes the entire contents of the output stream of this byte array to the specified output stream parameters. When the table is output
Java output stream FileOutputStream also plays a vital role.
The process of writing a file using FileOutputStream is the same as using the FileInputStream process, first opening the local file with the File class, instantiating the input and output stream, then calling the read-write method of the stream to read or write data, and finally closing the stream.
Writing method of FileOutputStream
The FileOutputStream class provides a variety of file writing methods, which can write a single byte to the file, write an byte array to the file, or take part of the byte array data to write to the file.
Write the read result to ByteArrayOutputStream
FileOutputStream can write data to a file.
ByteArrayOutputStream can write in the data read from other places, and finally get all the byte of the data, which is equivalent to reading all the data into memory.
/ / write the read result to ByteByteArrayOutputStream public static void main (String [] args) throws IOException {/ / TODO Auto-generated method stub String s = "C:\\ a.txt"; int len = 0; FileInputStream stream = new FileInputStream (s); ByteArrayOutputStream stream2 = new ByteArrayOutputStream (); byte [] buffer = new byte [5]; / / read and write while ((len = stream.read (buffer)! =-1) {stream2.write (buffer, 0, len) } byte [] data = stream2.toByteArray (); System.out.println (new String (data));} above is all the content of the article "how to write data to a local file using ByteArrayOutputStream implementation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.