Im having trouble with the files that I want to lock permanently. My program can lock the files but when I exit the program (the program is not running) the lock will be release. I want to lock files even the program is not running, is it possible? here's my code. Please help me
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import javax.swing.JOptionPane;
import static com.sun.nio.file.ExtendedOpenOption.NOSHARE_DELETE;
import static com.sun.nio.file.ExtendedOpenOption.NOSHARE_READ;
import static com.sun.nio.file.ExtendedOpenOption.NOSHARE_WRITE;
import java.nio.channels.FileLock;
public class LockFile {
public static void ProcessPackageFiles2(File Path) {
try {
File[] files = Path.listFiles();
for (File file : files) {
if (!file.isDirectory()) {
LockDown(file.getCanonicalPath());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void LockDown(String FilePath) {
try {
Path path = FileSystems.getDefault().getPath(FilePath);
FileChannel locks = FileChannel.open(path, NOSHARE_WRITE, NOSHARE_DELETE, NOSHARE_READ);
FileLock fileLock = locks.tryLock();
if (fileLock != null) {
System.out.println("Locked");
} else {
JOptionPane.showMessageDialog(null, "FAILED");
}
} catch (Exception ex) {
System.out.println(ex);
}
}
public static void main(String[] args) {
ProcessPackageFiles2(new File(path);
}
this is the result when I run the program. The file will be unreadable and
cannot be shared and deleted. But I want to extend it even the program is not
running. But the question is HOW
Related
I have got this class for loading blue images, which works fine in Eclipse but not in the exported jar. How can I access all the blue images in the folder (directory) called "blue" without knowing the names of the images?
public class Blue
{
public static void read() throws Exception
{
File directoryBlueImages = new File(
Blue.class.getResource("blue").getFile());
String[] blueImages = directoryBlueImages.list();
List<BufferedImage> blueImagesList = new ArrayList<>();
for (String blueImage : java.util.Objects.requireNonNull(blueImages))
{
blueImagesList.add(ImageIO
.read(Blue.class.getResourceAsStream("blue/" + blueImage)));
}
ApplicationImages.setBlueImages(blueImagesList);
}
}
UPDATE
I have tried this, but it does not work either. I am getting a NullPointer exception. I tried "/blue" and "blue" and even ".blue".
import java.awt.image.BufferedImage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import javax.imageio.ImageIO;
import vokabeltrainer.ApplicationImages;
public class Blue
{
public static void read() throws Exception
{
List<BufferedImage> blueImagesList = new ArrayList<>();
try (Stream<Path> pathStream = Files.walk(Paths.get(Blue.class
.getClassLoader().getResource("blue").toURI().toURL().getPath()))
.filter(Files::isRegularFile))
{
for (Path file : (Iterable<Path>) pathStream::iterator)
{
blueImagesList.add(ImageIO
.read(Blue.class.getResourceAsStream(file.toString())));
;
}
}
ApplicationImages.setBlueImages(blueImagesList);
}
}
I adapted an answer from How to list the files inside a JAR file?
First I distinguish wether I am running from jar or Eclipse:
try
{
Blue.readZip(); // when inside jar
}
catch (Exception e)
{
try
{
Blue.read(); // during development
}
catch (Exception e1)
{
System.out.println("Could not read blue.");
e1.printStackTrace();
}
}
Then class Blue looks like this:
public class Blue
{
private static List<BufferedImage> blueImagesList = new ArrayList<>();
public static void read() throws Exception
{
File directoryBlueImages = new File(
Blue.class.getResource("blue").getFile());
String[] blueImages = directoryBlueImages.list();
for (String blueImage : java.util.Objects.requireNonNull(blueImages))
{
blueImagesList.add(ImageIO
.read(Blue.class.getResourceAsStream("blue/" + blueImage)));
}
ApplicationImages.setBlueImages(blueImagesList);
}
public static void readZip() throws Exception
{
CodeSource src = Blue.class.getProtectionDomain().getCodeSource();
if (src != null)
{
URL jar = src.getLocation();
ZipFile zipFile = new ZipFile(jar.getFile());
ZipInputStream zip = new ZipInputStream(jar.openStream());
while (true)
{
ZipEntry ze = zip.getNextEntry();
if (ze == null)
break;
String name = ze.getName();
if (name.startsWith("vokabeltrainer/resources/blue/"))
{
blueImagesList.add(ImageIO.read(zipFile.getInputStream(ze)));
}
}
}
else
{
throw new IOException("can not find code source for blue images");
}
ApplicationImages.setBlueImages(blueImagesList);
}
}
I have the following code seen below, this code looks through a directory and then prints all of the different file names. Now my question is, how would I go about changing my code, so that it would also print out all of the content within the files which it finds/prints? As an example, lets say the code finds 3 files in the directory, then it would print out all the content within those 3 files.
import java.io.File;
import java.io.IOException;
public class EScan {
static String usernamePc = System.getProperty("user.name");
final static File foldersPc = new File("/Users/" + usernamePc + "/Library/Mail/V2");
public static void main(String[] args) throws IOException {
listFilesForFolder(foldersPc);
}
public static void listFilesForFolder(final File foldersPc) throws IOException {
for (final File fileEntry : foldersPc.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
}
I tested it before posting. it is working.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* #author EdwinAdeola
*/
public class TestPrintAllFiles {
public static void main(String[] args) {
//Accessing the folder path
File myFolder = new File("C:\\Intel");
File[] listOfFiles = myFolder.listFiles();
String fileName, line = null;
BufferedReader br;
//For each loop to print the content of each file
for (File eachFile : listOfFiles) {
if (eachFile.isFile()) {
try {
//System.out.println(eachFile.getName());
fileName = eachFile.getAbsolutePath();
br = new BufferedReader(new FileReader(fileName));
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(TestPrintAllFiles.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(TestPrintAllFiles.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
You may use Scanner to read the contents of the file
try {
Scanner sc = new Scanner(fileEntry);
while (sc.hasNextLine()) {
String s = sc.nextLine();
System.out.println(s);
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
You can try one more way if you find suitable :
package com.grs.stackOverFlow.pack10;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
public class EScan {
public static void main(String[] args) throws IOException {
File dir=new File("C:/your drive/");
List<File> files = Arrays.asList(dir.listFiles(f->f.isFile()));
//if you want you can filter files like f->f.getName().endsWtih(".csv")
for(File f: files){
List<String> lines = Files.readAllLines(f.toPath(),Charset.defaultCharset());
//processing line
lines.forEach(System.out::println);
}
}
}
Above code can me exploited in number of ways like processing line can be modified to add quotes around lines as below:
lines.stream().map(t-> "'" + t+"'").forEach(System.out::println);
Or print only error messages lines
lines.stream().filter(l->l.contains("error")).forEach(System.out::println);
Above codes and variations are tested.
I have requirement for monitoring the files copying status in a directory and the files are placed continuously into the directory in java.
I am planing to use Executor framework to find out individual files copy status and I have written below code but it is not working as expected, file without copy completion I am getting notification as copying got completed.
private boolean isFileCopied(String filePath) {
File file = new File(filePath);
Scanner scanner;
boolean isCopied = true;
while (true) {
try {
scanner = new Scanner(file);
isCopied = false;
} catch (FileNotFoundException e) {
System.out.println(filePath + " File is in copy State. ");
sleepFile();
}
if (isCopied == false) {
break;
}
}
System.out.println(filePath + " copy completed");
return isCopied;
}
private static void sleepFile() {
System.out.println("sleeping for 10 seconds");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Please someone help me out how can I find the exact status of a file like file "copy in progress" or "copying done" and how can I monitor each and every file copying status If bunch of large files placed in a directory.
I have used watcher API but it is not solving my purpose. even the file without copying got completed I am getting notification as copying got completed. Below are my code changes.
Using watcher service:
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class FolderWatchDemo {
public static void main(String[] args) {
final Path outputWatchFolderPath = Paths.get("/outputFolder/");
final Path sourceFolderPath = Paths.get("/sourceFolder/");
try {
//Registering outputWatchFolderPath
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get(outputWatchFolderPath.toString());
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
System.out.println("Watch Service registered for dir: " + dir.getFileName());
//copy files from inputfolder to o
for (final Path path: Files.newDirectoryStream(sourceFolderPath))
Files.copy(path, outputWatchFolderPath.resolve(path.getFileName()));
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
WatchEvent<Path> ev = (WatchEvent<Path>) event;
if (kind == ENTRY_CREATE) {
System.out.println("file got created !!");
}
if (kind == ENTRY_MODIFY) {
System.out.println("copying got completed !!");
}
if (kind == ENTRY_DELETE) {
System.out.println("file deleted successfully !!");
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
Thanks in Advance.
There is a very elegant way to create a Call Back system and implementing it via implementing ReadableByteChannel in order to monitor the progerss of copying files. Also the benefit is there is no need to monitor a directory. You can explicitly monitor the progress of the file which is being copied.
The main idea is proposed by this site, but changed it a little to fit in your problem:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
interface ProgressCallBack {
public void callback(CallbackByteChannel rbc, double progress);
}
public class Main{
public static void main(String[] args) {
ProgressCallBack progressCallBack = new ProgressCallBack() {
#Override
public void callback(CallbackByteChannel rbc, double progress) {
System.out.println(rbc.getReadSoFar());
System.out.println(progress);
}
};
try {
copy("SOURCE FILE PATH", "DESTINATION FILE PATH", progressCallBack);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy(String source, String destination, ProgressCallBack callBack) throws IOException {
FileOutputStream fos = null;
FileChannel sourceChannel = null;
try {
sourceChannel = new FileInputStream(new File(source)).getChannel();
ReadableByteChannel rbc = new CallbackByteChannel(sourceChannel, Files.size(Paths.get(source)), callBack);
fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(sourceChannel.isOpen()){
sourceChannel.close();
}
fos.close();
}
}
}
class CallbackByteChannel implements ReadableByteChannel {
ProgressCallBack delegate;
long size;
ReadableByteChannel rbc;
long sizeRead;
CallbackByteChannel(ReadableByteChannel rbc, long expectedSize, ProgressCallBack delegate) {
this.delegate = delegate;
this.size = expectedSize;
this.rbc = rbc;
}
public void close() throws IOException {
rbc.close();
}
public long getReadSoFar() {
return sizeRead;
}
public boolean isOpen() {
return rbc.isOpen();
}
public int read(ByteBuffer bb) throws IOException {
int n;
double progress;
if ((n = rbc.read(bb)) > 0) {
sizeRead += n;
progress = size > 0 ? (double) sizeRead / (double) size * 100.0 : -1.0;
delegate.callback(this, progress);
}
return n;
}
}
Hope this help.
package test;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class FolderWatchDemo {
public static void main(String[] args) {
final Path outputWatchFolderPath = Paths.get("/outputFolder/");
final Path sourceFolderPath = Paths.get("/sourceFolder/");
try {
// Registering outputWatchFolderPath
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get(outputWatchFolderPath.toString());
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
System.out.println("Watch Service registered for dir: " + dir.getFileName());
// copy files from inputfolder to o
for (final Path path : Files.newDirectoryStream(sourceFolderPath))
Files.copy(path, outputWatchFolderPath.resolve(path.getFileName()),
StandardCopyOption.REPLACE_EXISTING);
String fileName = null;
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
WatchEvent<Path> ev = (WatchEvent<Path>) event;
if (kind == ENTRY_CREATE) {
System.out.println("file got created !!" + ev.context());
}
if (kind == ENTRY_MODIFY) {
String fName = ev.context().getFileName().toString();
if (fileName != null && !fName.equals(fileName)) {
System.out.println("file copying completed !!" + fileName);
}
fileName = fName;
System.out.println("copying " + fName);
}
if (kind == ENTRY_DELETE) {
System.out.println("file deleted successfully !!");
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
I have two Java applications. One application will contain resource files and will be used as library to other Java application.
First app com.test.resourceusing.MainClass.java which contains res/base.xml resource file.
package com.test.resourceusing;
import java.io.BufferedInputStream;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class MainClass {
public MainClass() {
super();
}
public static void main(String[] args) {
MainClass main = new MainClass();
try {
main.start();
} catch (MalformedURLException e) {
}
}
public void start() throws MalformedURLException {
URL url = getClass().getResource("res/base.xml");
System.out.println(url.getPath());
System.out.println(url.getFile());
File f = new File(url.getFile());
if (f.exists()) {
System.out.println("File exist!");
BufferedInputStream result = (BufferedInputStream)
getClass().getResourceAsStream("res/base.xml");
Scanner scn = new Scanner(result);
while(scn.hasNext()){
System.out.println(scn.next());
}
} else {
System.out.println("Not working! :(");
}
}
}
Result is:
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
File exist!
<?xml
version='1.0'
encoding='utf-8'?>
<schema>
</schema>
Then I create .jar file which contains all resource files and try to use it as library in other application.
Second app:
resourcetest.MainClassTest.java
package resourcetest;
import com.test.resourceusing.MainClass;
import java.net.MalformedURLException;
public class MainClassTest {
public MainClassTest() {
super();
}
public static void main(String[] args) {
MainClass main = new MainClass();
try {
main.start();
} catch (MalformedURLException e) {
}
}
}
Result is:
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
Not working! :(
I don't understand why it's not working, is there problems in my code? Or this solution is not possible in Java?
Do you see the difference in the location of those files?
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
You cannot access a resource that is located in a JAR file with the File API.
Your code is already on the way. A simple edit should work:
public void start() throws IOException {
URL url = getClass().getResource("res/base.xml");
if (url != null) {
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println("File exist!");
try(InputStream result = url.openStream()) {
try(Scanner scn = new Scanner(result)) {
while(scn.hasNext()) {
System.out.println(scn.next());
}
}
}
} else {
System.out.println("Not working! :(");
}
}
After deeper searching looks like I found a reason - https://stackoverflow.com/a/10605316/1117515.
In this case I need to use getResourceAsStream().
I have made a program that continuously monitors a log file. But I don't know how to monitor multiple log files. This is what I did to monitor single file. What changes should I make in the following code so that it monitors multiple files also?
package com.read;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileWatcherTest {
public static void main(String args[]) {
final File fileName = new File("D:/logs/myFile.log");
// monitor a single file
TimerTask fileWatcherTask = new FileWatcher(fileName) {
long addFileLen = fileName.length();
FileChannel channel;
FileLock lock;
String a = "";
String b = "";
#Override
protected void onChange(File file) {
RandomAccessFile access = null;
try {
access = new RandomAccessFile(file, "rw");
channel = access.getChannel();
lock = channel.lock();
if (file.length() < addFileLen) {
access.seek(file.length());
} else {
access.seek(addFileLen);
}
} catch (Exception e) {
e.printStackTrace();
}
String line = null;
try {
while ((line = access.readLine()) != null) {
System.out.println(line);
}
addFileLen = file.length();
} catch (IOException ex) {
Logger.getLogger(FileWatcherTest.class.getName()).log(
Level.SEVERE, null, ex);
}
try {
lock.release();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // Close the file
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Timer timer = new Timer();
// repeat the check every second
timer.schedule(fileWatcherTask, new Date(), 1000);
}
}
package com.read;
import java.util.*;
import java.io.*;
public abstract class FileWatcher extends TimerTask {
private long timeStamp;
private File file;
static String s;
public FileWatcher(File file) {
this.file = file;
this.timeStamp = file.lastModified();
}
public final void run() {
long timeStamp = file.lastModified();
if (this.timeStamp != timeStamp) {
this.timeStamp = timeStamp;
onChange(file);
}
}
protected abstract void onChange(File file);
}
You should use threads. Here's a good tutorial:
http://docs.oracle.com/javase/tutorial/essential/concurrency/
You would do something like:
public class FileWatcherTest {
public static void main(String args[]) {
(new Thread(new FileWatcherRunnable("first.log"))).start();
(new Thread(new FileWatcherRunnable("second.log"))).start();
}
private static class FileWatcherRunnable implements Runnable {
private String logFilePath;
// you should inject the file path of the log file to watch
public FileWatcherRunnable(String logFilePath) {
this.logFilePath = logFilePath;
}
public void run() {
// your code from main goes in here
}
}
}