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 8 of 8

Thread: Urgent - File exist nor working

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Urgent - File exist nor working

    this.fullName = "userlogs/" + this.userLogId;
    		if(!File.exists(this.fullName)
    		{
    			File userLogId = new File("userlogs/" + this.userLogId);
    		}

    I want to check if this file exists, if it doesn't create it. userLogId is a variable
    I know this is wrong and i'v tried just about anything i know but it just keeps giving me errors.

    the name of the file will be what is stored in the String fullName variable. I am trying to use relative path to show java where the file is located which is inside userlogs folder.

    can anyone help? It's quite urgent for me to find out as soon as i can how to get this working. Matter of pass and fail


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Urgent - File exist nor working

    Take a look at this example. You will be able to adapt it to suit your needs.

    		String fullName = "C:\\JavaPF\\input.txt";		
    		File file = new File(fullName);
     
    		if(file.exists()){
    			System.out.println("File exists");
    		}	
    		else{
    			System.out.println("File doesn't exist");
    			// write file
    		}
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Urgent - File exist nor working

    I just realized how wrong I am about what i had written here before. Here is the new question: How do i get Java to create a new text file for me? and what happens when i try to create a file that already exists?

    File file = new File(fullName);
    this will only create a file object, but i want it to create a file .txt file in a folder i specified.
    Last edited by Bagzli; March 1st, 2011 at 07:21 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Urgent - File exist nor working

    Have a look at the API for File...the file.create() method will create the file. To write contents of the file, see Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Urgent - File exist nor working

    thats exactly what i'm looking for except i don't know how to get it to work. I have read the tutorial it does not make any sense to me, its using some buffer. I just need to create a file if it's not there so please can one of you write me the line of code where I can see how a file is created?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Urgent - File exist nor working

    I will give you some code on how to create the file, but I do recommend you become familiar with the API and tutorial links I posted above, going through the examples and playing with them to understand what is going on so you can use those code examples in your programs - this process is essential to learn the language, together with problem solving in the context of the language.
    File file = new File(myFilePath);
    if ( !file.exists() ){
        file.create();
    }else{
    ///file already exists...perhaps you wish to warn the user that the file exists
    }
    If you wish to write to the file, you must create a Writer, for example creating a BufferedWriter
    BufferedWriter writer = null;
    try{
        writer = new BufferedWriter(new FileWriter(file) );
        //write to file
    }catch(Exception e){
        //deal with exception
    }finally{
        if ( writer != null ){
            try{writer.close();}catch(Exception e){}
        }
    }
    Last edited by copeg; March 1st, 2011 at 10:42 PM.

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Urgent - File exist nor working

    can you explain me what this whole buffered thing is? I used PrintWriter to get my code finished.

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Urgent - File exist nor working

    Also see - http://www.javaprogrammingforums.com...sing-java.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Jar Executable File Not Working
    By Kimimaru in forum Java Theory & Questions
    Replies: 6
    Last Post: October 15th, 2010, 09:32 PM
  2. The Problem With My Code Is That It Doesn't Exist Yet. (Help w/ Retarded Teacher)
    By DylanWilliams92 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 08:10 PM
  3. org.apache.derby does not exist?
    By disclaimer in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 30th, 2010, 04:58 PM
  4. How to handle quadratic roots that don't exist?
    By kairojya in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2009, 12:21 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM