Moving files to a single list iteratively
Hello,
I have a folder of around 200 notepad files with ticket information inside.
Essentially, I need to compile a single PDF or Excel spreadsheet from all the information inside the individual notepad files.
I don't want to have to open each notepad file, then copy and paste its data manually. I'd prefer to use some sort of loop function that opens each notepad file, extracts the data, and puts it onto an excel or PDF sheet.
Is this possible?
Thanks!
Re: Moving files to a single list iteratively
I have moved your post to a more appropriate forum.
Yes it is possible. Get the folder, list the files in said folder, and read each one in a loop. See
Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
Re: Moving files to a single list iteratively
Hello Growler,
Welcome to the forums.
I have some time on my hands so here is a good example to get you going..
Code Java:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class TxtFiles {
/**
* JavaProgrammingForums.com
*/
// Directory path here
public static String path = ".";
public static void main(String[] args) {
listFiles();
}
public static void listFiles() {
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".txt") || files.endsWith(".TXT")) {
// Lists files in console
System.out.println(files);
openFile(files);
}
}
}
}
public static void openFile(String file) {
try {
FileInputStream in = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
// Prints content of file to console
System.out.println(strLine);
// Write file method needs to go here
}
} catch (Exception e) {
System.out.println(e);
}
}
}
This code lists all the .txt files in a directory. It then opens them one by one and displays their content.
You will need to figure out how to write the content to a single file.
All the code you need can be found here - Java Code Snippets and Tutorials - Java Programming Forums