Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training Big Data and Hadoop Training Java Training  
 

Java lock file or folder.

Ads

 
 

Share on Google+Share on Google+

Steve
Java lock file or folder.
1 Answer(s)      4 years and 8 months ago
Posted in : Java Beginners

Is it possible to create a desktop application to lock or encrypt file or folder in windows by using java code?? if possible can provide some reference?? because i can't find it...

thanks.

Ads
View Answers

November 27, 2012 at 5:51 PM


Here is an example of file locking.

import java.io.*;
import java.nio.channels.*;

public class FileLockExample {
        public static void main(String[] args) throws Exception {
                try {
                        RandomAccessFile file = new RandomAccessFile("C:/file.txt", "rw");
                        FileChannel fileChannel = file.getChannel();
                        FileLock fileLock = fileChannel.tryLock();
                        if (fileLock != null) {
                                System.out.println("File is locked");
                                accessFile();
                        }
                } catch (Exception e) {
                }
        }

        public static void accessFile() {
                try {
                        String line = "";
                        BufferedReader br = new BufferedReader(
                                        new FileReader("C:/file.txt"));
                        while ((line = br.readLine()) != null) {
                                System.out.println(line);
                        }
                } catch (Exception e) {
                        System.out.println(e);
                }
        }
}

Ads









Related Tutorials/Questions & Answers:
Java lock file or folder.
Java lock file or folder.  Is it possible to create a desktop application to lock or encrypt file or folder in windows by using java code?? if possible can provide some reference?? because i can't find it... thanks
Java file lock
Java file lock In this section, you will learn how to lock a file. The behavior of the file lock is platform-dependent. The file lock is advisory on some platforms, means unless an application checks for a file lock
Advertisements
copy file from folder to folder - Java Beginners
copy file from folder to folder   my requirement is I need to copy xml files from one folder to another my source file contains the field filename.... Suppose in the source file, file name is goodfile1.xml source folder
Java lock
Java lock  Can a lock be acquired on a class
Java lock
Java lock  Can a lock be acquired on a class
Image is in same folder where the java file is located - Swing AWT
Image is in same folder where the java file is located  dear sir It is there in the same folder..means the photograph.jpg file is created in the same folder where my java file is present... wat I should do sir
How to upload and save a file in a folder - Java Beginners
How to upload and save a file in a folder  Hi How to upload and save a file in a folder? for example Agreement.jsp File: So when user browses the file and enters the submit button the respective file should
code for recently created file from a folder in java - Java Beginners
code for recently created file from a folder in java  how to get the recently created file from a folder in java?  Hi friend, Code...=""; try { File file = new File(dirname); if(file.isDirectory
Java object's lock
Java object's lock  What is an object's lock and which object's have locks
Java thread lock
Java thread lock  What happens when a thread cannot acquire a lock on an object
Folder in Java.
Folder in Java.  I have any problem how to make Folder in java program? Can Anybody help me in this part
how to download a file from a folder??
how to download a file from a folder??  i can upload any kind of files to a folder, and only the path of the file is saved into the database, now how a client can download the file from my folder. pls provide me the jsp code
Getting the modification date and time of file or folder in Java
C:\nisha>java GetDirectoryAndFileModifiedTime Enter file or directory name in proper format to get the modification date and time : myfile.txt File name : myfile.txt File modification date
Version of com.deftlabs>mongo-java-distributed-lock dependency
List of Version of com.deftlabs>mongo-java-distributed-lock dependency
Zipping folder - Java Beginners
to zip a user selected folder . this folder may contain any number of folder and sub folders. i saw an example in this site showing , how to zip a folder, i executed... solve this.. zipping folder and all of its files and sub folders. thanking
java client server program for playing video file(stored in folder in the same workspace) using swings
java client server program for playing video file(stored in folder in the same... a client server program to play a video file, when I run both client and server... file name"); l.setBounds(50, 30, 100, 50); getContentPane().add(l
java client server program for playing video file(stored in folder in the same workspace) using swings
java client server program for playing video file(stored in folder in the same... a client server program to play a video file, when I run both client and server... file name"); l.setBounds(50, 30, 100, 50); getContentPane().add(l
java client server program for playing video file(stored in folder in the same workspace) using swings
java client server program for playing video file(stored in folder in the same... a client server program to play a video file, when I run both client and server... file name"); l.setBounds(50, 30, 100, 50); getContentPane().add(l
java client server program for playing video file(stored in folder in the same workspace) using swings
java client server program for playing video file(stored in folder in the same... a client server program to play a video file, when I run both client and server... file name"); l.setBounds(50, 30, 100, 50); getContentPane().add(l
image save to folder in java
image save to folder in java  Hi, I am working with java. In my application i want to give facility to user to add and change image. I use open... folder and that path stored into database, for furhter retrivation. Please guide me
Zipping folder - Java Beginners
to zip a user selected folder . this folder may contain any number of folder and sub folders. i saw an example in this site showing , how to zip a folder, i executed... solve this.. zipping folder and all of its files and sub folders. thanking
java program to copy the contents of one folder to another folder
java program to copy the contents of one folder to another folder  java program to copy the contents of one folder to another folder   Have a look at the following link: Java Copy one folder to another
how to backup files and folder with Java
how to backup files and folder with Java  Does anyone know to create a backup with java?. backup files and folders from my computer to an external... filename, String... content) throws IOException{ File
write file in aplication folder with out giving full path - WebSevices
write file in aplication folder with out giving full path  i have a simple java class by the help of this java class i want to write file in application folder with out giving full path like c:/data/aa
how to upload a file of apk format in a folder from servlet
how to upload a file of apk format in a folder from servlet  How to upload a file of apk format to a folder from servlet, i am trying to do, its getting uploaded as .zip file and not apk file
How to delete file in java ?
How to delete file in java ? In this section we will learn how to write a program to delete a file in java. You can delete the file or folder, java File.... Now, here is the code to delete a file in java. import java.io.File; public
Maven Dependency mongo-java-distributed-lock >> 0.1.0
You should include the dependency code given in this page to add Maven Dependency of com.deftlabs >> mongo-java-distributed-lock version0.1.0 in your project
Maven Dependency mongo-java-distributed-lock >> 0.1.1
You should include the dependency code given in this page to add Maven Dependency of com.deftlabs >> mongo-java-distributed-lock version0.1.1 in your project
Maven Dependency mongo-java-distributed-lock >> 0.1.4
You should include the dependency code given in this page to add Maven Dependency of com.deftlabs >> mongo-java-distributed-lock version0.1.4 in your project
Maven Dependency mongo-java-distributed-lock >> 0.1.6
You should include the dependency code given in this page to add Maven Dependency of com.deftlabs >> mongo-java-distributed-lock version0.1.6 in your project
How to make one waiting thread for lock - Java Beginners
to java. Can u explain about thread concept.How i know one thread is locked, and how can i force or acquire another thread for lock . Finally how to know thread is released from lock. Explain types of lock(like method level and block level