Michael Mutek

Greenhorn
+ Follow
since Jun 15, 2020
Cows and Likes
Cows
Total received
1
In last 30 days
1
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
19
Given in last 30 days
19
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Michael Mutek

@Campbell Ritchie:
Ah ok, got it - referring to you post before the your penultimate post.
@your last post: Sorry, will keep this from now on in mind ,whenever I am posting something
1 week ago

Dave Tolls wrote:

Michael Mutek wrote:
Does anyone have an idea why my list is apparently not storing the added elements correctly, but just overwrting itself with every new
element I want to add?



The first place I would check is how you are maintaining the full List in testStagingArea.
I'm taking a bit of a guess here, but I'm going to suggest this is a new List each time, and so will only ever contain the single StagingAreaItem when it is serialised.

Exactly how you ensure that List is correct before adding a new element is up to you, and likely depends on the system itself.



Yea, that was part of my problem - now it alll works fine in regard to functionality, but it is not the most str8 forward way - as said above, I will improve my
solution a bit in terms of having a more str8forward solution.
1 week ago

Campbell Ritchie wrote:. . . I did try running some straightforward code. . . .




Nice one - I am almost a little bit proud of myself that my solution (before have written here the posts) looks almost like your - only difference is that
you return a boolean in regard to the serialization(), while for me it is void...
My deserilaisation() also return a list (basically a kind of copy (in regard to the content)) of the original list and within the function where I have called
the deserilaisation(), I thehn add all its objects into my original list, then add the new file also into my original list and at the end serialize this original list
-> the .ser-file will contain (after the program has terminated) all objects.
I all works now, but likely is not str8forward.

I guess the reason you return a boolean in regard to your serialization() is reason which Paul Clapham has hinted me?
-> to be able to append objects to an exisiting .ser-file, I guess?
I do not see how it works, but will read the ObjectOutputStream documentation for that one, as you have recommended me to do - thanks so far:)
1 week ago

Paul Clapham wrote:

Michael Mutek wrote:Does anyone have an idea why my list is apparently not storing the added elements correctly, but just overwrting itself with every new element I want to add?



Yes. When you open a file for output, that's what it does. If there was anything there in a file of that name before, it's thrown away. Unless you use the FileOutputStream(File, boolean) constructor in which you can say you want to append to the file.
Ah nice one, I have not even known this constructor before - will have a look.
Since by now I have solved my problem and all works fine now, but there is quite a bit of laborious code in my solution, specifically 'helper-lists'.
With using FileOutputStream(File, boolean)' I might get rid of them (because my .ser-file will not be overwritten every time I call the add()_method),
which would lead to a nicer solution - will try it out.


You may also be having a problem because you don't close the file after you finish writing to it. Because you didn't do that, the last part of the output may not be flushed to wherever on disk the file is located.
Fixed that one in the meanwhile by using try-with-ressources-statements.

And some parts of your code are excessively complicated. In one method you pass a File object and you want to put that object into a FileOutputStream. So you should just do that. Converting the File object to a String and then back to a File is at best a waste of time. Probably it doesn't do any harm but why bother?

Yeah, there are up to now still many redundancies and it does not look too nice...
Will try to refactor a lot to get a nicer outcome.


I have to say that I didn't read most of that post. It looks like you're tryiing to build a workaround for problems which would be better addressed by just doing things in the straightforward (and correct) way.
At first, one must know how to do it str8-forward to do it str8-forward - seems I lack too many basics for solving this task in a more solid way.



Anyways, thanks for your input:)
1 week ago
Update:

I have tried now to serialize/deserialize a list-object, but somehow the different added elements are apparently not stored in the list
and my .ser-file gets overwritten whenever I run the program while the list.size stays always 1 and the new added element will overwrite
the older element - why isn't the older element stored, so that when I run twice the program,
I would have two element in the list, which I then would serialize and afterwards deserialize - here is my code so far - maybe you can spit my mistake:

Code of the add-function (shall add basically an element into the list)  :


Each element is of the type StagingAreaItem with the attributes 'filename' and 'hash'.
The 'List<StagingAreaItem> testStagingArea' was declared outside this method.


Code of my attempt to serialize the list into the .ser-file (=stagingAreaDir)



Code of my attempt to deserialize now the .ser-file, i.e. its contents:


Does anyone have an idea why my list is apparently not storing the added elements correctly, but just overwrting itself with every new
element I want to add?


Edit/Update 2:

I have kept thinking and thinking and came to the conclusion that my strategy to solve the problem is still not correct - I think the right strategy might be this one:

Step 1 (When adding the first file i.e. object)   :
The object will be added in my List.
Then this data structure, i.e. list will be serialized.

Step 2 (When adding now a second file i.e. a second object)  :
Before doing anything else,
at first I need to deserialize the .ser-file (which basically represents my list and contains its content).
Side question: Deserializing a .ser-file provides to you a sort of copy of the object which you have been serializing before?
In this case a kind of clone or our list which is an own object (I have tested it with debugger and it has its own object-ID in memory) - is this correct?


After I have deserialized the .ser-file I got basically a clone of my list which I just call now listClone.
This listClone contains all the elements from Step1 (when I have called my add-method the first time by starting the program
                                                                                 and adding 'add + filepath' as arguments') - so far only one object.
now I would
        a.) add this object from the listClone into my original list
        b.) add the second object (which is the argument when I run my program i.e. a representation of it) into my original list, too.

Finally I serialize the original list again (which now contains two elements/objects), before terminating the program.

This two steps I repeat as often as how many objects I would like to add into my staging area which is represented by the .ser-file
and the contents of the .ser-file are represented by my original list.


What you think about my solution ideas? Am I on to something or is it still very off?
Anyways, I will be offline til monday, which gives me hopefuly enough time to think about all of it further,
before I try/test it on mondays - I hope for the best:)
I will post on monday an update here - thanks so far.

cheers,
Michael
2 weeks ago

Mike Simmons wrote:

Campbell Ritchie wrote:

Michael Mutek wrote:. . . I cannot story multiple objects within my .ser-file, right?
How can this be done? . . .

I am afraid that, as far as I know, it can't be done. You can only serialise one object at a time in an XYZ.ser file.


Sure you can.  You can, for example, write a file with five objects in a row, simply by calling writeObject() five times with five different arguments, then closing the stream, and file.  Then when you read it, you call readObject() five times in a row.  Works just fine.

The problem, though, is that this requires you to know in advance how many objects will be in the file.  Often, you don't know that.  So there are several possible solutions:

1. Write N objects, then when deserializing, just read objects in a loop until you get an error from an end-of-file.  This is messy, and may be hard to distinguish between a normal end-of-file error, and other errors that indicate other problems.

2. Use writeInt() and readInt() to write and read the number of objects at the beginning of the file.  To write 5 objects, use writeInt(5), then use writeObject() 5 times. To read, use readInt(), then call readObject() that number of times.

3. Just put everything in a list or array, and write the list / array.
This is basically storing everything (and also every update (add or remove)) into a list-object (or array-object) and only then serialize this list/array-object?
If so, I like that best for know and will try that out.



4. Define a custom object to signal the end of file.  Write as many objects as you want, then write your EOF object. To read, just keep reading in a loop, and when you find an EOF object, you're done.

Option 3 is by far the easiest and most common (which is why it was already suggested).  Option 4 can be useful if you don't know in advance how many objects there will be, and you don't want to store them all in memory at once.  Option 2 can make sense as part of a custom serialization method.  And option 1 is really only done by accident.



Anyways, thanks for your suggestions - some of them sound fancy, but also pretty interesting.
Might come back to them in the future and try them out, as soon as I have solved my problems initially as common/simple or straightforward as possible,
which seems to be for me already at times quite an overwhelming challenge in itself xD
2 weeks ago

Campbell Ritchie wrote:

Michael Mutek wrote:. . . I know that the SHA-1 is outdated . . .

Does that matter, as long as you get a recognisable hash from it?

. . . I cannot story multiple objects within my .ser-file, right?
Actually thinking about my aimed usecase, security does not matter here indeed.
I just need basically distinctive, unique "IDs", i.e. hashes to avoid collisions...so yeah, thinking more about it, SHA-1 is here in general sufficient.

How can this be done? . . .

I am afraid that, as far as I know, it can't be done. You can only serialise one object at a time in an XYZ.ser file.
Yeah, I was thinking a lot yesterday about that, but could not wrap my head around it, but before going to sleep it hit me, that it makes sense that it cannot be done. As you said, I can serialize only one single  object at one time.
If I serialize later one more time, the now serialized object will just overwrite the content in my .ser-file - is this correct?
If so, then my solutionthought would be following:

When I want to represent a kind of staging are like in gitHHub (only simpler), then what I need at first is an object where I can store the current
working-directory which I want to add to my staging Area.
So basically for instance a object from the type List.
After that, I can serialize this list-object and store its contents in my .ser-file (= representation of my staging area).

Now, lets say I want to add to my staging area another file (like I have tried  - test2.txt-file).
At this point if I serialize it right away, then it would just overwrite the contents which are in my .ser-file - is this correct?
So what I need to do is to just add this new test2-txt-file into my list-object and then serialize my (updated) list-object again...
Would you agree with my solution idea or do I still get it wrong?


You can create a List<XYZ> and serialise that; most standard List implementations are serialisable. Its elements are rather like fields of the List, and fields of an object are serialised along with it. As long as those fields can be serialised at all.
Otherwise you would need multiple xyz.ser files; you can try adding date and time or hash to their names.

