Java lock file or folder.

Ads
 

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.

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
Folder in Java.
Folder in Java.  I have any problem how to make Folder in java program? Can Anybody help me in this part
Advertisements
ModuleNotFoundError: No module named 'lock_file'
ModuleNotFoundError: No module named 'lock_file'  Hi, My Python... 'lock_file' How to remove the ModuleNotFoundError: No module named 'lock... have to install padas library. You can install lock_file python with following
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
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
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
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
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
files that are present in any folder , how to make it clickable downloadble file in php
files that are present in any folder , how to make it clickable downloadble file in php   files that are present in any folder , how to make it clickable downloadble file in php
Spring 3.2 MVC, Upload File in a specific folder
In this Spring 3.2 MVC tutorial, you will learn about uploading file in a specified folder
Version of com.deftlabs>mongo-java-distributed-lock dependency
List of Version of com.deftlabs>mongo-java-distributed-lock dependency
Create new file in java
in java File class representing the files and folder pathnames by location. With File class we create files and directories. Java File represents actual...Create new file in java We are going to discuss how to create new file in java
Java- Save Image into project folder in web application
Java- Save Image into project folder in web application  I have a requirement of saving the image into project folder .. which is a web application. I am not able to figure out how to find the absolute path of my folder which I
Video Tutorial: How to check if a folder exists in Java through a source code
How to check if a folder exists in Java through a source code Java is an all... facilities. Here is the video tutorial of "How to check if a folder exists in Java... can check if a folder exists or not in Java. A new Java project is created
How can store image in server folder when deployed with a .war file?
How can store image in server folder when deployed with a .war file?  Hi all; When I am using application.getRealPath() , it return null when deployed with a .war file and when I use hard code it shows syntax error or path
how to upload multiple files in jsp and saving the path in database and the file in folder
how to upload multiple files in jsp and saving the path in database and the file in folder  how to upload multiple files in jsp and saving the path in database and the file in folder I have created a form for the upload of files
Java Util Zip. Zip files/folders inside a folder, without zipping the original source folder.
Java Util Zip. Zip files/folders inside a folder, without zipping the original... srcFile, ZipOutputStream zip) throws Exception { File folder = new File... { File folder = new File(srcFolder); for (String fileName : folder.list
Java get Folder Size
Java get Folder Size      ... in Java?": folder.getName()-This method returns the folder name... of folder in bytes. long fileSizeByte=size.getFileSize(new File
String file in to word file in java
String file in to word file in java  How to convert a String format notepad file into Ms word file in java
calling a java file in java
calling a java file in java  how to call a another java file in java
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.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.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
Zip Folder Using Java
in this folder are zipped by using java. Here we will learn how to compress files... objects of File class. In first object we are pass the name of  the folder... Zipping a Folder      

Ads