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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 141
bitbucket vs gitbucket 3 57
eclipse formatting 6 73
Java Loop 6 50
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

867 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

15 Experts available now in Live!

Get 1:1 Help Now