FILE HANDLING CONCEPT

Ads
 

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

View Answers

June 13, 2012 at 12:27 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){}
    }
}

June 16, 2012 at 2:44 PM

//If file is present in the current directory then cursor will move downwards,otherwise it will show error.

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

public class Ch13Lu1Ex1 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub


    try{
        //take input from keyboard
        Scanner in = new Scanner(System.in);
        System.out.print("Enter name of file: ");

        String fileName = in.nextLine();
        int mid = fileName.lastIndexOf(".");


        String ext = fileName.substring(mid+1,fileName.length());

        File directory=new File(".");
        //System.out.println("File directory: "+File directory);
          if(ext.equals("txt")){
          String st=directory.getCanonicalPath();
          //System.out.println("st: "+st);
          String file=st+"\\"+fileName;
          //System.out.println("file: "+file);
          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){}

}

}


June 16, 2012 at 2:52 PM

//If file is present in the current directory then cursor will move downwards,otherwise it will show error.


June 16, 2012 at 2:54 PM

Here is the Ans:-

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

public class Ch13Lu1Ex1 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub


    try{
        //take input from keyboard
        Scanner in = new Scanner(System.in);
        System.out.print("Enter name of file: ");

        String fileName = in.nextLine();
        int mid = fileName.lastIndexOf(".");


        String ext = fileName.substring(mid+1,fileName.length());

        File directory=new File(".");
        //System.out.println("File directory: "+File directory);
          if(ext.equals("txt")){
          String st=directory.getCanonicalPath();
          //System.out.println("st: "+st);
          String file=st+"\\"+fileName;
          //System.out.println("file: "+file);
          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){}

}

}

Ads









Related Tutorials/Questions & Answers:
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
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  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
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
File Handling
File Handling  how we do file handling of parsed XML code bu DOM in java
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
File handling  how do i search for an occurance of a word in a file... in a file import java.io.*; import java.util.*; class FileOperations{ public static void main(String[] args)throws Exception{ int count=0; File f
File handling in Java
File handling in Java  Hi, Can any one tell me the basics of File handling in Java? Thanks   Hi, File handling is very important... handling features in their api. You can read more at File Handling in Java
File Handling in Java
File Handling in Java  Hi, While opening a file in Java developers... was trying to find some tutorials about handling file in Java on net. I have also seen... are the use of these classes? Is there example code for explaining the File Handling
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
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
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
Excel file Handling in Java
Excel file Handling in Java  Hello Sir, I am new to Java, I have Started Java Core by myself. I want to make a project which include 3-4 Excel file to handle.Please explain how to read and write Excel file in Java.Is it tough
java file handling
java file handling  enter string from keyboard and then read a file if this string is present in file then print message already exists else write this string to file
java file handling
java file handling  program to get values from textbox array and the write them to text files
java file handling
java file handling  Hi friends, My situation is i have file named "sample.conf" which contains more IP Adressess.Eg: the file contains "IP=255.255.255.255" at some lines in the file. what i need to do is. i need to search
File Handling In Java
File Handling in Java In this section we will discuss about the file handling in Java. This section describes you about how to perform file handling in Java. In this section you will read what is file handling in Java, how to perform
File Handling Classes in Java
In this section, you will get to know about file handling classes in java to handle the file input output operations
Java File Handling Test Results
Java File Handling Test Results   Is somebody able to help me with this Write a program to read each name from the file and prompt the user... FileNotFoundException { new File("team.html").delete(); Scanner scan = new
Java File Handling
File Handling in Java       File Handling in Java For file handling... In a file system, it gets input bytes from a file. FileOutputStream
exception handling
exception handling  could you please tell me detail the concept of exception handling
unicode handling in java
unicode handling in java  reading a unicode text file in java
exception handling
exception handling  explain about exception handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling

Ads