How can we set cursor position in a text file?
My requirement is that i need to remove the new line charachter (as the cursor points in a new line)and i need to move the cursor to the previous line(end of the line) .
when i open that text file and press 'Control+End' key.Cursor points at the * location.
But i want the cursor to be at the end of the second line.(Where ^ is a special charachter i.e 'Enter' Charachter)
And one more thing is that i also want to replace the last charachter of the last line to 'BackSpace' charachter and place the cursor next to that.
File should be like this:
ABCD 5522203000027B1TIRUPATI RC.WADA 232408/02/201001 261107/04/201001 287 0 0^
ABCD 5522203000029B1TIRUPATI RC.WADA 165408/02/201001 165407/04/201002 414 0 0#
(where # is the 'Backspace' charachter and when i press 'Control+End' key.Cursor points next to that)
Because in our requirement we need to upload that text file to machine which wont allow the first file.
I did lot of coding using RandomAccess File but of no use.
Can you please help me out.
Joined: Jan 11, 2010
What i have done usin RandomAcess File was:
RandomAccessFile raf = new RandomAccessFile(filePath.concat("/".concat(filename)), "rw");
long length = raf.length();
raf.setLength(length - 2);
Please Any suggestions .
Joined: Mar 22, 2005
So you want something to happen once you open the file in a text editor (or some other application, you didn't provide details about that - is it a Java application?); is that correct? If so, I don't see where Java enters the picture, since, as I said before, files don't have a concept of a cursor - applications do.
My requirement is that can we modify the file and set the pointer at the end of the last line (Next to the last charachter)using java api while we deal with the Files.
That modified text file will be used by our customers to upload those files in to thier SBM(Spot billing machine) machines which is used while taking reading of Electricty Meters.
Joined: Mar 22, 2005
I'll say it once more: Files do not have a concept of a cursor (or pointer, or whatever you want to call it); certainly plain text file don't.
If you want a particular application to do that, then you need to somehow store the relevant information in the file, and make sure that the application that's used to open the file knows how to extract and use that information when displaying it.
JTextComponent (and therefore also its sub classes JTextField, JTextArea and several others) have method setCaretPosition which does exactly what you want - if the file is displayed in an instance one of those classes. You may need to search the contents for the exact position but you should be able to do that with the methods available in the String class.