The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


RandomAccessFile Question

Posted by Jyothi on October 22, 2001 at 8:30 AM

Hi Everybody,

I am having a problem with writing data from a text file using RandomAccessFile stream.

The problem is this;
I am opening a password file which is of form username:password,if the username is found and the password has been modified I am taking the new password and updating the same in a .config file.

If the password is greater than the existing password then the next line characters(beginning) are being chopped off.

Is there anything wrong in the code that i have written;The code is as follows:

Any help on this is highly appreciated,

import java.util.StringTokenizer;

public class TPassfileManagerBatchUnix implements IPassfileManagerBatch{
final String COLON =":";
String strPassTokens[];

* This method takes the passwordfilename,UserName,Password and a boolean flag
* opens the file for read,write mode and checks for the username
* if the username is found updates with the new given password.
* if the username is not found it append a line at the end of the file
* as this format username:password.

* @param strPasswordFile the file name to be update the password.
* @param fCaseSensitive is a flag to tell the username matches with case sensitive
* @param strUserName the Username
* @param strPassword the Password which will be updeted in the file.

public String updateAppendPassword(String strPasswordFile, boolean fCaseSensitive , String strUserName, String strPassword) throws IOException {

/** e.g line in the password file.
* userid:password */

//*********** File System Object Checking ****************
// create a file object that will contain all the file properties.
File passwordFile = new File(strPasswordFile);

// check whether the given file exists in the file system

// check the file is readable

// check the file for writable
return "File is not writable";
return "can't read the File";
return " File Not Found ";
passwordFile = null;

// opens the file for read and write mode
RandomAccessFile raf = new RandomAccessFile(strPasswordFile,"rw");


//each line in the password file.
String strLine="";
StringBuffer strNewLine = new StringBuffer();

long lStartPos;

// starting position of the line.
lStartPos = raf.getFilePointer();
int f=0;

// read the file until EOF reaches
while( (strLine = raf.readLine()) != null ){

strLine = strLine.trim();

//Check if the line is blank
if(strLine.length() > 0){
strPassTokens = new String[1];

// split the line with the delimeter ":" and store each value in the
// array strPassTokens[].

// check the current line is for the User.
if(fCaseSensitive ? strPassTokens[0].equals(strUserName) : strPassTokens[0].equalsIgnoreCase(strUserName)){

//Update the Password with the New Password.
strPassTokens[1] = strPassword;

// prepare the new line with the new password.
strNewLine.append(strPassTokens[0] + COLON + strPassTokens[1]);

// holds the difference of lenght between the OldLine and NewLine
int iLengthDiff;

// Check if the new line lenght is lessthan the old Line
// then append the that many empty chars to the new line.

if( strNewLine.toString().length() < strLine.length()){

iLengthDiff = strLine.length() - strNewLine.toString().length();

for (int i=0; i strNewLine = strNewLine.append(" ");

/* * the new line lenght is lessthan the existing line
* so there is no need to increase the file size.
* reset the iLenghtDiff to ZERO
iLengthDiff = 0;

/* * if the NewLine length is greater than the OldLine
* find the difference and increase the file size to
* that many characters.
iLengthDiff = strNewLine.toString().length() - strLine.length();

// move the pointer to the starting postion of the line.;

// find the lenght of the file size.
long lFileSize;
lFileSize = raf.length();

//increase the file size to number of extra characters in the new password
//raf.setLength(lFileSize + iLengthDiff );

// write the new line into the file.
raf.writeBytes(strNewLine.toString() + "\n");

//close the file stream

// return SUCCESS and exit from the function.
return "SUCCESS";

} // if the password matches

}// if the line is empty

// get the next line strating postion before reading the next line.
lStartPos = raf.getFilePointer();

}// while loop until the EOF

/* * if the password is not found until the End of File
* append the username and password with the this format
* username:password in to the password file.

String strAppendLine;

strAppendLine = strUserName + COLON + strPassword;

raf.writeBytes( "\n" + strAppendLine );

return "APPEND";

}// try block

catch(FileNotFoundException Ex){
return "File Not Found";

catch(Exception e){
return e.toString() ;


// This method splits the given String with the ":" delimeter.
private void split(String strLine){
int i=0;
StringTokenizer st = new StringTokenizer(strLine,COLON);
strPassTokens[i] = st.nextToken();



  • Go easy Jay October 27, 2001 at 1:52 PM (0)
  • way to do Mukul October 23, 2001 at 5:21 AM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us