Avatar of dandeliondream
dandeliondreamFlag for Singapore

asked on 

How to write a string to a file

Hi,

I'm new to Java. I would like to know if I can insert a string to an existing text file. Example if my file consists of the following:
apple nice
orange sour
banana ok

after insertion:
apple is very nice. I eat 5 apples. (Notice i insert "is very" and ". i ate 5 apples" here)
Java

Avatar of undefined
Last Comment
CEHJ
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dandeliondream

ASKER

after insertion:
apple is very nice. I eat 5 apples. (Notice i've inserted "is very" and ". i ate 5 apples." here)
orange sour
banana ok
Avatar of sciuriware
sciuriware

2 solutions:
1) open as RandomAccessFile, read the contents, insert in memory, write back.
2) open as serial text file, read all contents, close, insert in memory, reopen for write, write, close.

You can not just insert text into the middle of a file.

;JOOP!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use the StringList class here: just replace the correct line(s)

https://www.experts-exchange.com/questions/21245044/Replacing-a-line.html
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You don't need to do all that as the class at that link does it for you ;-)
Avatar of sciuriware
sciuriware

CEHJ, I can even do it all in 3 lines of code using my own VirtualEditor class,

but I would like to give a solution in plain JAVA 6.


;JOOP!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Do that then - let's teach code reuse here ;-)
Avatar of dandeliondream

ASKER

thanks i appreciate your help
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:-)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo