This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!

Usha Viswanathan

+ Follow
since Feb 22, 2019
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Usha Viswanathan

Campbell Ritchie wrote:

Usha Viswanathan wrote:. . . . . .

What makes you think that code causes the StaticClassTest class to be loaded? It loads the Class<StaticClassTest> class and creates an instance of it. Try Class.forName("StaticClassTest");

Whatever we call after the "Class." will be called right ? That is what I thought of.

I thought calling "StaticClassTest.class" itself will make my static block inside StaticClassTest to be called

Since it is static, you dont need to create an instance of a class

So wanted a reason why my sysout inside the static block is not running
2 weeks ago

Below is the sample code I am running

On executing the above class with main, my statement on previous class "Hello" is not printing
2 weeks ago

Campbell Ritchie wrote:By static block, you probably mean a static initialiser.You can't call it. A static initialiser is called once, when the class is loaded, and never again. It is no more possible explicitly to call it than it is to call a constructor on a extant object.


Even once also not getting called in my snippet of code. I am asking why is it so ?
2 weeks ago

I am writing junit test for a class which has a static block on it.

I have many test methods which is accessing the class using .class . This doesnt call the static block


What can be done if I want this static block to be executed ?
2 weeks ago

Campbell Ritchie wrote:Look in the Jaa™ Tutorials. That says,

. . . The copy fails if the target file exists, unless the REPLACE_EXISTING option is specified.

Directories can be copied. However, files inside the directory are not copied, so the new directory is empty even when the original directory contains files. . . .

You are going to have to find the individual files an copy them. Alternatively, unless the files are small, find out how to open a channel between the two directories. I have forgotten how to myself.

Target directory is empty and it does not have the same file name as the source directory

I tried iterating the files present in source directory and copied the same using Files.copy method but getting FileAlreadyExistingException

Could you please tell me how to proceed
1 month ago

How to copy multiple files from one folder to another,

Path path1 = Paths.get("C:\\Users\\320053202\\testdir1");
Path path2 = Paths.get("C:\\Users\\320053202\\testdir2");
Path copy = Files.copy(path1, path2);

I thought File copy will be same as the files inside a folder to be copied
I have used java.nio package

Please help me out
1 month ago

Dave Tolls wrote:As the docs for the renameTo method say:
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.

okay. what if I delete the destination file and rename the other file to the destination file name again ? Will this work ?

try {

File file = new File("tempFile.xml");

// root element
Element root = new Element("company");
Document document = new Document(root);

// employee element
Element employee = new Element("employee");
employee.setAttribute(new Attribute("id", "10"));

// firstname element
Element firstName = new Element("firstname");

// lastname element
Element lastname = new Element("lastname");

// email element
Element email = new Element("email");

// department elements
Element department = new Element("department");
department.setText("Human Resources");

Element dob = new Element("dob");



// create the xml file
// transform the DOM Object to an XML File
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
File newFile = new File("tempFile.xml.tmp");

FileOutputStream outputStream = new FileOutputStream(newFile);

try {
} catch (InterruptedException e) {

outputter.output(document, outputStream);

if (file.exists()) {

boolean delete = file.delete();
System.out.println("Delete status =" +delete);

boolean renameTo = newFile.renameTo(new File("tempFile.xml"));

} catch (IOException e1) {
// TODO Auto-generated catch block

Here both .xml and .xml.tmp has the same content. Now if I change some data, I will be doing it in the .xml.tmp file and I want that to be renamed back to .xml file.

Even if I delete the file if it exists and renaming it doesnt work
1 month ago

Tim Moores wrote:test1.txt exists, but test2.txt does not? Does the code have write privileges to the directory where test1.txt resides?

Yes. Both are in the same directory.
1 month ago
File oldFile = new File("test1.txt");
File newFile = new File("test2.txt");
boolean success = oldFile.renameTo(newFile);

I have these two files created. Always the result of the boolean becomes false.

Please let me know why is it so
1 month ago

Swastik Dey wrote:DB is always a better option, and preferably with proper transaction management.

Ya, will try that. Let me know if there is any way to handle shutdown gracefully while writing to XML
2 months ago

Liutauras Vilda wrote:As mentioned, if you open file upon login and only close (if at all) upon logout, the problem arise when program quits abruptly, that means you don't close file at all or you don't write at all depending how you programmed. Well, it has been mentioned earlier, we don't know what you are doing since we never saw the code.

You still can try such way:
1. User logs in -> open file -> write login info -> close file
2. User does something else -> open file -> append some extra info -> close file
3. ...
4. IF program quits abruptly, you still have the file with some content. Now whether it is enough for your next login or no - I don't know.

In the 4th point, if jvm gets crashed, even if the xml is half written, the contents gets erased fully and I am unable to get the content for the next login. Thats the problem I face.

Will try saving the contents in DB.
2 months ago

Liutauras Vilda wrote:

Usha Viswanathan wrote:Sometimes, if the Windows gets hung, user is performing hard shutdown and at this time, the data gets erased away.

Perhaps you are sort of writing file for an entire app-use lifetime and close file if ever just upon logout.

Why you don't use database to persist such information?

Will try doing that. Is there any other way like writing to tmp files will work out for this issue ?
2 months ago
Only during abrupt shutdown, contents gets erased.

During a normal shutdown, this doesnt happen.
2 months ago

Liutauras Vilda wrote:

Usha Viswanathan wrote:During the write process of xml file, when this shutdown happens, the xml file becomes blank

Cambell was asking, but you didn't explain yet, why and when file gets blank in the first place if the shutdown went abruptly?

Please explain, when file gets written with content and when the content gets deleted in normal conditions?

During the login of the application, xml file is written with some information like time in which the user is logged in. This information is used in the next login, where I show a prompt of last login time.

Similar to updating time, there are other writes performed on the XML.

Sometimes, if the Windows gets hung, user is performing hard shutdown and at this time, the data gets erased away.
2 months ago


XML is read initially to capture the last login details.

Even I have run a sample program dealing with File. That also gets blanked out if I start my program and terminate it immediately.

Please help me with some solution on how to deal with this
2 months ago