This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Setting cursor position in a text file Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Setting cursor position in a text file Java " Watch "Setting cursor position in a text file Java " New topic
Author

Setting cursor position in a text file Java

hareesh Puthala
Greenhorn

Joined: Jan 11, 2010
Posts: 21
Hi All,

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) .


hareesh Puthala
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
Files do not have a concept of a cursor. Are you talking about text that is displayed in a TextArea or some such component? If so, what kind of component is it, exactly?


Android appsImageJ pluginsJava web charts
hareesh Puthala
Greenhorn

Joined: Jan 11, 2010
Posts: 21
Let me explain in detail.

When i call a procedure it returns me a text file.which i give it for download.

Example text File:
-----------------------


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^
*


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.


hareesh Puthala
Greenhorn

Joined: Jan 11, 2010
Posts: 21
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);
raf.seek(raf.length());
raf.writeByte(8);
raf.close();

Please Any suggestions .
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
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.
hareesh Puthala
Greenhorn

Joined: Jan 11, 2010
Posts: 21
Yes it is a pure java Application.

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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting cursor position in a text file Java
 
Similar Threads
Positioning the cursor in an input field
JTextPane
How to goto a particular row /column in JTable
Find current line number in JTextField
Java Keyword input