Article View: borland.public.jbuilder.java.language
Article #15218Re: Trying to create a new line in an output file?
From: Eric Advincula
Date: Tue, 06 Jul 1999 00:00
Date: Tue, 06 Jul 1999 00:00
248 lines
6357 bytes
6357 bytes
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
public class IOLine1 {
public static void main( String args[] )
{
CWinIniIO m_ini = new CWinIniIO();
m_ini.blUseDebugMode = true;
if( m_ini.open( 100, "d:\\public\\test.ini" ) == true )
{
String sValue = null;
if( m_ini.read("DataTypes", "Value", sValue ) == true )
{
System.out.println( sValue );
m_ini.close(100);
}
}
if( m_ini.open( 101, "d:\\public\\test.ini" ) == true )
{
if( m_ini.write( "DataTypes", "Value", "Emethis" ) == true )
m_ini.close(101);
}
}
}
class CWinIniIO
{
public boolean blUseDebugMode = false;
private File inputFile = null;
private File outputFile = null;
private File tempFile = null;
private FileInputStream inputStream = null;
private FileInputStream tempStream = null;
private FileOutputStream outputStream = null;
private DataInput aDI = null;
private DataInput aTemp = null;
private DataOutput aDO = null;
public final int READ_FILE = 100;
public final int WRITE_FILE = 101;
String sInputLine = null;
String sInputFilePath = null;
String sTempInputFilePath = null;
String sOutputFilePath = "c:\\tmp.ini";
CWinIniIO()
{
}
public boolean open( int nFormat, String sFile )
{
try
{
if( nFormat == READ_FILE )
inputFile = new File( sInputFilePath = sFile );
inputStream = new FileInputStream( inputFile );
aDI = new DataInputStream( inputStream );
}
else if( nFormat == WRITE_FILE )
tempFile = new File ( sTempInputFilePath = sFile );
tempStream = new FileInputStream( tempFile );
aTemp = new DataInputStream( tempStream );
outputFile = new File( sOutputFilePath );
outputStream = new FileOutputStream( outputFile );
aDO = new DataOutputStream( outputStream );
}
return true;
}
catch( java.io.IOException e )
{
return false;
}
}
public boolean close( int nFormat )
{
try
{
if( nFormat == READ_FILE )
inputStream.close();
else if( nFormat == WRITE_FILE )
{
tempStream.close();
outputStream.close();
}
return true;
}
catch( java.io.IOException e )
{
return false;
}
}
public boolean write( String sSection, String sKey, String sValue )
{
String sTemp = "[" + sSection + "]";
if( blUseDebugMode == true )
System.out.println( sTemp );
StringBuffer sbModifiedLine = null;
try
// Loops until eof
while(( sInputLine = aTemp.readLine()) != null )
{
if( blUseDebugMode == true )
System.out.println( sInputLine );
// Found the correct "Section"
if( sInputLine.compareToIgnoreCase( sTemp ) == 0 )
{
sbModifiedLine = new StringBuffer( sInputLine );
aDO.writeBytes( sbModifiedLine.toString() );
aDO.write( '\n' );
// Loops until eof
while(( sInputLine = aTemp.readLine()) != null )
{
if( blUseDebugMode == true )
System.out.println( sInputLine );
// Found the correct "Key"
if( sInputLine.indexOf( sKey ) != -1 )
{
// Fixing the new "Key"
sInputLine = sKey + "=" + sValue;
if( blUseDebugMode == true )
System.out.println( sInputLine );
sbModifiedLine = new StringBuffer( sInputLine );
// Writes the new "Key=Value"
aDO.writeBytes( sbModifiedLine.toString() );
aDO.write( '\n' );
}
else
sbModifiedLine = new StringBuffer( sInputLine );
// Writes the output
aDO.writeBytes( sbModifiedLine.toString() );
aDO.write( '\n' );
}
}
if( blUseDebugMode == true )
System.out.println( sInputLine );
}
if( sInputLine != null )
sbModifiedLine = new StringBuffer( sInputLine );
// Writes the output
aDO.writeBytes( sbModifiedLine.toString() );
aDO.write( '\n' );
file://aDO.writeBytes( "\n" );
if( blUseDebugMode == true )
System.out.println( sInputLine );
}
}
return true;
}
catch( java.io.IOException e )
{
return false;
}
}
public boolean read(String sSection, String sKey, String sValue )
{
String sTemp = "[" + sSection + "]";
if( blUseDebugMode == true )
System.out.println( sTemp );
try
// Continues the loop untils the end of file
while(( sInputLine = aDI.readLine()) != null )
{
if( blUseDebugMode == true )
System.out.println( sInputLine );
// If passes then found the "Section"
if( sInputLine.compareToIgnoreCase( sTemp ) == 0 )
{
// Continues loop until end of file
while(( sInputLine = aDI.readLine()) != null )
{
if( blUseDebugMode == true )
System.out.println( sInputLine );
// If passes then "Value" is found
if( sInputLine.indexOf( sKey ) != -1 )
{
sValue = sInputLine.substring( sKey.length() + 1 );
if( blUseDebugMode == true )
System.out.println( sTemp );
break;
}
}
}
}
return true;
}
catch( java.io.IOException e )
{
return false;
}
}
}
Tom Davies <tom@smartchat.net.au> wrote in message
news:37826A67.42D55575@smartchat.net.au...
> Eric Advincula wrote:
> > private FileOutputStream outputStream = null;
>
> > private DataOutput aDO = null;
>
> It would be useful to see how these are created.
>
> Tom
Technical Details
Message-ID:
<7ltg4f$d5f7@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7ln468$7266@forums.borland.com> <37826A67.42D55575@smartchat.net.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,6eca38836f6f34eb X-Google-Attributes: gidf74b3,public From: "Eric Advincula" <eric07@earthlink.net> Subject: Re: Trying to create a new line in an output file? Date: 1999/07/06 Message-ID: <7ltg4f$d5f7@forums.borland.com> X-Deja-AN: 497823566 References: <7ln468$7266@forums.borland.com> <37826A67.42D55575@smartchat.net.au> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.jbuilder.java.language