Article View: borland.public.jbuilder.java.language
Article #15669Re: Saving data to files
From: Tom Heagy
Date: Fri, 07 May 1999 00:00
Date: Fri, 07 May 1999 00:00
127 lines
4039 bytes
4039 bytes
Here are some code excerpts you might find handy.
==========================================================================
/** Creates a file and returns the handle to it via a PrintWriter
object.<BR>
* AutoFlush is set to TRUE.<BR>
* Output to the file is done by calling "print" or "println".
* @param fileName The name of the file to create
* @return the handle to the file.<BR><BR>
*/
public static PrintWriter createASCIIfile(String fileName)
{
PrintWriter aWriter = null;
try
{
aWriter = new PrintWriter(new BufferedWriter(new
FileWriter(fileName)), true);
}
catch (IOException e)
{
System.out.println("IO Exception attempting to create file: '" +
fileName + "'");
}
return aWriter;
}
/** This method opens a file and returns the handle to it via a
BufferedReader object
* Example code for reading from the file follows:
* BufferedReader aReader = Util.openASCIIfile("FileToRead.Txt");
* if (aReader != null)
* try
* {
* String sLine = aReader.readLine();
* while (sLine != null)
* {
* System.out.println(sLine);
* sLine = aReader.readLine();
* }
* aReader.close();
* }
* catch (IOException e)
* {
* System.out.println("IO Exception reading file.");
* }
* @param fileName The name of the file to be opened.
* @return the handle to the BufferedReader file.<BR><BR>
*/
public static BufferedReader openASCIIfile(String fileName)
{
BufferedReader aReader = null;
try
{
aReader = new BufferedReader(new FileReader(fileName));
}
catch (IOException e)
{
System.out.println("IO Exception attempting to open file: '" +
fileName + "'");
}
return aReader;
}
/** This method opens a random access file.
* The following code excerpt shows how to read the file a line at a
time.
* String sLine = aFile.readLine();
* while (sLine != null) {
* System.out.print(sLine);
* sLine = aFile.readLine();
* }
* aFile.close();
* @param fileName The name of the file to be opened.
* @param isReadOnly Indicates if the file is to be opened as Read-Only
or not.
* @return the handle to the RandomAccessFile.<BR><BR>
*/
public static RandomAccessFile openRandomFile(String fileName, boolean
isReadOnly)
{
RandomAccessFile aFileHandle = null;
String sReadWriteAttribute;
if (isReadOnly)
sReadWriteAttribute = "r"; // read only
else
sReadWriteAttribute = "rw"; // read/write
try
{
aFileHandle = new RandomAccessFile(fileName, sReadWriteAttribute);
}
catch (IOException e)
{
System.out.println("IO Exception attempting to open file: '" +
fileName + "'");
}
return aFileHandle;
}
public static void saveGame(boolean bIsCurrentPlayerFinished)
{
java.io.PrintWriter aFile =
Util.createASCIIfile(SAVED_GAME_FILE_NAME);
if (aFile != null)
{
aFile.println("[GAME]");
aFile.println("iNbrOfPlayers=" + iNbrOfPlayers);
// etc.
}
aFile.close();
}
==========================================================================
Simon Murrell wrote:
> Hello
>
> Does anyone have a sample java source code of saving basic
> string/integer data to a flat file.
>
> Thanks
> Simon
Technical Details
Message-ID:
<373330E6.CDA969AF@sciatl.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3732F30B.AC1942B4@goaldata.co.za>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,af3124ab2ad06a2c X-Google-Attributes: gidf74b3,public From: Tom Heagy <tom.heagy@sciatl.com> Subject: Re: Saving data to files Date: 1999/05/07 Message-ID: <373330E6.CDA969AF@sciatl.com> X-Deja-AN: 475253283 Content-Transfer-Encoding: 7bit References: <3732F30B.AC1942B4@goaldata.co.za> To: Simon Murrell <smurrell@goaldata.co.za> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language