0

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?

6
  • 1
    Why 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. Commented Apr 11, 2014 at 4: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<String> 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. Commented Apr 11, 2014 at 4: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? Commented Apr 11, 2014 at 4:10
  • 1
    Read this. Commented Apr 11, 2014 at 4:26
  • 1
    Read this too. Don't ignore these. Commented Apr 11, 2014 at 4:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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.
Can you post an example of the "machine language with a bunch of weird symbols"?
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ß
What is the URL you are trying to save from? Are you requesting an image? Have you tried pointing it at www.google.com?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.