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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

615 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