• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting cursor position in a text file Java

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) .
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
hareesh Puthala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic