Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Save java file

Posted on 2006-06-12
4
Medium Priority
?
2,053 Views
Last Modified: 2012-08-13
HI,

I would like to pass a string to a java program and then save this to the file system, in a specific location.

It should overwrite any file that is existing but wait if the file is being read.

Can i use the file class?

Thanks.

I am using the file class to read the file class to read the file but don't think i can do this for my output.

0
Comment
Question by:wilflife
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Accepted Solution

by:
colr__ earned 800 total points
ID: 16885052
use the following:

File theFile = new File("fikle/lcoation");
if (theFile.exists())
theFile.delete();
theFile.createNewFile();

This will create a new file, overwriting any file that existsed before.

To write to the file, use the following:

FileOutpStream fo = new FIleOutputStream(theFile);
DataOutputStream do = new DataOutputStrean(fo);

do.writeChars(theStringToWrite);

do.close();
fo.close();

colr__
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 800 total points
ID: 16885064
See here for read text from a file:
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html
This to write:
http://javaalmanac.com/egs/java.io/WriteToFile.html?l=rel
This to append:
http://javaalmanac.com/egs/java.io/AppendToFile.html?l=rel
and so on.

The String could be passed to your program from the args[] of the main method.

Bye, Giant.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16886030
>>> but wait if the file is being read.
That's not possible.

;JOOP!
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 400 total points
ID: 16886137
Make sure you close in finally blocks.
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question