java - A file equals content of website? -
i couldnt find out how compare content of specific file content of website.
this code used check, if equals:
private static boolean equals() { try { return new string(files.readallbytes(paths.get(filepath))).equals(getfile()); } catch (exception e) { return false; } } filepath:
private static final string filepath = "test.txt"; getfile():
private static string getfile() { try { url pageurl = new url(simpleurl); urlconnection uc = pageurl.openconnection(); stringbuilder text = new stringbuilder(); try (scanner scanner = new scanner(uc.getinputstream(), "utf-8")) { while (scanner.hasnextline()) { text.append(scanner.nextline()).append("\n"); } } return text.tostring(); } catch (exception ex) { return null; } } the method #equals() keeps returning false while content matches file.
you're unnecessarily massaging bytes characters , , hereby losing information contained in original bytes. usually, should transform bytes characters when interested in reading or manipulating bytes on per-character basis and absolutely understand how character encodings work. neither of seems case here. should reading , writing raw , unmodified bytes instead of transforming them characters.
to read inputstream url byte[] (instead of string), 1 of ways be:
bytearrayoutputstream output = new bytearrayoutputstream(); try (inputstream input = url.openstream()) { byte[] buffer = new byte[10240]; (int length = 0; (length = input.read(buffer)) > 0;) { output.write(buffer, 0, length)); } } byte[] contentfromurl = output.tobytearray(); (apache commons io , google guava have oneliner methods this)
note when intend save byte[] file, should merely doing follows without need massage them characters new string() or so:
files.write(path, contentfromurl); also note when intend save inputstream file without need intermediairy byte[], should doing in first place:
try (inputstream input = url.openstream()) { files.copy(input, path); } either way, end file containing exactly same bytes obtained. based on code, know can byte[] out of follows:
byte[] contentfromfile = files.readallbytes(path); if have content byte[] , want compare against byte[], should using arrays#equals() without massaging them strings:
arrays.equals(contentfromurl, contentfromfile); that's all. there no need explicitly read using character encoding of haven't confirmed http response been encoded in that encoding, , there no need swallow newlines , replace them fixed 1 of haven't confirmed http response using that newline character.
Comments
Post a Comment