Getting filesnames from directory and write to file
Hello guys.
I was trying to get filenames from a directory and then put as content to another file.
Here is what I've tried:
Code :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadFromDirectory {
public static void main(String[] args) {
String directoryName = "C:" + File.separator + "test";
String outputFile = "C:" + File.separator + "test" + File.separator + "test2";
File dir = new File(directoryName);
visitAllFiles(dir);
File objOutFile = new File(outputFile);
try {
//Create a stream for our output
FileOutputStream hamletOutputStream = new FileOutputStream(objOutFile);
// Write our output to our stream
// and thus to our file
hamletOutputStream.write('\n');
hamletOutputStream.write(dir);
//Close the output stream
hamletOutputStream.close();
}
catch (FileNotFoundException fnfe) {
System.out.println("Couldn't find a file called" + outputFile);
} catch (IOException ioe) {
System.out.println("Couldn't find a file called" + outputFile);
}
}
public static void visitAllFiles(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllFiles(new File(dir, children[i]));
}
} else {
process(dir);
}
}
}
There are some mistakes and I can't figure out how to fix them, or may be I didn't understand the method. Help me to solve that. Thanks.
Re: Getting filesnames from directory and write to file
What does the process method do? It seems to me that visitAllFiles is not actually changing anything at the moment.
Are you getting any errors or exceptions or anything?
Re: Getting filesnames from directory and write to file
Thanks for posting.
Actually it shoud list the files in a directory, I took it from here: Listing the Files or Subdirectories in a Directory | Example Depot
But it uses recursion and I can't understand.
I find out another way to solve my problem. It works, but I don't know how to not replace the existing data in the file.
Code java:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
//import java.io.FileNotFoundException;
//import java.io.IOException;
import java.io.*;
public class ReadFromDirectory {
public static void main(String[] args) {
String directoryName = "C:" + File.separator + "test";
String outputFile = "C:" + File.separator + "test" + File.separator + "test2.txt";
String files;
File folder = new File(directoryName);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
System.out.println(files);
try{
FileWriter outFile = new FileWriter(outputFile);
PrintWriter out = new PrintWriter(outFile);
out.write(files);
out.close();
}
catch (Exception e){
System.out.println("Exception");
}
}
}
}
}
The output is the last file name processed, but I don't want it. I want to have all the names from the directory. How can I merge all the filenames like bunch of strings in my test2.txt?