Solved

Save java file

Posted on 2006-06-12
4
2,041 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

735 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