Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Save java file

Posted on 2006-06-12
4
Medium Priority
?
2,057 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

783 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