Question of some stranger:
any one tell me I have number of files in a folder i want make a single file with all contents of number of file without using any loop, because of it is taking too much time please help me.
Ans:
You want to merge all files in some folder?
It's impossible to do it in *usual* Java without a loop, because you have to iterate over all the files you want to merge. Maybe there is a way to do using some low-level filesystem functions, but even in this way you will have to iterate over the memory blocks representing the files.
It's impossible to do it in *usual* Java without a loop, because you have to iterate over all the files you want to merge. Maybe there is a way to do using some low-level filesystem functions, but even in this way you will have to iterate over the memory blocks representing the files.
Ans:
In Bash, i.e., below java, you can do:
- cat '~/some-directory/some-file*' > one-file.csv
or use find for more flexibility:
- find ~/some-directory/ -name 'some-file*' | xargs cat >one-file.csv
- cat '~/some-directory/some-file*' > one-file.csv
or use find for more flexibility:
- find ~/some-directory/ -name 'some-file*' | xargs cat >one-file.csv
Ans;
This sounds like an interview question asking for the knowledge of FileFilter class. The way you do it without a loop is shown below. If this is a more sophisticated question, then you may want to think about Windows journals which provides quicker access to files.
public class AggregateFileContent
{
public static void main(String[] args)
{
String parentDirPath = args == null || args.length == 0 ? "/Users/alebedev/Documents/" : args[0];
aggregateContent(new File(parentDirPath));
}
public static StringBuffer aggregateContent(final File parentDir)
{
final StringBuffer allContent = new StringBuffer();
parentDir.listFiles(new FileFilter()
{
@Override
public boolean accept(File file)
{
if (!file.isDirectory()) {
try {
String content = CharStreams.toString(new InputStreamReader(new FileInputStream(file), "UTF-8"));
allContent.append(content);
}
catch (IOException e) {
System.err.println("Failed to read file " + file.getParentFile().getAbsolutePath());
}
}
return true;
}
});
System.out.println(allContent);
return allContent;
}
}
public class AggregateFileContent
{
public static void main(String[] args)
{
String parentDirPath = args == null || args.length == 0 ? "/Users/alebedev/Documents/" : args[0];
aggregateContent(new File(parentDirPath));
}
public static StringBuffer aggregateContent(final File parentDir)
{
final StringBuffer allContent = new StringBuffer();
parentDir.listFiles(new FileFilter()
{
@Override
public boolean accept(File file)
{
if (!file.isDirectory()) {
try {
String content = CharStreams.toString(new InputStreamReader(new FileInputStream(file), "UTF-8"));
allContent.append(content);
}
catch (IOException e) {
System.err.println("Failed to read file " + file.getParentFile().getAbsolutePath());
}
}
return true;
}
});
System.out.println(allContent);
return allContent;
}
}
No comments:
Post a Comment