I am trying to save a HTML resource in a file, as well as in a byte array. I have created a function to create a directory and a separate function called saveResource which will download an HTML file and store it in said directory. This part of the function works quite well and stores the correct html file corresponding to the URL that was inputted in the command line. However, I am having an issue storing this file into a byte array. The function returns a byte array of the contents of the resource specified by urlString. How can I write the function so that it also stores the HTML file in the byte array so that the array will have the correct contents?
1 Answer
"[B@d9438de" is the address (and type) of your byte array.
You should print out the actual bytes in the array, not the reference.
Edit: If you use a BufferedReader, you can read the HTML line by line into Strings and then you can perform any regular expression or indexOf searches that you want.
11 Comments
Gio
Would a
System.out.println(new String(data)) do that? Because when I try that, the output seems like machine language with a bunch of weird symbols. When I do System.out.println(new String(data, "UTF-8") a bunch of question marks and symbols appear.Jason
Can you post an example of the "machine language with a bunch of weird symbols"?
Gio
I am trying that out right now. In the meantime, here is the current output: o±x߲É∫w˛ˇé—ˆ/ˇ–wNˇ¡cÒ⁄>≈‚ü˙Èfl¯,o˛;GÿºSˇA›;ˇçˇ«h˚äË;߇±ø¯ÌbÒO˝tÔ¸7ˇ†Ï^)ˇ†ÓùˇÇ∆ˇ„¥}ã≈?Ù”øXfl¸vıxß
Jason
What is the URL you are trying to save from? Are you requesting an image? Have you tried pointing it at www.google.com?
Jason
Those look like the byte values shown as integers. If you want a String containing the characters that they represent, I would suggest new String(data, "UTF-8"). The '0' values look a little suspect though.
|
byte[]to print out anything other than what it's printing out? It's abytearray after all.datain my next function which will search through this HTML resource and pull out all src= tags and download all the images, audio, etc. For example, the name of the function is public staticVector<String> getSourceUrls(byte[] data) throws IOException, URISyntaxExceptionwhere byte[] data is a sequence of chars that might contain src (or SRC) URLs, initialized from the contents of a URL which is where I use the contents returned by the saveResource function.byte[]as aString. Do you see what's wrong with that?