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?
Asked
Active
Viewed 1,511 times
0

Gio
- 15
- 7
-
1Why do you expect printing out the `byte[]` to print out anything other than what it's printing out? It's a `byte` array after all. – Sotirios Delimanolis Apr 11 '14 at 04:05
-
@SotiriosDelimanolis Well what I am trying to do is use the contents of `data` in 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 static `Vector
getSourceUrls(byte[] data) throws IOException, URISyntaxException` where 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. – Gio Apr 11 '14 at 04:09 -
It doesn't matter what you're trying to do. You have some expectations. Ask your self why do you have them? It seems like you are expecting to use a `byte[]` as a `String`. Do you see what's wrong with that? – Sotirios Delimanolis Apr 11 '14 at 04:10
-
1Read [this](http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string). – Sotirios Delimanolis Apr 11 '14 at 04:26
-
1Read [this](http://stackoverflow.com/questions/7060016/why-does-the-tostring-method-in-java-not-seem-to-work) too. Don't ignore these. – Sotirios Delimanolis Apr 11 '14 at 04:32
-
@SotiriosDelimanolis Ohhh I see exactly what you were talking about now. These links are incredibly helpful. Thanks so much for your time. I really appreciate it – Gio Apr 11 '14 at 04:34
1 Answers
1
"[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.

Jason
- 11,744
- 3
- 42
- 46
-
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. – Gio Apr 11 '14 at 04:18
-
Can you post an example of the "machine language with a bunch of weird symbols"? – Jason Apr 11 '14 at 04:21
-
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ß – Gio Apr 11 '14 at 04:23
-
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 Apr 11 '14 at 04:24
-
The URL I am trying to save from is my professor's homepage: which ends with /sample.html so it's basically http://www.example.edu/projects/sample.html. In this function I am downloading the entire webpage into a directory and trying to save it into a byte array as well, but I think I am running into an issue saving it into the byte array. If you need the actual URL I can provide that. – Gio Apr 11 '14 at 04:28
-
As @SotiriosDelimanolis has linked, try System.out.println(Arrays.toString(data)); and see what that outputs. – Jason Apr 11 '14 at 04:37
-
Ah yes, as I am reading over the links I tried that and now I believe I am receiving the actual bytes. The output is a bunch of numbers separated by commas i.e. [73, 68, 51, 2, 0, 0, 0, 0, 16, 62, 84, 84, 50, 0, 0, 13, etc – Gio Apr 11 '14 at 04:41
-
1Those 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. – Jason Apr 11 '14 at 04:44
-
When I try this, the correct result that I need appears for a split second, however, suddenly switches back to the strange symbols like ?$???cb@?=j?k??q??1?®?Q ?????PFI?Om?èy1??34?/?%}?A??????^? – Gio Apr 11 '14 at 04:51
-
I am not sure if it is Eclipse that is the issue here or not. I doubt it's that though. – Gio Apr 11 '14 at 04:51
-
Ah I guess it was an issue with the compiler. I finally got it to work. Thanks so much guys. You guys are life savers. – Gio Apr 11 '14 at 04:54