Solved

Save java file

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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 476
eclipse buid path vs tomcat lib path 10 49
SequenceInputStream example 3 39
wild fly 8 startup error 2 83
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

710 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