Oops, the next time I should make a habit of at first reading the whole post before starting to reply back right away D:
Anyways,
is this what you say basically the same what my solution idea says?
And yeah, I rather want to keep it as simple as possible and would therefore try your first slution-idea and skip for now the idea with
adding multiple .ser-files and somehow label them distinctively.




Thanks a lot for your reply - I feel that thanks to your replies I slowly get a better idea of this problem:)
2 weeks ago

Campbell Ritchie wrote:There is probably an FX class corresponding to JFileChooser. If you are already writing FX, try an FX class first. If you become proficient in FX, you may never need to use Swing. But that isn't either of the naughty things I thought I was doing.
Yes, you did use something almost identical to line 8. But it is not a case of the line being empty. When the buffered reader reaches the end of the file, it returns null from readLine(). That is how it signals completion of its task.
Oops, I actually meant this in my brain, but have for some unknown reason (maybe exhaustion^^) stated something which is wrong in my post above.
Yeah, clearly - the task there is to execute the while-loop as long as there is content in the file, i.e. til the end of the file.
A single iteration ends apparently when there is a 'new line' incoming - so after every line, a new iteration of the loop will begin.


You start by assigning line with whatever is read next, and you need additional () because != has a higher precedence than =. When you have assigned line, you test whether it is null. It there is an empty String, that suggests the enter key has been pressed twice, and readLine() will return a 0‑length String. You may wish to test for empty Strings in the body of the loop, or not.
Ah that is intersting.
Let's say we have one text-file with two paragraphs, where between thos two paragraphs two or more empty lines are existent, e.g.
 "Hello world!
 
 
 
  Bye"
In this case, reading from the file using the method would be a problem, since it would think that the file came to an end, after "Hello world!" ?


I think the subejct of your second thread is different enough from this one to merit a second thread.
Al right:)



Thanks so far!
regards
Michael:)
2 weeks ago

Campbell Ritchie wrote:Can we have that very simply. Are you trying to serialise multiple objects to the same Path?
Yeah, basically the goal is to serialize multiple objects into the same .ser-file.

That won't work; you will only retain the most recently serialised object.
What are you serialising, and why are you serialising anything?
I want to track for potential changes/modification of the contents inside the files.
I plan to track for changes by using a MerkleTree (I have already implemented the code).
If I understand it correct, the idea of a MerkleTree is that two hashes from two children will create throughout concatenating them (hash1 + hash2) and then hashing the concatenated hash throughout a SHA-Algorithm (I have already implemented one),
the hash from its parent.
I have simplified my problem to having only two files and one parent-directory for now - when it works, I will extend the problem.
See the attached image.


Why doesn't your object have the digest hash as a field? You can probably find ready‑made classes that will calculate an SHA256 or similar for you. You need to be circumspect about when you call that method, so you aren't storing anything with an out of date SHA.

I know that the SHA-1 is outdated + there are ready classes for it. But for this task, I want to use still the SHA-1 and implement it myself (already done and it works, as you can see in my image in the first post -> the file gets a hash).
The objects have an attribute hash, as well as an attribute name (=filename).
But the hash needs to be created based on the content of the file using my convertToHash-method.


If you are doing version control, why do you need a serialised object? Why can't you simply use text files? Much easier and less error‑prone; I heard that Oracle want to get rid of serialisation because it is error‑prone, anyway.
Yeah, but for this task, I need to use Serialization for the cvs.
It's not my call...
edit: Or maybe I understand the task wrong? What does this sentence mean in our context:
"so you have to serialize the information stored in the “staging area” and load
again before every following call."

Maybe I shall not serialize from the beginning, but:
1.) At first store the objects normally in the .ser-file (which reperesents the staging area).
2.) And afterwards serialize/deserialize this .ser-file?

Was it maybe meant like that or totally differently and I do not get it at all?

Why are you using new File in line 5? Why aren't you using try with resources? I can't see that output stream being closed anywhere, which means the file may be locked as write‑only.



@try-with-ressources-statements:
Have learned only yesterday about them - will update my code in this regard.
But this will not solve my issue that I cannot story multiple objects within my .ser-file, right?
How can this be done?

Thanks so far.
Regards
Michael
2 weeks ago
@Tom Holloway: Thanks for clearing that up and the historical insights:)

@Campbell Ritchie:
Hehe, I have learned only yesterday about 'try-with-ressources-statements'.
From my understanding this statements basically tell the following try-block that one ore more ressources are needed.
The needed ressources will be then closed automatically at the end -> no close() needed.
But i have used this already in my code-solution?

@ your quiz:
Line 3 - is this Java Swing? I worry that I am not able to comprehend what is happening there - I am gulity in not have been learning up to now Java Swing,
but learning at the moment JavaFX on the side - will start learning Java Swing later.

