Urgent - File exist nor working
Code Java:
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 :-s
Re: Urgent - File exist nor working
Take a look at this example. You will be able to adapt it to suit your needs.
Code Java:
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
}
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?
this will only create a file object, but i want it to create a file .txt file in a folder i specified.
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)
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?
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.
Code Java:
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
Re: Urgent - File exist nor working
can you explain me what this whole buffered thing is? I used PrintWriter to get my code finished.
Re: Urgent - File exist nor working