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 a message that says so.

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
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... the concept of file not found exception. Output
Java file exception
Java file exception This section illustrates you the concept of file exception... to understand the concept of file exception. Output:ADS_TO_REPLACE_2... or it is generated by a code.  In the given example, we have specified a file
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
Explain normalization concept?
Explain normalization concept?  Explain normalization concept
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
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
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
Explain the concept of thread priorities in Java.
Explain the concept of thread priorities in Java.  Explain the concept of thread priorities in Java
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
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
Write Full concept of abstract class
Write Full concept of abstract class  Abstract class
ModuleNotFoundError: No module named 'concept-formation'
ModuleNotFoundError: No module named 'concept-formation'  Hi, My... named 'concept-formation' How to remove the ModuleNotFoundError: No module named 'concept-formation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'concept-formation'
ModuleNotFoundError: No module named 'concept-formation'  Hi, My... named 'concept-formation' How to remove the ModuleNotFoundError: No module named 'concept-formation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'concept-formation'
ModuleNotFoundError: No module named 'concept-formation'  Hi, My... named 'concept-formation' How to remove the ModuleNotFoundError: No module named 'concept-formation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'concept-py'
ModuleNotFoundError: No module named 'concept-py'  Hi, My Python... 'concept-py' How to remove the ModuleNotFoundError: No module named 'concept-py' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'concept-formation'
ModuleNotFoundError: No module named 'concept-formation'  Hi, My... named 'concept-formation' How to remove the ModuleNotFoundError: No module named 'concept-formation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'concept-py'
ModuleNotFoundError: No module named 'concept-py'  Hi, My Python... 'concept-py' How to remove the ModuleNotFoundError: No module named 'concept-py' error? Thanks   Hi, In your python environment you
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
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
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.
File
File  How to create .Exe file in jana
file
file  how can copy the information in the file to the string?   Store file data into String
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
file
file  Could anyone please help me to write a code that does the following: Opens a file named MyName.txt, reads the first line from the file and displays it, and then closes the file. Thank you so much
file
file  how can add the information to the graphic file
file
file  Hi,what can delet some of the information of the file
file
file  Hi,how can delet some of the information of the file
File
File   How to find all files in our local computer using java
FILE
FILE  There is a file named Name.txt that contains information related to appliances.I want to read the contents of this file and I used the code... args[]) { try { File file=new File("Name.txt"); FileReader f=new FileReader(file
file
file  hi i want read special line of file. what am i doing ? I write... read data from file. please help me! void maketree(BtNode root,int i) throws... at the following link: Read specific line from file
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
Java - Read file Applet
the concept of the reading file from an applet. This program illustrates you how an applet can read the content from the given file. In this program we passes... Java - Read file Applet     
about the concept - Design concepts & design patterns
about the concept  sir,please explain me how should i interpret the concept "object oriented technology" and how it is differ from "structured langauge".  Object Oriented Language http://www.roseindia.net/java/master
OOPS Concept Abstraction with example - Java Beginners
OOPS Concept Abstraction with example  I am new to java. In java OOPS concept Abstraction means abstract or something. Please explain it with one example  Hi Friend, The process of abstraction in Java is used
Multiarray concept
Thread concept
programming concept
serialization concept
oop concept

Ads