How to read all files in directory iteratively in Java? - java

I have a directory on network drive with million of files. If I try to read it with
folder.listFiles()
it will take a lot of time until resulting array will be filled with files.
I would like to receive files by one and printout a progress.
How can I do this in Java?

You might try with DirectoryStream:
Path dir = Paths.get("C:\\"); // directory to list
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry: stream) {
System.out.println(entry);
}
} catch (DirectoryIteratorException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
You can also make the DirectoryStream filter files for you if you need to: all you need to do is add a parameter to the Files.newDirectoryStream call:
DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{txt,png,jpg}");

You can use the NIO.2 API:
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("/path/to/directory"))) {
for (Path path : directoryStream) {
System.out.println(path.toString());
}
} catch (IOException ex) {}

Related

Spring boot uploading file error when try bigger file

I have this upload method:
try {
Files.createDirectories(filesPath);
} catch (IOException e1) {
e1.printStackTrace();
return null;
}
for (MultipartFile file : Arrays.asList(files)) {
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get(filesPath + File.separator + file.getOriginalFilename());
Files.write(path, bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
It work well, but when i try upload bigger file around 1,5GB i get this error:
Invalid string length
How can i fix it?
First you need to adjust two properties if you want to allow uploads of such big files.
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
Then it is better to use MultipartFile.getInputStream() to read the contents of the file.
You might also use IOUtils.copy() from Apache Commons IO to simplify your job.

file.delete() returning false even file is writable

I'am trying to copy a file to a directory and then deleting it, but file.delete() keeps returning false
Here is my code:
for (File file : list) {
if (!file.isDirectory()) {
try {
FileUtils.copyFileToDirectory(file, path);
file.setWritable(true);
System.out.println(file.delete());
if(file.exists()){
file.deleteOnExit();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
Few ideas that you can work it out.
If you want to delete file first close all the connections and streams. after that delete the file
Make sure you have the delete permissions for the file
Make sure you are in right directory. Try using absolute path for deleting the file, in case delete is not working. Path might not be correct for the file.
Use Files.delete. The delete(Path) method deletes the file or throws an exception if the deletion fails. For example, if the file does not exist a NoSuchFileException is thrown. You can catch the exception to determine why the delete failed as follows: See Oracle docs here
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}

Error while copying files from source to destination java

//original file
Path original = Paths.get("C:\\Users\\Laksahan\Desktop\\bg.jpg");
File f = new File("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
// new file
Path destination = Paths.get("C:\\Program Files\\Tour v0.1\\image\\"+f.getName());
try {
Files.copy(original, destination, LinkOption.NOFOLLOW_LINKS);
} catch (IOException x) {
x.printStackTrace();
}
i tried above method to copy files, it wont work and it prints this error
java.nio.file.NoSuchFileException: C:\Users\Laksahan\Desktop\bg.jpg -> C:\Program Files\Tour v0.1\image\bg.jpg
please help
Java 7's NIO will not create a folder if it doesn't exist when using Files.copy().
The best you can do is check for the folder and create it if it doesn't exist before you call copy.
Try
Path original = Paths.get("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
instead of
Path original = Paths.get("C:\\Users\\Laksahan\Desktop\\bg.jpg");
Create a folder programmatically for example -
Path from = Paths.get("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
Path to = Paths.get("C:\\Program Files\\Tour v0.1\\image\\");
Path destination;
File f = new File("C:\\Users\\Laksahan\\Desktop\\bg.jpg");
if (!Files.exists(to)) {
try {
Files.createDirectories(to);
} catch (IOException ioe) {
ioe.printStackTrace();
}
destination = Paths.get(to.toString() + "\\" + f.getName());
try {
Files.copy(from, destination, LinkOption.NOFOLLOW_LINKS);
} catch (FileAlreadyExistsException faee) {
faee.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

Lower cased properties in Java

Do anyone know if it is possible to have lower case letters in a property file in Java?
Currently when I have Example:
My_Conf=1234567
I get, when checking the property
My_Conf=null
in the code.
The code is suppose to be ran in Jboss servlet engine.
Best regards
Yes, it is definitely possible to have lower case characters in a .properties file. The problem might be that your code doesn't load the file.
For more information, have a look here: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.Reader%29
Try this for debugging purpose:
String filename = "example.properties";
Properties properties = new Properties();
BufferedInputStream stream = null;
try {
stream = new BufferedInputStream(new FileInputStream(filename));
try {
properties.load(stream);
String prop = properties.getProperty("My_Conf");
System.out.println(prop);
} catch (IOException e) {
System.out.println("IOException");
}finally{
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
}

A Problem while Retriving file from FTPS Server

I am working in an application development. On that application i am performing files store, retrieve and delete operations. For identifying the files on server i am using an index(a hash map file) file. every time when i perform upload operation i update "index" file and upload "index" file on server along with other uploading files.
For performing delete operation first i am retrieving the "index" file and based on the index i am deleting the files from server and after updating "index" file i again upload "index" file on server.
I am able to perform file uploading operation successfully but while performing delete operation, i am getting "java.io.EOFException" exception, when i am trying to retrieve "index" file.
i am writing following code to download "index" file from FTPS server
//download index file
if (service.retrFile("INDEX", "") == service.OK) {
try {
ObjectInputStream objIn = new ObjectInputStream(new FileInputStream("INDEX"));
try {
Map<String, FileData> filesUploaded = (HashMap<String, FileData>) objIn.readObject();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
objIn.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Where "service.ok" returns '0' if it is successfully connected to FTPS server
and "FileData" contains information about file(attributes).
Same code i am using while performing uploading operation. there it is working fine with no exception. but while performing delete operation when i am retrieving "index" file i am getting exception on the statement :
Map filesUploaded = (HashMap) objIn.readObject();
Exception is :
SEVERE: null
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2298)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2767)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:798)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at com.pixelvault.gui.DeleteServerFilesDialog.startDeleting(DeleteServerFilesDialog.java:447)
I have checked whether FTPS server connections are properly closed after performing corresponding operations.
I am not getting where i am doing wrong.
please give me your valuable suggestions. i thank to all your suggestions which will help me to overcome with this problem.
i am using org.apache.commons.net.ftp and "retrFile" is a method created by me for retrieving files from server.
Here is code for "retrFile"
FTPSClient ftp;
public int retrFile(String filename, String savePath) {
if (!connected) {
return ERR;
}
FileOutputStream fout = null;
InputStream bin = null;
try {
ftp.enterLocalPassiveMode();
fout = new FileOutputStream(savePath + filename);
bin = ftp.retrieveFileStream(filename);
if (bin == null) {
fout.close();
return ERR;
}
byte[] b = new byte[ftp.getBufferSize()];
int bytesRead = 0;
while ((bytesRead = bin.read(b, 0, b.length)) != -1) {
fout.write(b, 0, bytesRead);
}
ftp.completePendingCommand();
fout.close();
} catch (FTPConnectionClosedException ex) {
ex.printStackTrace();
connected = false;
return NOT_CONNECTED;
} catch (IOException ex) {
ex.printStackTrace();
return ERR;
} finally {
try {
fout.close();
} catch (IOException ex) {
ex.printStackTrace();
return ERR;
}
try {
if (bin != null) {
bin.close();
}
} catch (IOException ex) {
ex.printStackTrace();
return ERR;
}
}
return OK;
}
Are you sure the INDEX file is correctly downloaded?
It's present in the filesystem when application is closed?
What FTP lib are you using?. i only know commons.net from Apache and i not recognice the "retrFile" file method. Could it be threaded so that the file is not completely downloaded when the readObject statement is executed?

Categories

Resources