This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

How to write a string in a file?  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to write "Hello World!" in a file name x.txt. Previously the file contained "I am Ananda". I want Hello World to be in the exact next line. How do I do that? I am using RAF and writeUTF but it doesn't go to next line and put a "?" before Hello. I know about writeUTF, why it's writing the ? but wondering if there any other way to do this.
Please help.
- Ananda.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to seek() to the end of the file, then write a "\r\n", then write the string you wish to append to the file. When you open the text file in Notepad, the new next will be on a new line.
 
Ananda Kashyap
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
Thanks for the advice but which function should I use? I used writeUTF(String s) for "Hello World!" but it puts a "?" before Hello.. and what about "\r\n". I am trying with writeChar('\r') as well as writeChars("\r\n"); don't work.
- Ananda.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Lets see if this helps:
{
FileOutputStream f= new FileOutputStream("c:\\x.txt" , true);
/*Note: append is true*/
PrintStream p= new PrintStream(f);
p.println();
p.println("Hey its Workin");
}

 
What's gotten into you? Could it be this tiny ad?
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!