Results 1 to 15 of 15
- 09-17-2010, 09:32 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Fully qualified file name is given as input. Look to see it exists or not
Hi
I would be of great help , if anyone can share their knowledge on how to do this in java
The user enters the full path of the file , (which is considered for processing ) as the input .
How to look to see if the file exists or not.
Thank You in advance .
- 09-17-2010, 10:06 PM #2
See the File class. It has a method to test if a file exists.
- 09-18-2010, 01:24 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
In preparation for JDK 7
File I/O (Featuring NIO.2) (The Java™ Tutorials > Essential Classes > Basic I/O)
- 09-20-2010, 04:04 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
? to give the fullpath and filename as input at the console
Please provide me the statement to enter the filename and its path as the input at the console .
Thanks in advance .
- 09-20-2010, 04:26 PM #5
Read from System.in using a Scanner class method
- 09-20-2010, 09:21 PM #6
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
with this lines, i am able to give the full path and filename at the console .But how to test whether the given file exists or not .
I need to check whether the file exists or not , and if it doesnot , i have to ask the user to enter the fully qualified filename again.?Pls help with the answer.Thank You
Scanner console = new Scanner(System.in);
System.out.println("Enter the fully qualified file name to be processed : ");
String inFile = console.next();
System.out.println("The file is located at :"+inFile);
:
:
try {
FileReader readin = new FileReader(inFile);
Scanner in = new Scanner(readin);
while (in.hasNextLine()){
String input = in.nextLine();
- 09-20-2010, 09:35 PM #7
Use the File class's exists method.I need to check whether the file exists or not
- 09-20-2010, 09:38 PM #8
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
- 09-20-2010, 09:43 PM #9
Sorry, I don't understand what does not work.The exists class doesnot work
Can you show the code with the problem?
Do you know what I mean when I say the File class?
- 09-20-2010, 09:57 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Sir,
I am learning thru ur guidance .
I wrote this code , seeing java sites to get the file with its full path as the console input.For which i wrote
Scanner console = new Scanner(System.in);
System.out.println("Enter the fully qualified file name to be processed : ");
String inFile = console.next();
System.out.println("The file is located at :"+inFile);
It works fine. The string inFile contains the wholepath anf the filename in it.
:
:
Then i started reading each line from the input file , for which i wrote the below lines ...and it works .
try {
FileReader readin = new FileReader(inFile);
Scanner in = new Scanner(readin);
while (in.hasNextLine()){
String input = in.nextLine();
I need to test whether the file exists before i start using the file.
For which ,
after this statement
FileReader readin = new FileReader(inFile);
I trying to use the exists method.But it doesnot come after the dot
inFile.(........?
File class , i am not using here .
Your help will be greatly appreciated ...
- 09-20-2010, 10:02 PM #11
Let me repeat. Use the File class's exists method to determine if the file exits.I need to test whether the file exists before i start using the file.
Go to the API doc, find the File class and read about its Constructor and methods.
- 09-20-2010, 10:39 PM #12
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException
XML Code:Scanner console = new Scanner(System.in); System.out.println("Enter the fully qualified file name to be processed : "); String inFile = console.next(); Scanner in=null; File readin = new File(inFile); System.out.println("The file is located at :"+readin); if(readin.exists()){ System.out.println("Did u enter this loop"); in = new Scanner(readin); }else{ System.out.println("Please enter the fully qualified filename again"); } [COLOR="Red"] while (in.hasNextLine())//ERROR POINTS TO THIS LINE.{[/COLOR] String input = in.nextLine(); // for(String input ;(input = in. != null);){ recCount++; String[] column = input.split(","); int columnCount = column.length; if(columnCount == 4){ // the String to int conversion happens here for each field of the record read.
I need to repeat , till i get the correct filename.
Can U correct me ...so that i can write and understand java coding
- 09-20-2010, 10:43 PM #13
Put the code in a while loop and stay in the loop until you get a good nameI need to repeat , till i get the correct filename.
pseudo code:
boolean gettingFilename = true;
while(gettingFilename) {
get name from user
test if file exists
if file exists, set gettingFilename false
end while loop
- 09-21-2010, 04:09 PM #14
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Thank You Sir ..here is the code i wrote with all your help
XML Code:try { File readin = null; boolean gettingFilename = true; while(gettingFilename) { System.out.println("Enter the fully qualified file name to be processed : "); String inFile = scan.next(); readin = new File(inFile); System.out.println("The file is located at :"+readin); if(readin.exists()){ System.out.println("Did u enter this loop"); gettingFilename = false; }else{ System.out.println("Please enter the fully qualified filename again"); } }
- 09-21-2010, 04:10 PM #15
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Similar Threads
-
Qualified S.C.J.P. 1.5
By ankurrsmh in forum Java CertificationReplies: 1Last Post: 05-24-2010, 04:12 PM -
Checking if file exists in a directory
By achilles in forum New To JavaReplies: 7Last Post: 08-10-2009, 02:43 AM -
File fp = new File(filePath);fp.exists() does not yeild proper result
By ganeshp in forum Advanced JavaReplies: 2Last Post: 04-07-2009, 06:25 AM -
How to check whether file is exists or not
By Java Tip in forum java.ioReplies: 0Last Post: 04-05-2008, 10:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks