Java File processing
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, April 3, 2003 08:54 AM

Hi, I am trying to read records from a multi-record pipe ("|") delimited file, make some modifications to the data elements and write the records to an output file. I am using the "StringTokenizer" class to read the file and access individual tokens and I am using the "PrintWriter" class to write to an outout file. The problem is, I am only able to write the last record to the output file, though I am able to read all the records from the input file. I think I am overwriting the previous records and am able to write and display only the last record. How can I write all the records I read into an output file? My current code is listed below: Also, when I read from a pipe delimited file using as fol   More>>

Hi,

I am trying to read records from a multi-record pipe ("|") delimited file, make some modifications to the data elements and write the records to an output file. I am using the "StringTokenizer" class to read the file and access individual tokens and I am using the "PrintWriter" class to write to an outout file.

The problem is, I am only able to write the last record to the output file, though I am able to read all the records from the input file. I think I am overwriting the previous records and am able to write and display only the last record. How can I write all the records I read into an output file? My current code is listed below:

Also, when I read from a pipe delimited file using as follows,

StringTokenizer t = new StringTokenizer(s, "|")

if the input file has a null value, I am unable to read it. For example, if the input file is like this:

XYZ|ABC||TTT|YYY

I am able to read the first two values, XYZ and ABC but am unable to read the 3rd value, which is null. How can I read this null value using the StringTokenizer class?


The following is my current code. Thanks a lot for your help.
__________________________________________________
import java.io.*;
import java.util.*;

class inputFile
{

private String ordDate;
private String ordNum;
private String custName;
private String custDesc;
private String ccType;
private String newccType;
private String expDate;

public inputFile()
{}

public void readData(BufferedReader in) throws IOException
{
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, "|");

ordDate = t.nextToken();
ordNum = t.nextToken();
custName = t.nextToken();
custDesc = t.nextToken();
ccType = t.nextToken();

if (ccType.equals("V") )
newccType = "VI";

expDate = t.nextToken();

System.out.println(ordDate);
}

public void writeData(PrintWriter out) throws IOException
{
out.println(ordDate + "|" +
ordNum + "|" +
custName + "|" +
custDesc + "|" +
newccType + "|" +
expDate);
}
}

class readFile2
{
public static void main(String args[])
{
inputFile inp = new inputFile();

// Read the input file
try
{
BufferedReader in = new BufferedReader(new FileReader("orders.dat"));

//inputFile ipf = readData(in);
inp.readData(in);
in.close();
}

catch(IOException e)
{
System.out.print("Error reading from the file : " + e);
System.exit(1);
}


// Write the contents to a new file
try
{
PrintWriter out = new PrintWriter(new FileWriter("sri2.dat"));

inp.writeData(out);
out.close();
}

catch(IOException e)
{
System.out.print("Error reading from the file : " + e);
System.exit(1);
}

}
}

   <<Less

Re: Java File processing

Posted By:   Stephen_Ostermiller  
Posted On:   Thursday, April 3, 2003 05:18 PM

Your program is all crammed together in the forum. When posting here, please useng the tags that are listed at the bottom of the compose page for posting code.


The reason that you only get the last record in the file is that you keep opening the file with a FileWriter for each writer. A FileWriter erases the file each time it is opened. You should open the file then write all your records.


StringTokenizer does not do null tokens. There are several ways to get around this.

About | Sitemap | Contact