How to test if a file is open in Java? [duplicate] - java

This question already has answers here:
Check if file is already open
(8 answers)
Closed 4 years ago.
Is it somehow possible to test if a java.io.File is currently open in another process by any process ? I am using Java 7 and target platforms are Linux/Windows/Mac.

There is no easy way in Java to go about this that will work reliably across different platforms. Depending on what you're trying to do, you might be able to patch together a series of try/catch statements which ostensibly work for most cases on most file systems, but you can never have full certainty that the next situation it encounters won't throw it off.
If you do end up going this route you want to ensure that when there is any doubt, it fails fast and doesn't get into a situation where it thinks a file is not open when it really is. My advice would be to see if there is any way you can possibly work around having to do this check.

Related

How Java is Architectural neutral? [duplicate]

This question already has answers here:
What is the difference between "architecture-neutral" and "portable"?
(9 answers)
Closed 6 years ago.
I read that "Java is architectural neutral because it have the capacity to read the factor key of one processor into the factor of another processor."
Please explain me in detail the above statement?
Java was designed to support applications on networks.
To enable a Java application to execute anywhere on the network, the compiler generates an architecture-neutral object file format--the compiled code is executable on many processors, given the presence of the Java runtime system

Find hardcoded Strings in legacy application [duplicate]

This question already has answers here:
Extract all string from a java project
(5 answers)
Closed 8 years ago.
I inherited a Java-Application with many many hardcoded Strings that should have been localized. THe code-base is huge and I need an overview about all the used hardcoded String values in the code. Is there a tool (or an IDE-function; I use Eclipse and IntelliJ IDEA) that extracts all strings from Java-classes?
With Eclipse you can use the menu option Source > Externalize Strings...
This only works on a single file at a time however.
I would recomend you to run your code through SonarCube. It's great tool that gives you a good overview the projects technical debt.

Eject USB drive from Java [duplicate]

This question already has answers here:
How Do I Eject a Volume in Java?
(2 answers)
Closed 3 years ago.
I know that questions like this have been asked before, but I couldn't find any more recent ones and I have a twist to my question.
I've developed an application in Java that is designed to run on removable media, and work on both Windows and Mac. I would like to add a button to safely remove/eject the device, if it is supported (i.e a USB drive). Is there a command line for each operating system that would allow me to do this?
I know that it can be done by an application running on the device to be ejected, because I've seen one that does it, but obviously I understand there are certain limitations to Java.
Thanks in advance
This is something that you will have to do by invoking an auxiliary application. These applications are not platform independent as you wish. So, to do that, find out which OS you are on by using System.getProperty("os.name") and invoke the appropriate command for the detected OS. Invoking applications is done with Runtime.getRuntime().exec(). Search for the commands you need for each OS.

Finding the average CPU usage for Windows machine in Java [duplicate]

This question already has answers here:
How do I monitor the computer's CPU, memory, and disk usage in Java?
(12 answers)
Closed 9 years ago.
I have searched all over the internet for about two days now. I have found a lot about how to find the CPU usage using VB and anything in the .NET framework. I am not using the .NET framework so these are irrelevant, I feel.
What I really need is results similar to the ones shown in the "CPU Usage" section in a Windows Task Manager under the "Performance" tab. If I could find a way to mimic that calculation, then that is exactly what I am looking for. I need the percentage of CPU Usage.
Obviously, Java is not the ideal language for finding the CPU usage of a computer, but that is the language I am using and I have been able to get everything else I need about the computer except this.
I am using Windows 7 with Java version 7 update 21. Eclipse is my IDE.
This is my first time posting a question on here, so if I have left anything out or something is not clear, please ask and I will do my best to explain more of what I mean.
There are a couple of ways to do this; one is to run a command-line tool and parse the output. On Windows 7, you can run
c:\Windows\system32\typeperf "\processor(_total)\% processor time"
Try it -- it prints a few lines of sampled CPU load data.
Another way is to use OperatingSystemMXBean .

Installing DDL with Java [duplicate]

This question already has answers here:
Java - Storing SQL statements in an external file [closed]
(12 answers)
Closed 9 years ago.
I need to install a DDL, preferably saved in a file, into a DB using JDBC. I have seen the addBatch() and executeBatch() methods in 'Statement' API but they seem to require adding individual statements one at a time. I am trying to find a way to add a big bulk of statements (DDL) that I can store in a file and pass as a paramter into a java app that will then use JDBC to install.
I'm going to give an answer that I think will help you maintainability wise, but it may not be what you're looking for. Here are two libraries that help with database migrations:
http://www.liquibase.org/
http://flywaydb.org/
Not only will these help you execute DDL in a file, they bring lots of other features you don't want to have to reinvent. EG, they'll make sure they only get run once if they succeed.
If these aren't the right tool for the job, I think the comment #Alpesh Gediya made is what you're looking for.

Categories

Resources