Save java file

Posted on 2006-06-12
Last Modified: 2012-08-13

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?


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.

Question by:wilflife
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

Accepted Solution

colr__ earned 200 total points
ID: 16885052
use the following:

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

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);



LVL 12

Assisted Solution

Giant2 earned 200 total points
ID: 16885064
See here for read text from a file:
This to write:
This to append:
and so on.

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

Bye, Giant.
LVL 24

Expert Comment

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

LVL 30

Assisted Solution

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

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