Solved

Save java file

Posted on 2006-06-12
4
2,008 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 200 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 200 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:mayankeagle
mayankeagle earned 100 total points
ID: 16886137
Make sure you close in finally blocks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 39
sumHeights2  challenge 7 78
network + 7 73
Python Assistance 7 36
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now