Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output file handling and definition

Status
Not open for further replies.

artskib

Programmer
Jul 17, 2002
8
US
Hi there,

I am fairly new to writing code in Java.
What I'm trying to do is to write a program which will read an input file, and then create a multiple output files, based on a control breaks (tags) in the input file. The output file names, will be derived from the control-break lines of the input file.

So far, most of my logic works.
I am having a problem (the program will not compile) writing to the output file outside of the IF construct, where the output file is defined. There is also a compilation error with closing the output file.

Here is the code:

/* 7/6/05 - Parse throught the rank and create separate RAW file for each lit. */

import java.io.*;
import java.text.*;

public class RankSeparation {

static final String IN_FILE_PATH="C:\\Art\\LitArchive\\Ranks\\";
static final String OUT_FILE_PATH="C:\\Art\\LitArchive\\Lits\\";
static final String IN_FILE_EXTENTION=".dat";
static final String OUT_FILE_EXTENTION=".dat";


/***************************************************************/
public static void main (String args[]) {

// Check for arguments - syntax: program_name input_file
if (args.length < 1)
{
System.out.println("!!! Error: No filename specified !!!\n");
System.out.println("Proper syntax is: RankSeparation raw_filename <ENTER>\n");
System.exit(0);
}
if (args.length > 1)
{
System.out.println("!!! Error: Too many parameters !!!\n");
System.out.println("Proper syntax is: RankSeparation raw_filename <ENTER>\n");
System.exit(0);
}


try
{
ProcessInputFile_function(args[0]);
}
catch (Exception exc)
{
String err = exc.toString();
System.out.println("Error in ProcessInputFile_function:" + err);
} // end try/catch

} // end main

/***************************************************************/
// Read and process every record in the input file, code from page 614
public static void ProcessInputFile_function(String sParmFileName) throws IOException {

String sInFilename="";
String sOutFilename="";
String sOutFilenameWithoutPath="";
// int sInputRecordCntr=0;
// boolean bOutputFileOpened = false;
int iCharCnt=0;
int iLitCnt=0;
String sTag="";
String sInvoiceNumber="";
String sOrderNumber="";
String sMemberFirstName="";
String sMemberLastName="";
String sMemberAddress1="";
String sMemberAddress2="";
String sMemberState="";
String sMemberCity="";
String sMemberZip="";
String sMemberPhoneDay="";
String sMemberPhoneEvening="";

sInFilename = IN_FILE_PATH+sParmFileName+IN_FILE_EXTENTION;
System.out.println("\n Input file="+sInFilename);

// Open the input file
RandomAccessFile fileIn = new RandomAccessFile(sInFilename, "r");
long lFilePointer = 0;
long lFileLength = fileIn.length();

// read all reacords in the input file
while (lFilePointer < lFileLength) {
String sInputRecord = fileIn.readLine();
lFilePointer = fileIn.getFilePointer();

sTag = sInputRecord.substring(0,6);

if (sTag.equals("<PTAD>") || sTag.equals("<ATAD>")) {

sInvoiceNumber = sInputRecord.substring(23,32);
sOrderNumber = sInputRecord.substring(6,16);
sMemberFirstName = sInputRecord.substring(33,53);
sMemberLastName = sInputRecord.substring(53,73);
sMemberAddress1 = sInputRecord.substring(73,98);
sMemberAddress2 = sInputRecord.substring(98,123);
sMemberCity = sInputRecord.substring(123,148);
sMemberState = sInputRecord.substring(148,150);
sMemberZip = sInputRecord.substring(150,159);
sMemberPhoneDay = sInputRecord.substring(159,169);
sMemberPhoneEvening = sInputRecord.substring(169,179);

sOutFilename=OUT_FILE_PATH+"inv"+sInvoiceNumber+"_"+sParmFileName+OUT_FILE_EXTENTION;
System.out.println("\n Output file="+sOutFilename);
sOutFilenameWithoutPath="inv"+sInvoiceNumber+"_"+sParmFileName+OUT_FILE_EXTENTION;

// Open the dynamically named output file here
File fileOut = new File (OUT_FILE_PATH, sOutFilenameWithoutPath);
FileOutputStream fosOut = new FileOutputStream (fileOut);
PrintStream psOut = new PrintStream(fosOut);

// xyz1 This next line works fine
psOut.println(sInputRecord);



} // end if (sTag.equals("<PTAD>") || sTag.equals("<ATAD>"))


// write the data to individual lit files here

// xyz2 This line gives me compilation error
psOut.println(sInputRecord);



} // end while

// Close the input file
fileIn.close();
// Close the output file

// xyz3 This line gives me a compilation error as well
fOut.close();



} // end ProcessInputFile_function




} // end of class


Here are the compilation errors:

C:\art\web\java\Programs\RankSeparation>javac RankSeparation.java
RankSeparation.java:113: cannot resolve symbol
symbol : variable psOut
location: class RankSeparation
psOut.println(sInputRecord);
^
RankSeparation.java:122: cannot resolve symbol
symbol : variable fOut
location: class RankSeparation
fOut.close();
^
2 errors


Any pointers will be greatly appreciated.

 
Walk this way:
Code:
PrintStream psOut = null;
if (sTag.equals("<PTAD>") || sTag.equals("<ATAD>")) 
{
        // ...
        PrintStream psOut = new PrintStream(fosOut);
        // ...
}
if (psOut != null) psOut.close ();

seeking a job as java-programmer in Berlin:
 
Thanks Stefan.
Your sugestion worked fine with one modification - the line "PrintStream psOut = new PrintStream(fosOut);" had to be changed to "psOut = new PrintStream(fosOut);". Otherwise, to compiler comlained that the psOut was already defined.
 
oh, sorry, yes.
I forgot to correct this.

Here with the nice code-tags:
Code:
PrintStream psOut = null;
if (sTag.equals("<PTAD>") || sTag.equals("<ATAD>"))
{
        // ...
        psOut = new PrintStream(fosOut);
        // ...
}
if (psOut != null) psOut.close ();

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top