Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


SANTOSH AGRASINGH
FILE CONCEPT
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners


Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does not exist print a message that says so.


Advertisement
View Answers

June 13, 2012 at 12:28 PM


The given code prompt the user to enter the name of a text file to search for. If the name does not have a .txt extension, display an error message. Then search the file in the current directory. If the file does not exist in the current directory then also display an error message.

import java.io.*;
import java.util.*;

class FileHandling
{
    public static void main(String[] args) 
    {
        try{
        Scanner input=new Scanner(System.in);
        System.out.print("Enter name of file: ");
        String fileName=input.nextLine();
        int mid= fileName.lastIndexOf(".");
        String fname=fileName.substring(0,mid);
        String ext=fileName.substring(mid+1,fileName.length());
        File directory=new File(".");
        if(ext.equals("txt")){
            String st=directory.getCanonicalPath();
            String file=st+"/"+fileName;
            File f=new File(file);
            if(!f.exists()){
            System.out.println("File does not exists!!!!");
            }
         }
        else {

            System.out.println("File does not have text extension!!!!!");
        }
        }
        catch(Exception e){}
    }
}


Related Tutorials/Questions & Answers:
FILE CONCEPT
FILE CONCEPT  Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does not exist print
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
Advertisements
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT   Write a program that reads input from the console in the form of a string. When the user enters a word called end, the program should stop reading from the console and print out the values that have been
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  In a random access file, first write the alphabets a,b, c, d till z. Read the file in reverse order and print it on screen
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program that stores the names of four employees in an array of strings. Store the contents of the array in an alphabetical order in a file and display it back on console. Do not use
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program to use a File object and print even numbers from two to ten. Then using RandomAccessFile write numbers from 1 to 5. Then using seek () print only the last 3 digits
FILE HANDLING CONCEPT
FILE HANDLING CONCEPT  Write a program to read and write the details of an employee like year of joining, Department code, salary using some byte stream classes
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program to write the details of an employee to a file. Details such as year of joining, department code, employee name and salary should be included. You must use RandomAccessFile
IO concept
file to another and deletes the old file.   Hi Friend, Try...[] args) throws Exception { File f1=new File("C:/data.txt"); File f2=new File("D:/data.txt"); BufferedReader br=new BufferedReader(new
Java file not found exception
Java file not found exception This section illustrates you the concept of file not found exception. Java provides a powerful concept of Exceptions... by a code. When a file with the specified pathname does not exist, file not found
Java file exception
Java file exception This section illustrates you the concept of file exception... the concept of file exception. Output: java.io.FileNotFoundException... or it is generated by a code.  In the given example, we have specified a file
Explain normalization concept?
Explain normalization concept?  Explain normalization concept
Java file object
Java file object This section demonstrates you the concept of File object... the programming much easier. Here we will discus the operations performed by File object. Using the File object, you can perform several operations. It parses
Concept of subqueires using JSP
Concept of subqueires using JSP  Can I implement the concept of sub queries in JSP page , if yes please tell me how this can be achieved. I have requirement where output of the SQL query should be given as input to the update
session concept - JSP-Servlet
session concept  Hello friends, How can we track unexpectedly closing a window when a jsp project running with session concept. And this tracking should update the log in status in data base
Concept error - WebSevices
Concept error  What is the difference between Joomla And Zend framework. Any one know the answer  Hi friend, Joomla CMS is a multi... for many types of backends, such as memory or a file system. Thanks
Session concept - JSP-Servlet
Session concept  Hai friends, I am doing a jsp project with session concept. If one person is not accessing his logged window for more than 10 minutes it gets automatically log out.Anybody explain me the reason
Java Inheritance Concept.
Java Inheritance Concept.  Yesterday i got confused with following question in an aptitude exam. Question:Class A,B,C have method named doit(). Class B extends Class A,Class C extends Class B.How will you call method doit
Explain the concept of thread priorities in Java.
Explain the concept of thread priorities in Java.  Explain the concept of thread priorities in Java
Write Full concept of abstract class
Write Full concept of abstract class  Abstract class
Java Query(based on swing concept)
Java Query(based on swing concept)  I want a particular submit button to get disabled whenever i write some account number starting with 774 and 775 in a text box of that screen
concept Understatnding problem - Java Beginners
concept Understatnding problem  Even though I have studied in detail inheritance & interfaces in java , I fail to understand "How Interfaces Help in Multiple Inheritance ?" . Pls. Supplement ur ans. with an example. Thanx
Does javaScript have the concept level scope?
Does javaScript have the concept level scope?  Does javaScript have the concept level scope
Java + Timer concept - Java Beginners
concept.  Hi Friend, Try the following code: import java.awt.
Connected Mode and Dissconnect mode coding concept.
Connected Mode and Dissconnect mode coding concept.  Tell Me about Connected Mode and Dissconnect mode coding concept. also give me some syantax
how to call a frame having threading concept
how to call a frame having threading concept   i hav a frame having buttton on it .on click event of the button ,i want to call another frame which implements threading concept. i tried it simply by calling constructor
 

 

 

DMCA.com