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.
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?