How can one convert these 5 lines of Python code in Java?
-
def get_hash(name): readsize = 64 * 1024 with open(name, 'rb') as f: data = f.read(readsize) f.seek(-readsize, os.SEEK_END) data += f.read(readsize) return hashlib.md5(data).hexdigest() This API from http://thesubdb.com/api/ Could anybody please help me convert this into Java? I'm a beginner :'(
-
Answer:
You'll need to look at multiply things. First, read about Input Streams in Java : http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html The easiest thing to do is wrap BufferedReader around http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html that is: InputStream is = new BufferedInputStream(Files.newInputStream(Paths.get(name)) http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newInputStream%28java.nio.file.Path,%20java.nio.file.OpenOption...%29 Of course wrap it in try-with-resource block: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html Getting firsts 64k is easy, just be careful to remember stream shall read at least one byte, but not necesarily all of the buffer you supply. For last 64k just calculate starting position and skip to it. Files.size(Paths.get(name)) for size of the file and is.skip(n) for skipping n bytes. Read the documentation. Lastly there is a matter of calculating md5 hash. For that simple google search should yield enough answers. You'll generate a message digest, than go with update\update...\doFinal and after all is done converting to hex string is trivial.
Anonymous at Quora Visit the source
Related Q & A:
- How can I convert UTF-16 file to UTF-8?Best solution by Stack Overflow
- How can I convert Matlab code to c#?Best solution by Stack Overflow
- How can I convert the query from SQL to LINQ?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- how can I write this shell script in python?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.