Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to compare any types of two files in Java?
I'm able to compare 2 text files, but I need to compare any type of files (like xls,doc,jpp...etc.)
I just need a Boolean result (for any type of files) telling if the files are the same or not.
You can first compare the files length, then you can compare files content byte by byte and return false as soon as a difference is found.
public static boolean sameContent (File f1, File f2) throws IOException {
if(f1.length()!=f2.length())return false;
FileInputStream fis1 = new FileInputStream(f1);
FileInputStream fis2 = new FileInputStream(f2);
try {
int byte1;
while((byte1 = fis1.read())!=-1) {
int byte2 = fis2.read();
if(byte1!=byte2)return false;
}
} finally {
fis1.close();
fis2.close();
}
return true;
}
One note about md5 comparaison (suggested in comments) :
Comparing md5 of the files is not reliable because md5 of 2 different files can be the same (if you are unlucky).
Computing md5 requires reading the whole file (+ hashing algorithm) and so is less efficient
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I've multiple List of Java-Datetimes with MyCustomDate object contains ( start DateTime and end DateTime ) LocalDateTime with hours and seconds.
I need to write function to check if There is Overlapping dates in my list return true \ false - with the best performance
can use java8 (stream functions)
You have to test all ranges aginst each other. There is no other way. Try this way, although is just one implementation, there are plenty of correct ways to implement it:
public boolean isThereOverlapingRanges(List<CustomDateRange> ranges) {
if (ranges.size() <= 1) {
return false;
}
boolean overlaping = false;
for(int i = 1; i < ranges.size(); i++) {
if (isDateInRange(ranges.get(0).start(), ranges.get(i)) ||
isDateInRange(ranges.get(0).end(), ranges.get(i))) {
overlaping = true;
break;
}
}
return overlaping || isThereOverlapingRanges(ranges.subList(1, ranges.size());
}
private boolean isDateInRange(DateTime date, CustomDateRange range) {
return date.after(range.start()) && date.before(range.end());
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What is the best way to write the following code in Java 8 in terms of for loop and filtering.
boolean flag = true;
List<Feed> availableFeeds = data.getAvailableFeeds();
for (Feed feedElement : availableFeeds) {
String type = feedElement.getType();
if ("MatchState".equals(type)) {
flag = false;
break;
}
}
boolean flag = data.getAvailableFeeds()
.stream()
.map(Feed::getType)
.noneMatch("MatchState"::equals)
The first line creates a stream out of the list. The second one maps each Feed to type by calling getType. The last one returns true if there is no type that equals the string "MatchState".
You need filtering and a short break:
boolean flag = !availableFeeds.stream()
.map(Feed::getType)
.anyMatch(type -> "MatchState".equals(type));
or:
boolean flag = availableFeeds.stream()
.map(Feed::getType)
.allMatch(type -> !"MatchState".equals(type));
One way to do it is:
boolean flag = !data.getAvailableFeeds()
.stream()
.filter(Objects::nonNull)
.anyMatch(feed -> feed.getType().equals("MatchState"));
This example constructs a Stream<Feed> from the List<Feed> returned by getAvailableFeeds(), and then calls Stream<Feed>.anyMatch() which accepts a Predicate<Feed> as it's parameter, in this case the predicate is a feed where the feed is not null, and the feed's type returned by Feed.getType() equals "MatchState"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
For example if:
String str = "100101010";
Then following should happen:
a[0]=1
a[1]=0
a[2]=0
...
It should hold for large strings also (up to 64 characters).
One hint: find a chart for ASCII. http://www.asciitable.com/index/asciifull.gif The code for "0" is 48 and the code for "1" is 49. You can convert characters to numbers just by subtracting 48 from the ASCII value.
int value = str.charAt(0) - 48;
It's important to realize that a Java char is just an integer type just like int and byte. You can do math with them. With that idea in mind, you should be able figure out the rest yourself.
(This is technically a duplicate since I answered a similar question long ago, but I can't find it, so you get a freebie.)
public static int[] getAsArray(String value){
// not null ; not empty ; contains only 0 and 1
if(value == null || value.trim().length()<1 || !value.matches("[0-1]+")){
return new int[0];
}
//if it necessary !value.matches("[0-1]+" regex to validate
value = value.trim();
value = value.length()>64 ? value.substring(0,63) : value;// up to 64 characters
int[] valueAsInt = new int[value.trim().length()];
for (int i = 0 ;i<value.length();i++){
valueAsInt[i]=(int)value.toCharArray()[i]-48;
}
return valueAsInt;
}
also if it's possible use shor or byte type as it's enoght to store 0,1 and you consume less memory as with int
I don't know why you want to do this, but it is a simple problem.
public static void main (String[] args) {
String s = "111232";
String element[] = s.split("");
System.out.println(element[0]);
System.out.println(element[1]);
System.out.println(element[2]);
System.out.println(element[3]);
System.out.println(element[4]);
System.out.println(element[5]);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was wondering if it was possible in Java to have a series of booleans such as:
boolean boo1, boo2, boo3, boo4, boo5;
I want to make it so that if one of these booleans become true than all others become false. I know this can be done with a series of long if statements but is there a simpler way to achieve this.
Thanks!
Using an array,
boolean bs = new boolean[10];
lightUp(bs, 6); // only bs[6] will be true after this
lightUp(bs, 0); // only bs[0] will be true after this
With the lightUp function defined as:
/**
* modifies the passed-in array to make sure only
* the selected index is set to true
*/
void lightUp(boolean[] bs, int index) {
for (int i=0; i<bs.length; i++) bs[i] = false;
bs[index] = true;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to develop a parser for a binary message exchange format i.e., a message parser which parses a binary message into an java object representation. I would like to ask what useful patterns could be used to implement a parser in a most flexible way. Could anybody describe this in a nutshell or provide resources to read?
Since youre trying to read binary data and transform it into Java Object, there are many approaches, but first thing first, you must know the structure/protocol of your binary.
The pattern I show you bellow is the style that I (if I were you) will use for this scenario.
Make sure you have an input stream that will stream out your binary data. If what you have is a byte array, Make a ByteArrayInputStream.
In your objects graph, each node/object should implement something like parseIn(InputStream s) method.
public class Parent extends ArrayList<Child> {
int age;
// ... more code here
public void parseIn(InputStream is) throws IOException {
// .. logic to read the stream into this instance.
DataInputStream dis = new DataInputStream(is);
this.age = dis.readInt();
// .. if necessary
Child c = new Child();
c.parseIn(InputStream is);
this.add(c);
}
// ... more code here
}
public class Child {
int height;
short weight;
Date birthdate;
public void parseIn(InputStream is) throws IOException {
// .. logic to read the stream into this instance.
DataInputStream dis = new DataInputStream(is);
height = dis.readInt();
weight = dis.readShort();
birthdate = new Date(dis.readLong());
}
}
So, when you obtain your stream, you simply
InputStream stream = this.getInputStream();
Parent p = new Parent();
parent.parseIn(stream);
And so on and so forth.
Some times, you need to read the underlying stream for some hint you need to read forward. For example when reading a string data in the binary stream. Either you keep reading byte-by-byte until you find a terminator byte (as of C's style 0 termination character). Or to provide the string length on the first byte and then read a byte array of that length.
I hope you get the Idea. And I hope it helps.