Results 1 to 8 of 8
  1. #1
    wmrwl is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default 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!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    wmrwl is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    it won't overwrite an existing file
    If there is an error message copy and paste it here.

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by wmrwl View Post
    I believe this would create the file, but it won't overwrite an existing file.
    Yes it is, your code not overwrite the *.html file. Carefully check what you have done in your code. You check that a file is exist or not. If the file is not exist you create a new one. What you have done there if the file is exist? Nothing. No error messages either.

  6. #6
    wmrwl is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    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!

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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()){
    Say that file is exist at the time you try to create a file. Nothing is happen according to your code.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    What will you do differently if the file exists and if it does not exist?
    You need to decide that and a LOT more BEFORE writing ANY code.

    Design first and then code!!!

Similar Threads

  1. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  2. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 AM
  3. Replies: 20
    Last Post: 12-17-2007, 03:20 PM
  4. how to convert mpeg files to .wav files
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:14 AM
  5. Create view of files in java-Swing
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2007, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •