The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

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.

Message:

way to do

Posted by Mukul on October 23, 2001 at 5:21 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.io.RandomAccessFile;
> import java.util.StringTokenizer;
> import java.io.FileNotFoundException;
> import java.io.File;
> import java.io.IOException;


> 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
> if(passwordFile.exists()){

> // check the file is readable
> if(passwordFile.canRead()){

> // check the file for writable
> if(!passwordFile.canWrite()){
> return "File is not writable";
> }
> }
> else{
> return "can't read the File";
> }
> }
> else{
> return " File Not Found ";
> }
> passwordFile = null;

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

> try{

> //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[].
> split(strLine.toString());

> // 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.
> */
> else{
> iLengthDiff = strNewLine.toString().length() - strLine.length();
> }

> // move the pointer to the starting postion of the line.
> raf.seek(lStartPos);

> // 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
> raf.close();

> // 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 );

> raf.close();
> return "APPEND";

> }// try block

> catch(FileNotFoundException Ex){
> raf.close();
> return "File Not Found";
> }

> catch(Exception e){
> raf.close();
> return e.toString() ;
> }

> }//function

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

>
> Thanks,

> Jyothi


The characters are being chopped off as they are being overwritten as if the length of new password is more or less than the old password, the characters wont be shifted. Having said that, maybe you can modify your program a bit...

1. suppose xxxx is to be replaced with xxxxxxxx.
2. get the position of xxxx and call it say position.
3. read all the characters position + xxxx.length() till the end of file in a buffer.
4. insert xxxxxxxx at the beginning of the buffer.
5. position the file pointer at position and write the contents of buffer to the file.



Replies:

Sponsored Links



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