Line 8 - isn't this a smilar strategy I have choosen in my code above?
You declare a variable of type String and in Line 9 you initialize this variable with your "reader"-object.
Not the reader always checks if the current line is not empty yet, and then put the current non-empty line as argument in your method below.

@ keeping everything is this thread:
I am sorry, but I have read your post too late - in the meanwhile I have already created a new thread...
I hope this is not a big problem - I will link the newly created Thread here and maybe they can be somehow merged?
https://coderanch.com/t/731937/java/store-deserialized-objects-List

regards
Michael


2 weeks ago
Hi!
I have a problem to solve, where I am very stuck
Hm, at best I describe shortly the goal, then what I have so far and finally the problem where I am stuck. At the very end I will share code-snippets, so that you can better understand it.

Goal
We have two text-files and we want to simulate/build a very simple codeversioning-environment (like gitHub, but very simple).
At first our program creates a folder with thow sub-folders.
In one of the subfolder ('staging') we want to store basically all files which we have added into a .ser-file.
This .ser-file is in this 'staging'-folder.
We want to serialize basically a kind of staging area (into the .ser-file) and then deserialize it by reading from this .ser-file.
When we read from it, we want to print to the console
          a.) the filename
          b.) the hash of the file (for this we have built a convertToHash-method which creating out of the file-content an unique hashcode).


What I have so far:
I am able to acomplish all of the above mentioned, but only with/for one file.
When I want do add another file, it will get obvious that the first file was never stored within a list, like intended, as well as not in the .ser-file.
The .ser-file will be just overwritten with the second added file, while the first added file is no more longer inside - thus only the second added file will be displayed.
Please see the attached picture which shows to you the console-output, as well as the above mentioned directories, so that you get a better idea of what I mean. Please be aware that this is the second added file, and the first file has disappeared.

My problem
It clearly seems to be that my list-storing does not work.
I just show you the relevant code of
    a.) the serialization-method:

I have created outside of any method (at the top of this class) an ArrayList from the type 'StagingAreaItem', which is a class that I have created.
This class ('StagingAreaItem') contains the both attributes 'name' and 'hash'.

Code of declaring the mentioned list:


The File 'stagingAreaDir' is the staging.ser-file which you can see in the attached picture and where everything, i.e. every file that
is added shall be stored as implemented (name and hash).


And here is my deserializing-code:



As said, it works for one file, but this first file will not be saved/stored in the .ser-file (= stagingAreaDir) and thus not in my list, since I write the list into this file.

I really have no idea how I can make it to work and would appreciate any help.
regards
Michael

2 weeks ago

Tim Holloway wrote:

Note for your own sanity's sake: Java has the convention that the "real", that is forward slash may be used as a filepath separator on all OS's, including Windows. So rather than this:

write this:

Because just one dropped backslash can ruin your whole day. Plus it helps if you ever need to run on a non-Windows OS, and that's no longer a rare occurrence. And may get rarer if Microsoft doesn't keep screwing up Windows 10.  



About this one:
Yeah, I have just copy/pasted the path lazily - anyways, do I understand it correctly that my syntax there "\\\\...\\\\.." is only working on Windows,
while the conventional one ("/../../..") is basically working on any OS?
So when I am using Windows, but someone would test my program on his Linux-OS it would work with the second conventional method, but not with my used one?

edit:
Continueing my own little task, I have faced another problem, which I am not able to solve.
I think it is best to start an own thread for it, which I will do - please let me in hindsight know, if this was the preferred action or if it would have been better to continue to use this thread? Thanks^^
2 weeks ago

Jj Roberts wrote:I'm not at my computer, so I can't help much. You are using the wrong class for reading text. Instead, look into the Reader classes, and maybe the Scanner class. The I/O learning trail from Oracle here is an excellent place to start.

Hope that helps for now.



Thanks a lot - I have checked the docu and have found something to make it work.
Also thanks a lot to all other replies.
Should have mentioned that if posssible, I need to use as a requirement java.io and not java.nio, as well as <= JDK version 10.

Finally this is how my code is looking now and it seems to work:


side-question:
Interestingly if I just print out the Stringbuilder 'content' instead of content.toString(),
I would get the same output - so I wonder what is the purpose of converting Stringbuilders into Strings before reading from a file, i.e. before outputting?
Is it just, that sometimes we want to have the contents in a file saved into a variavle of type String since we want to work further with it?

Like in my case here, I need the contents of a file within a String-variable, which I will pass on to a method which then will create for me a hash using SHA-1, based on the contents of the relvant file.

So is the only reason why we convert Stringbuilders usually into Strings at the end, the mentioned thought above (we need a string variable to proceed) or
are there other reasons behind?
2 weeks ago