Results 1 to 8 of 8
Thread: Create and overwrite files
- 09-29-2008, 06:11 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
Create and overwrite files
Hey everyone. I am very new to Java and have been reading a lot about it, but I haven't come across anything that explains what I'm looking to do.
I need to make a Java program that can create new files and overwrite existing ones. I am in the process of building an html page creator and have chosen Java as the programming platform (even though I've never programmed with it). For example I want to create files like test_page.htm, new_page.htm, etc. I also want it to overwrite those pages with new data if they already exist How is this done in Java?
Thanks!
- 09-29-2008, 06:27 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Ok, what you have done up to now?
- 09-29-2008, 09:45 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
I really just started with it and since creating html files would be a major part of the program, I decided to tackle that first. The problem is that I cant find anything in books or online on how to create files.
The closest thing I found was this:
import java.io.*;
public class CreateFile1{
public static void main(String[] args) throws IOException{
File f;
f=new File("myfile.html");
if(!f.exists()){
f.createNewFile();
System.out.println("New file \"myfile.html\" has been created
to the current directory");
}
}
}
I believe this would create the file, but it won't overwrite an existing file.
- 09-29-2008, 11:47 PM #4it won't overwrite an existing file
Can you explain the logic of the code you posted and why you are using it? It looks like the code does NOT call createNewFilel if the file exists. But it doesn't show where you write to the file.
- 09-30-2008, 05:04 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 10-01-2008, 07:51 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
OK, I'll be honest. I didn't write that code. I found it on the internet. :o
It was the only thing I could find after reading a few books and some internet searches. Any ideas on what it should look like?
Thanks!
- 10-01-2008, 08:26 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Ok, that's fine. First be honest. So in that sense people may really help you.
Your code create a file on the specified path, if the file is not exist.
Java Code:if(!f.exists()){
- 10-01-2008, 05:16 PM #8
Similar Threads
-
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 04:20 PM -
Text and image files within jar files
By erhart in forum Advanced JavaReplies: 8Last Post: 01-19-2008, 05:43 AM -
can java.io.File create a list of all files and folders.
By MattStone in forum New To JavaReplies: 20Last Post: 12-17-2007, 04:20 PM -
how to convert mpeg files to .wav files
By christina in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:14 AM -
Create view of files in java-Swing
By Albert in forum AWT / SwingReplies: 1Last Post: 07-06-2007, 07:06 PM
Bookmarks