Article View: borland.public.jbuilder.java.language
Article #15119Re: Trying to create a new line in an output file?
From: Tom Davies
Date: Sat, 10 Jul 1999 00:00
Date: Sat, 10 Jul 1999 00:00
251 lines
7218 bytes
7218 bytes
Eric,
I don't think you should be using a DataOutputStream -- that is for
writing native Java types in a portable way. I think a BufferedWriter
would be better. You can use the newLine() method to write a line
seperator to the file (this will give you CR LF on DOS and LF on UNIX)
Tom
Eric Advincula wrote:
>
> 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:
<37866B1B.2D4321FE@smartchat.net.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7ln468$7266@forums.borland.com> <37826A67.42D55575@smartchat.net.au> <7ltg4f$d5f7@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,6eca38836f6f34eb X-Google-Attributes: gidf74b3,public From: Tom Davies <tom@smartchat.net.au> Subject: Re: Trying to create a new line in an output file? Date: 1999/07/10 Message-ID: <37866B1B.2D4321FE@smartchat.net.au> X-Deja-AN: 499045777 Content-Transfer-Encoding: 7bit References: <7ln468$7266@forums.borland.com> <37826A67.42D55575@smartchat.net.au> <7ltg4f$d5f7@forums.borland.com> To: Eric Advincula <eric07@earthlink.net> Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language