Creating a text file from a list of files? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Creating a text file from a list of files?

flatleyldflatleyld Posts: 10Member
I have a list of pdf files that I need to be put into a text file. My I know something is wrong with the structure of my for loop, but I can't figure out what to do to make it work. here is my code:

[code]import java.io.*;
import java.io.IOException;

public class Main
{

public static void main(String[] args) throws IOException
{
String dirName = "\\optical\Data\Pemco\";
String pemfile = "\\optical\Data\Pemco\pemco.txt";
PrintWriter outputFile = new PrintWriter(pemfile);
File dir = new File(dirName);
File[] content = dir.listFiles();
String quote = """;
int i = 0;

if (content == null) { System.out.println("Unable to read directory: "+dirName); }
else {
for (i = 0; i < content.length; i++) {
File file = content[i];
String fileName = file.getName();

if (fileName.indexOf(".pdf")!=-1) {
if (fileName.substring(0,5) != "ADMIN")
{
String month = fileName.substring(5,7);
String day = fileName.substring(7,9);
String year = fileName.substring(9,13);
outputFile.println(quote+"ADMIN"+quote+","+quote+"Admin Reports"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,3) != "AO1")
{
String month = fileName.substring(3,5);
String day = fileName.substring(5,7);
String year = fileName.substring(7,11);
outputFile.println(quote+"AO1"+quote+","+quote+"AO1"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,3) != "AO2")
{
String month = fileName.substring(3,5);
String day = fileName.substring(5,7);
String year = fileName.substring(7,11);
outputFile.println(quote+"AO2"+quote+","+quote+"AO2"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,5) != "AOATM")
{
String month = fileName.substring(5,7);
String day = fileName.substring(7,9);
String year = fileName.substring(9,13);
outputFile.println(quote+"AOATM"+quote+","+quote+"AOATM"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,3) != "ATM")
{
String month = fileName.substring(3,5);
String day = fileName.substring(5,7);
String year = fileName.substring(7,11);
outputFile.println(quote+"ATM"+quote+","+quote+"ATM"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,5) != "BKOFF")
{
String month = fileName.substring(5,7);
String day = fileName.substring(7,9);
String year = fileName.substring(9,13);
outputFile.println(quote+"BKOFF"+quote+","+quote+"Back Office"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,5) != "CDACT")
{
String month = fileName.substring(5,7);
String day = fileName.substring(7,9);
String year = fileName.substring(9,13);
outputFile.println(quote+"CDACT"+quote+","+quote+"Card Activation"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,4) != "CRDI")
{
String month = fileName.substring(4,6);
String day = fileName.substring(6,8);
String year = fileName.substring(8,12);
outputFile.println(quote+"CRDI"+quote+","+quote+"Card Issue"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,7) != "DISPUTE")
{
String month = fileName.substring(7,9);
String day = fileName.substring(9,11);
String year = fileName.substring(11,15);
outputFile.println(quote+"DISPUTE"+quote+","+quote+"Dispute Processing"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,7) != "INVOICE")
{
String month = fileName.substring(7,9);
String day = fileName.substring(9,11);
String year = fileName.substring(11,15);
outputFile.println(quote+"INVOICE"+quote+","+quote+"Invoice Risk Report"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,4) != "RISK")
{
String month = fileName.substring(4,6);
String day = fileName.substring(6,8);
String year = fileName.substring(8,12);
outputFile.println(quote+"RISK"+quote+","+quote+"Risk Reports"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,6) != "SETTLE")
{
String month = fileName.substring(6,8);
String day = fileName.substring(8,10);
String year = fileName.substring(10,14);
outputFile.println(quote+"SETTLE"+quote+","+quote+"Settlement"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}
if (fileName.substring(0,5) != "TRANS")
{
String month = fileName.substring(5,7);
String day = fileName.substring(7,9);
String year = fileName.substring(9,13);
outputFile.println(quote+"TRANS"+quote+","+quote+"Transmission"+quote+","+quote+month+"/"+day+"/"+year+quote+","+quote+dirName+fileName+quote);
}

break;
}
}
}
outputFile.close();
}
}[/code]

The output to the text file should be this:

"ADMIN","Admin Reports","04/07/2009","\OpticalDataPemcoADMIN04072009.pdf"
"AO1","AO1","04/07/2009","\OpticalDataPemcoAO104072009.pdf"
"AO2","AO2","04/07/2009","\OpticalDataPemcoAO204072009.pdf"
"AOATM","AOATM","04/07/2009","\OpticalDataPemcoAOATM04072009.pdf"
"ATM","ATM","04/07/2009","\OpticalDataPemcoATM04072009.pdf"
"BKOFF","Back Office","04/07/2009","\OpticalDataPemcoBKOFF04072009.pdf"
"CDACT","Card Activation","04/07/2009","\OpticalDataPemcoCDACT04072009.pdf"
"CRDI","Card Issue","04/07/2009","\OpticalDataPemcoCRDI04072009.pdf"
"DISPUTE","Dispute Processing","04/07/2009","\OpticalDataPemcoDISPUTE04072009.pdf"
"INVOICE","Invoice Risk Report","04/07/2009","\OpticalDataPemcoINVOICE04072009.pdf"
"RISK","Risk Reports","04/07/2009","\OpticalDataPemcoRISK04072009.pdf"
"SETTLE","Settlement","04/07/2009","\OpticalDataPemcoSETTLE04072009.pdf"
"TRANS","Transmission","04/07/2009","\OpticalDataPemcoTRANS04072009.pdf"

But instead it comes out like this:

"ADMIN","Admin Reports","04/07/2009","\opticalDataPemcoADMIN04072009.pdf"
"AO1","AO1","IN/04/0720","\opticalDataPemcoADMIN04072009.pdf"
"AO2","AO2","IN/04/0720","\opticalDataPemcoADMIN04072009.pdf"
"AOATM","AOATM","04/07/2009","\opticalDataPemcoADMIN04072009.pdf"
"ATM","ATM","IN/04/0720","\opticalDataPemcoADMIN04072009.pdf"
"BKOFF","Back Office","04/07/2009","\opticalDataPemcoADMIN04072009.pdf"
"CDACT","Card Activation","04/07/2009","\opticalDataPemcoADMIN04072009.pdf"
"CRDI","Card Issue","N0/40/7200","\opticalDataPemcoADMIN04072009.pdf"
"DISPUTE","Dispute Processing","07/20/09.p","\opticalDataPemcoADMIN04072009.pdf"
"INVOICE","Invoice Risk Report","07/20/09.p","\opticalDataPemcoADMIN04072009.pdf"
"RISK","Risk Reports","N0/40/7200","\opticalDataPemcoADMIN04072009.pdf"
"SETTLE","Settlement","40/72/009.","\opticalDataPemcoADMIN04072009.pdf"
"TRANS","Transmission","04/07/2009","\opticalDataPemcoADMIN04072009.pdf"

I can see something is wrong with the fileName variable because the loop can go through the next cycle of names, but I needed to put the break in because it would create a text file too long. I'm such a noob. Please help me.

Comments

Sign In or Register to comment.