Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to create a text file?

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to create a text file?

    Hi everyone,
    this is what i tried(i tried a few things hope you would understand):
    File f=new File("c:/FilePractice/text.txt");
    		f.mkdirs();
    and it creates only the folder text.txt.
    i am trying to create a blank txt file int this folder?what is the easiest way to do it?
    i try this one also:
    [CODE]PrintWriter writer = new PrintWriter("test.txt");

    writer.close();
    [CODE]
    and its work but the test.txt file created in sort of default folder in my project folder.
    how can i make it in a folder that i want?
    if it helps i am using windows.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,351
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: How to create a text file?

    Look at the API doc for the PrintWriter class. It has several different constructors. Some of them take a File object.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to create a text file?

    sorry i didnt find anything to make this text come to specific directory

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,351
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: How to create a text file?

    What did you try? Post the code showing what I suggested.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to create a text file?

    "c:/FilePractice/text.txt" is wrong, should it be "\", instead of "/"?

    Let's say you want to create a text file in C:\MyData\Java. You should add: "C:\\MyData\\Java\\textfile.txt" .

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: How to create a text file?

    Quote Originally Posted by PitLv View Post
    "c:/FilePractice/text.txt" is wrong, should it be "\", instead of "/"?

    Let's say you want to create a text file in C:\MyData\Java. You should add: "C:\\MyData\\Java\\textfile.txt" .
    Using this syntax is not wrong - forward slashes are perfectly fine when used in this context.

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 07:14 AM
  2. How do I copy a simple text-file to a new text-file?
    By get703 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 5th, 2012, 10:59 PM
  3. Replies: 8
    Last Post: March 25th, 2011, 03:34 PM
  4. Create Multiple Arrays from a text file
    By chris11kgf in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2011, 01:40 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 04:10 PM