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.

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
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
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
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
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
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 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
How to extract a zip file to a folder in Linux?
How to extract a zip file to a folder in Linux? Step by step command with example to extract a zip to specified folder In this tutorial we are going... content of a zip file to a specified directory. If you have a zip file and you
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 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
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- 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
How to access the image file from project folder itself while writing to pdf using itext?
How to access the image file from project folder itself while writing to pdf... to search for the image in the domain folder of the glassfish location.But i want it to pick it up from the project folder itself.Because later i deploy the project
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
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
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
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      
Version of com.deftlabs>mongo-java-distributed-lock dependency
List of Version of com.deftlabs>mongo-java-distributed-lock dependency
java program to take the backup of folders and copy the folder in different location without deleting the existing one
java program to take the backup of folders and copy the folder in different location without deleting the existing one  java program to take... the existing one   Have a look at the following link: Java Copy one folder
create folder
create folder  what code we should write as a web application that it create an empty folder on client's desktop
Java sdk/java poi hssf code to creat excel with folder structure,( i,e folders, subfolders and childrens )
Java sdk/java poi hssf code to creat excel with folder structure,( i,e folders... in the excel file . To be precise, am working on Java sdk of business objecsts . i need some help in getting folder structure using this java . Folder
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

Ads