Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

creating/copying files..

Thread view: 
Frances - 28 Dec 2005 20:48 GMT
Pls, what is best way to create or copy files?

in java.io.file  I see under the constructors:

    Creates a new File instance from a parent abstract pathname and a
child pathname string.

what is a file "instance"?

  I mean if I want to create an html file, then write into it, how
would I do that?

(FileWriter is just for writing into files, not creating them.. is this
right?)  thanks..

Frances
Roedy Green - 28 Dec 2005 21:49 GMT
>   I mean if I want to create an html file, then write into it, how
>would I do that?

see http://mindprod.com/applets/fileio.html

File lets you play with the file name, check for existence, delete.
But it does not do anything with contents.

You need say a FileWriter FileOutputStream or such.  
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Frances - 28 Dec 2005 22:12 GMT
>>  I mean if I want to create an html file, then write into it, how
>>would I do that?
[quoted text clipped - 5 lines]
>
> You need say a FileWriter FileOutputStream or such.  

thank you Roedy, you mean to CREATE files I need FileWriter and
FileOutputStream?  it says in docs under FileWriter:

  Convenience class for writing character files.

(still don't know if by "writing" is meant creating a new file or just
adding content to an existing file..)

this guy  http://javaalmanac.com/egs/java.io/CreateFile.html?l=rel
seems to be implying you CAN create new files w/class File..  thank you
Roedy..

Frances
Roedy Green - 28 Dec 2005 22:26 GMT
>(still don't know if by "writing" is meant creating a new file or just
>adding content to an existing file..)

File by itself won't let you create an empty file.  You can create
with FileOutputStream and writing no bytes. Just close.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Carl - 28 Dec 2005 22:47 GMT
> File by itself won't let you create an empty file.  You can create
> with FileOutputStream and writing no bytes. Just close.

Are you sure about that? It seems to work just fine for me.

//code
import java.io.File;
import java.io.IOException;

public class Test {
   
    private void makeFile(){
       
        File file = new File(System.getProperty("user.home"),
                            "testfile.txt");
       
        System.out.println("Checking existence of file: "
                            + file.toString());
        if (file.exists()) {
            System.out.println("Found!");
        } else {
            System.out.println("File not found!" +
                                "\nCreating file.");
           
            try {
                if (file.createNewFile()) {
                    System.out.println("File created!");
                }
            } catch (IOException e) {
                System.out.println("Unable to create file!");
                e.printStackTrace();
            }
        }
       
    }
   
   
    public static void main(String[] args){
        Test t = new Test();
        t.makeFile();
        System.out.println("Done.");
        System.exit(0);
    }

}
// end code

Carl.
Roedy Green - 29 Dec 2005 01:21 GMT
>Are you sure about that? It seems to work just fine for me.

All I am saying in that new File does not create a file. It is just a
handle to a file.  createNewFile or createTempFile will create a file
though if all you want is an empty file.  If you plan to write
something in it, there is no point in createNewFile. The file gets
created as needed as a side effect of writing.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Carl - 29 Dec 2005 01:26 GMT
>>Are you sure about that? It seems to work just fine for me.
>
[quoted text clipped - 3 lines]
> something in it, there is no point in createNewFile. The file gets
> created as needed as a side effect of writing.

Roedy,

Ok, I understand. I was confused by your previous post that said "You
can create with FileOutputStream and writing no bytes. Just close." that
the goal was to create an empty file.

Cheers,
Carl.
Frances - 29 Dec 2005 02:13 GMT
>>> Are you sure about that? It seems to work just fine for me.
>>
[quoted text clipped - 12 lines]
> Cheers,
> Carl.

actually the goal is not to create an empty file, but a file that I will
be writing into, and will also need to copy, modify files...  Roedy, as
a "side effect of writing?" writing where??  :)  thanks..  Frances
Roedy Green - 29 Dec 2005 07:51 GMT
>actually the goal is not to create an empty file, but a file that I will
>be writing into, and will also need to copy, modify files...  Roedy, as
>a "side effect of writing?" writing where??  :)  thanks..  Frances

If you look at the generated sample code at
http://mindprod.com/applets/fileio.html to write a file, as a side
effect, if the file does not already exist, it will create it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ricky Clarkson - 29 Dec 2005 11:28 GMT
Frances,

Roedy gave you a tutorial to read.

Also see the Java io tutorial:
http://www.google.com/search?q=java%20io%20tutorial

I'm sure between those and a little bit of extra googling you'll be
able to help yourself, but obviously feel free to come back and ask
specific questions.  At the minute your question is so vague that it is
best answered by tutorials.

Cheers.
Frances - 29 Dec 2005 14:26 GMT
> Frances,
>
[quoted text clipped - 9 lines]
>
> Cheers.

yes I have been reading the relevant pgs @ java.sun tutorial and my copy
of O'Reilly "Learning Java"...  I like to ask questions here also...
sometimes if you see three different ways of explaining someting it
becomes clearer...  thanks all for yr help..  Frances
Roedy Green - 29 Dec 2005 07:50 GMT
>Ok, I understand. I was confused by your previous post that said "You
>can create with FileOutputStream and writing no bytes. Just close." that
>the goal was to create an empty file.

That will work too.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Mark Thomas - 28 Dec 2005 23:24 GMT
Frances

You need to realise that there is a difference between creating a File
object, which is what the constructor does, and creating an actual File
on your computer.
Very simply, if I write:

    File myFile = new File("sample.txt");

I now have a file object in memory which will eventually 'represent' a
file on the hard disc.  This is the file "instance" you mention.  I can
now check to see if there already is a file of this name by using "if
(myFile.exists())...".  I can create an actual file on the hard disc by
calling:

    myFile.createNewFile();

Now the picture is complete.  We have an instance of the File class in
memory which is acting as a proxy for the real file on disc.  If You now
create, say a FileWriter passing this File object to the constructor,
you can now start writing to the real file.

Hope that helps.

Mark

> Pls, what is best way to create or copy files?
>
[quoted text clipped - 12 lines]
>
> Frances
Frances - 29 Dec 2005 02:05 GMT
> Frances
>
[quoted text clipped - 21 lines]
>
> Mark

Mark, that helps A LOT, thank you..  this is precisely the kind of
explanation I needed, it's very clear now..  create File Obj then
pass this File Obj to constructor when creating the file...

:)  thanks again....  Frances
Rhino - 29 Dec 2005 04:55 GMT
>> Frances
>>
[quoted text clipped - 27 lines]
>
> :)  thanks again....  Frances

You may also benefit from the section on I/O in the Java Tutorial. Here's a
link to that chapter:
http://java.sun.com/docs/books/tutorial/essential/io/index.html.

Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.