My question is that I want to send pdf files through web service with condition that only 1mb of files are taken from that folder containing many files.
Please help me to resolve this question.I am new to web service.
Ask me again if it not clear.
Thanks In Advance.
The following method will return a list of all the files whose total size is <= 1Mb
public List<File> getFilesList(){
File dirLoc = new File("C:\\Temp");
List<File> validFilesList = new ArrayList<File>();
File[] fileList;
final int fileSizeLimit = 1024000; // Bytes
try {
// select all the files whose size <= 1Mb
fileList = dirLoc.listFiles(new FilenameFilter() {
public boolean accept(final File dirLoc, final String fileName) {
return (new File(dirLoc + "\\" + fileName).length() <= fileSizeLimit);
}
});
long sizeCtr = fileSizeLimit;
for(File file : fileList){
if(file.length() <= sizeCtr){
validFilesList.add(file);
sizeCtr = sizeCtr - file.length();
if(sizeCtr <= 0){
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
validFilesList = new ArrayList<File>();
} finally {
fileList = null;
}
return validFilesList;
}
Well, I dont know if I have understood your requirements correctly and if this would help your problem but you can try this java solution for filtering the files from a directory.
You will get a list of files and then you can use the web-service specific code to send these files
File dirLoc = new File("C:\\California");
File[] fileList;
final int fileSize = 1024000;
try {
fileList = dirLoc.listFiles(new FilenameFilter() {
public boolean accept(final File dirLoc, final String fileName) {
return (new File(dirLoc+"\\"+fileName).length() > fileSize);
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
fileList = null;
}
This should work.
If you just require filenames, replace the File[] with String[] and .listFiles() with list()
I cannot say much about the performance though. For a small list of files it should work pretty fast.
I am not sure if this is what you want but you can pick the files and check their size by :
java.io.File file = new java.io.File("myfile.txt");
file.length();
File.length()Javadoc
Send files whose size is 1 Mb.
Related
I have a project structure like below:
Now, my problem statement is I have to iterate resources folder, and given a key, I have to find that specific folder and its files.
For that, I have written a below code with the recursive approach but I am not getting the output as intended:
public class ConfigFileReader {
public static void main(String[] args) throws Exception {
System.out.println("Print L");
String path = "C:\\...\\ConfigFileReader\\src\\resources\\";
//FileReader reader = new FileReader(path + "\\Encounter\\Encounter.properties");
//Properties p = new Properties();
//p.load(reader);
File[] files = new File(path).listFiles();
String resourceType = "Encounter";
System.out.println(navigateDirectoriesAndFindTheFile(resourceType, files));
}
public static String navigateDirectoriesAndFindTheFile(String inputResourceString, File[] files) {
String entirePathOfTheIntendedFile = "";
for (File file : files) {
if (file.isDirectory()) {
navigateDirectoriesAndFindTheFile(inputResourceString, file.listFiles());
System.out.println("Directory: " + file.getName());
if (file.getName().startsWith(inputResourceString)) {
entirePathOfTheIntendedFile = file.getPath();
}
} else {
System.out.print("Inside...");
entirePathOfTheIntendedFile = file.getPath();
}
}
return entirePathOfTheIntendedFile;
}
}
Output:
The output should return C:\....\Encounter\Encounter.properties as the path.
First of all, if it finds the string while traversing it should return the file inside that folder and without navigating the further part as well as what is the best way to iterate over suppose 1k files because every time I can't follow this method because it doesn't seem an effective way of doing it. So, how can I use an in-memory approach for this problem? Please guide me through it.
You will need to check the output of recursive call and pass that back when a match is found.
Always use File or Path to handle filenames.
Assuming that I've understood the logic of the search, try this which scans for files of form XXX\XXXyyyy
public class ConfigReader
{
public static void main(String[] args) throws Exception {
System.out.println("Print L");
File path = new File(args[0]).getAbsoluteFile();
String resourceType = "Encounter";
System.out.println(navigateDirectoriesAndFindTheFile(resourceType, path));
}
public static File navigateDirectoriesAndFindTheFile(String inputResourceString, File path) {
File[] files = path.listFiles();
File found = null;
for (int i = 0; found == null && files != null && i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
found = navigateDirectoriesAndFindTheFile(inputResourceString, file);
} else if (file.getName().startsWith(inputResourceString) && file.getParentFile().getName().equals(inputResourceString)) {
found = file;
}
}
return found;
}
}
If this is slow especially for 1K of files re-write with Files.walkFileTree which would be much faster than File.list() in recursion.
When I zip 500mb of html files, p7zip does it in a couple of seconds and the filesize is 7mb (Without any custom settings, just 7z a filename.7z /folder).
Thus I expected apache commons compress to also compress, using 7z, to a comparable size. This is however, not the case. Even though I enabled the max presets for apache commons compress 7z. The resulting file size is also huge, close to 100mb.
Do I do something wrong or do I need to tune my presets? I have read the apache commons compress wiki but have not found my answers.
Relevant code for the java implementation :
public static Path compress(String name, List<Path> files) throws IOException {
try (SevenZOutputFile out = new SevenZOutputFile(new File(name))) {
List<SevenZMethodConfiguration> methods = new ArrayList<>();
LZMA2Options lzma2Options = new LZMA2Options();
lzma2Options.setPreset(LZMA2Options.PRESET_MAX);
SevenZMethodConfiguration lzmaConfig =
new SevenZMethodConfiguration(SevenZMethod.LZMA, lzma2Options);
methods.add(lzmaConfig);
out.setContentMethods(methods);
for (Path file : files) {
addToArchiveCompression(out, file, ".");
}
}
return Paths.get(name);
}
private static void addToArchiveCompression(SevenZOutputFile out, Path file,
String dir) throws IOException {
String name = dir + File.separator + file.getFileName();
if (Files.isRegularFile(file)) {
SevenZArchiveEntry entry = out.createArchiveEntry(file.toFile(), name);
out.putArchiveEntry(entry);
FileInputStream in = new FileInputStream(file.toFile());
byte[] b = new byte[1024];
int count = 0;
while ((count = in.read(b)) > 0) {
out.write(b, 0, count);
}
out.closeArchiveEntry();
} else if (Files.isDirectory(file)) {
File[] children = file.toFile().listFiles();
if (children != null) {
for (File child : children) {
addToArchiveCompression(out, Paths.get(child.toURI()), name);
}
}
} else {
System.out.println(file.getFileName() + " is not supported");
}
}
Could you please try to remove these lines:
List<SevenZMethodConfiguration> methods = new ArrayList<>();
LZMA2Options lzma2Options = new LZMA2Options();
lzma2Options.setPreset(LZMA2Options.PRESET_MAX);
SevenZMethodConfiguration lzmaConfig =
new SevenZMethodConfiguration(SevenZMethod.LZMA, lzma2Options);
methods.add(lzmaConfig);
out.setContentMethods(methods);
I have searched for this answer and I have tried to solve the problem but I can't. I got and Exception of type
No fue posible copiar los archivos. Motivo: C:\Test1 (Acceso denegado)java.io.FileNotFoundException: C:\Test1 (Acceso denegado)
java.io.FileNotFoundException: C:\Test1 (Acceso denegado)
Translated would be something like "It was not possible copying the files. Motive: (Access denied)".
What I am trying to do is to copy a List into a directory recursively.
I could simply copy the files recursively (I already did that) but the requirements are to copy all into a List and then do whatever I want (copy, delete, etc) with the records in the list.
My List contains this records:
C:\Test\Carpeta_A
C:\Test\Carpeta_A\Entrenamiento_1.txt
C:\Test\Carpeta_A\Requerimientos.txt
C:\Test\Carpeta_B
C:\Test\Carpeta_B\queries.txt
C:\Test\Things.txt
Here is my code:
This is the main method.. it calls a method for listing and saving the files and directories and then calls the method for copying the files into another directory preserving my main structure:
public static void main(String[] args) throws IOException
{
String fuente = "C:/Test";
String ruta = "C:/Test1";
teeeeeest listing = new teeeeeest();
List<File> files = listing.getFileListing(fuente);
listing.copyDirectories(files, ruta);
}
public List<File> getFileListing( String fuente ) throws FileNotFoundException
{
List<File> result = getFileListingNoSort(fuente);
Collections.sort(result);
return result;
}
private List<File> getFileListingNoSort( String fuente ) throws FileNotFoundException
{
File source = new File(fuente);
List<File> result = new ArrayList<>();
File[] filesAndDirs = source.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs) {
result.add(file); //always add, even if directory
String s = file.getPath().trim();
if (! file.isFile()) {
//must be a directory
//recursive call!
List<File> deeperList = getFileListingNoSort(s);
result.addAll(deeperList);
}
}
return result;
}
public static void copyDirectories(List<File> files, String destiny)
throws IOException
{
InputStream in = null;
OutputStream out = null;
File targetPath = new File(destiny);
System.out.println(targetPath.getPath());
for(int i = 0; i < files.size(); i++)
{
File temp = new File(files.get(i).toString());
//System.out.println(temp.getPath());
try
{
if(temp.isDirectory())
{
if(!targetPath.exists())
{
targetPath.mkdir();
}
File[] filesAndDirs = temp.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs)
{
if (! file.isFile())
{
//must be a directory
//recursive call!
copyDirectories(filesDirs,destiny);
}
}
}
else
{
in = new FileInputStream(files.get(i).toString());
out = new FileOutputStream(targetPath);
System.out.println(temp.getPath());
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
}
}
catch(Exception e)
{
System.err.println("No fue posible copiar los archivos. Motivo: " + e.getMessage() + e);
e.printStackTrace();
}
}
}
It would be very bad of me to just paste the working code here, it will not help you to think about what the problems are. I will try to give you enough without giving everything: Please mark my answer as accepted if it helps you.
1: You should not sort the file listing. The order the files are read in is important so that you don't get files before directories in your list. That said, it doesn't matter if you sort them because the shorter names, which are the directories, will appear first anyway. Still, don't do work you shouldn't be doing. Remove the getFileListing method and use only the getFileListingNoSort.
List<File> files = listing.getFileListingNoSort(fuente);
2: You need to pass both the source and the destination directories to copyDirectories so that you can make a destination filename from the source filename.
listing.copyDirectories(files, fuente, ruta);
3: You need to create a destination file out of the source filename. There may be better ways, but using simple String parsing will do the trick:
File temp = files.get(i);
String destFileName = destiny + temp.toString().substring(source.length());
File destFile = new File(destFileName);
4: You must create the new directories based on the new destFile. You are using the targetPath, which is only the base directory, not the new directory that needs to be created.
if(!destFile.exists())
{
destFile.mkdir();
}
5: After you make the destination directory, there is nothing else to do. Remove all that code after that up to the 'else'
6: Your outfile should be the new destFile you created.
out = new FileOutputStream(destFile);
7: close your input and output streams or the file copies will not be complete.
in.close();
out.close();
That should get you going. Use an IDE if you can so that you can step through the program with a debugger and see what's happening.
In your copyDirectories method calls the destiny is always the same value, even in recursive calls. You are copying all the files to the same destination file.
I need to read all the files from a shared location and returns a File Map. I use FTPClient to access the shared location. Using FTPClient I able to retrieve all the File as a FTPFile. But I want Convert FTPFile to File. please see the code.
FTPFile[] ftpFiles = ftpClient.listFiles(folderPath);
Note:- I Don't want to Create new connection every time. I want to read all in one connection
Looks like this is very old question but just wanted to update what I have done.
InputStream iStream=ftpClient.retrieveFileStream(ftpFile.getName());
File file = File.createTempFile("tmp", null);
FileUtils.copyInputStreamToFile(iStream, file);
Hopefully this is helpful.
If you only want get the name, try this code:
private File[] getRemoteFilesInFolder() {
FTPFile[] elements;
File[] files;
try {
elements = ftpClient.listFiles();
files = new File[elements.length];
for(int i=0; i< elements.length; i++) {
files[i] = new File(elements[i].getName());
}
return files;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
File f=new File("C:/");
File fList[] = f.listFiles();
When i use this it list all system file as well as hidden files.
and this cause null pointer exception when i use it to show in jTree like this:
public void getList(DefaultMutableTreeNode node, File f) {
if(f.isDirectory()) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode(f);
node.add(child);
File fList[] = f.listFiles();
for(int i = 0; i < fList.length; i++)
getList(child, fList[i]);
}
}
What should i do so that it do not give NullPointerException and show only non hidden and non system files in jTree?
Do this for hidden files:
File root = new File(yourDirectory);
File[] files = root.listFiles(new FileFilter() {
#Override
public boolean accept(File file) {
return !file.isHidden();
}
});
This will not return hidden files.
As for system files, I believe that is a Windows concept and therefore might not be supported by File interface that tries to be system independent. You can use Command line commands though, if those exist.
Or use what #Reimeus had in his answer.
Possibly like
File root = new File("C:\\");
File[] files = root.listFiles(new FileFilter() {
#Override
public boolean accept(File file) {
Path path = Paths.get(file.getAbsolutePath());
DosFileAttributes dfa;
try {
dfa = Files.readAttributes(path, DosFileAttributes.class);
} catch (IOException e) {
// bad practice
return false;
}
return (!dfa.isHidden() && !dfa.isSystem());
}
});
DosFileAttributes was introduced in Java 7.
If running under Windows, Java 7 introduced DosFileAttributes which enables system and hidden files to be filtered. This can be used in conjunction with a FileFilter
Path srcFile = Paths.get("myDirectory");
DosFileAttributes dfa = Files.readAttributes(srcFile, DosFileAttributes.class);
System.out.println("System File? " + dfa.isSystem());
System.out.println("Hidden File? " + dfa.isHidden());
If you are trying to list all files in C:/ please keep in mind that there are other files also which are neither hidden nor system files, but that still won't open because they require special privileges/permissions. So:
String[] files = file.list();
if (files!=null) {
for (String f : files) open(f);
}
So just compare if the array is null or not and design your recursion in such a way that it just skips those files whose array for the list() function is null.
private void nodes(DefaultMutableTreeNode top, File f) throws IOException {
if (f.isDirectory()) {
File[] listFiles = f.listFiles();
if (listFiles != null) {
DefaultMutableTreeNode b1[] = new DefaultMutableTreeNode[listFiles.length];
for (int i = 0; i < b1.length; i++) {
b1[i] = new DefaultMutableTreeNode(listFiles[i].toString());
top.add(b1[i]);
File g = new File(b1[i].toString());
nodes(b1[i], g);
}
}
}
Here is the code I used to create a window file explorer using jtree.