0% found this document useful (0 votes)
14 views8 pages

Ex No 8,9,10

The document outlines multiple Java programs, including one for file operations that checks file existence, readability, writability, type, and size, and another using generics to find maximum values from various data types. Additionally, it includes a JavaFX example demonstrating button and menu functionalities. Each program is accompanied by a brief description of its aim and procedure, concluding with successful execution results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Ex No 8,9,10

The document outlines multiple Java programs, including one for file operations that checks file existence, readability, writability, type, and size, and another using generics to find maximum values from various data types. Additionally, it includes a JavaFX example demonstrating button and menu functionalities. Each program is accompanied by a brief description of its aim and procedure, concluding with successful execution results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

8.

PROGRAM FOR PERFORM FILE OPERATION

AIM

To write a java program that reads a file name from the user, displays information about whether the file

exists, whether the file is readable, or writable, the type of file and the length of the file in bytes.

PROCEDURE

1.Create a class filedemo. Get the file name from the user .

2.Use the file functions and display the information about the file.

3.getName() displays the name of the file.

4.getPath() diplays the path name of the file.

5.getParent () -This method returns the pathname string of this abstract pathname’s parent, or

null if this pathname does not name a parent directory.

6.exists() – Checks whether the file exists or not.

7. canRead()-This method is basically a check if the file can be read.

8. canWrite()-verifies whether the application can write to the file.

9. isDirectory() – displays whether it is a directory or not.

10. isFile() – displays whether it is a file or not.

11. lastmodified() – displays the last modified information.

12.length()- displays the size of the file.

13. delete() – deletes the file

14.Invoke the predefined functions abd display the iformation about the file.

PROGRAM

import java.io.*;

import java.util.*;

import java.text.SimpleDateFormat;

class FileDemo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the file name: ");


String filename = scanner.nextLine();

File file = new File(filename);

System.out.println("\n*****************");

System.out.println("FILE INFORMATION");

System.out.println("*****************");

System.out.println("Name of the file: " + file.getName());

System.out.println("Path of the file: " + file.getPath());

System.out.println("Parent directory: " + file.getParent());

if (file.exists()) {

System.out.println("The file exists.");

} else {

System.out.println("The file does not exist.");

if (file.canRead()) {

System.out.println("The file can be read.");

} else {

System.out.println("The file cannot be read.");

if (file.canWrite()) {

System.out.println("Write operation is permitted.");

} else {

System.out.println("Write operation is not permitted.");

}
if (file.isDirectory()) {

System.out.println("It is a directory.");

} else {

System.out.println("Not a directory.");

if (file.isFile()) {

System.out.println("It is a file.");

} else {

System.out.println("Not a file.");

if (file.exists()) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println("File last modified: " + sdf.format(new Date(file.lastModified())));

System.out.println("Length of the file: " + file.length() + " bytes");

System.out.print("\nDo you want to delete this file? (yes/no): ");

String response = scanner.nextLine();

if (response.equalsIgnoreCase("yes")) {

if (file.delete()) {

System.out.println("File deleted successfully.");

} else {

System.out.println("File deletion failed.");

} else {
System.out.println("File not deleted.");

scanner.close();

Result:

Thus the java program that reads a file name from the user, displays information about whether the file

exists, whether the file is readable, or writable, the type of file and the length of the file in byte.

9. PROGRAM TO FIND THE MAXIMUM VALUE FROM THE GIVEN

TYPE OF ELEMENTS USING GENERICS

AIM

To write a java program to find the maximum value from the given type of elements using a

generic function.

PROCEDURE

1.Create a class Myclass to implement generic class and generic methods.

2.Get the set of the values belonging to specific data type.

3.Create the objects of the class to hold integer,character and double values.

4.Create the method to compare the values and find the maximum value stored in the array.

5.Invoke the method with integer, character or double values . The output will be displayed based on

the data type passed to the method.

PROGRAM

class MyClass<T extends Comparable<T>> {

T[] vals;

MyClass(T[] o) {
vals = o;

public T min() {

T v = vals[0];

for (int i = 1; i < vals.length; i++)

if (vals[i].compareTo(v) < 0)

v = vals[i];

return v;

public T max() {

T v = vals[0];

for (int i = 1; i < vals.length; i++)

if (vals[i].compareTo(v) > 0)

v = vals[i];

return v;

class GenDemo {

public static void main(String args[]) {

Integer inums[] = {10, 2, 5, 4, 6, 1};

Character chs[] = {'v', 'p', 's', 'a', 'n', 'h'};

Double d[] = {20.2, 45.4, 71.6, 88.3, 54.6, 10.4};

MyClass<Integer> iob = new MyClass<>(inums);

MyClass<Character> cob = new MyClass<>(chs);

MyClass<Double> dob = new MyClass<>(d);


System.out.println("Max value in inums: " + iob.max());

System.out.println("Min value in inums: " + iob.min());

System.out.println("Max value in chs: " + cob.max());

System.out.println("Min value in chs: " + cob.min());

System.out.println("Max value in d: " + dob.max());

System.out.println("Min value in d: " + dob.min());

Result:

Thus the java program to find the maximum value from the given type of elements using a

generic function is executed successfully.

Exercise :10: java fx Controls

Aim:

Develop applications using JavaFX controls, layouts and menus.

Algorithm:

Import the packages support for the javafx controls

Create the instance for frame anf textfields and necessary layouts

Declare the function for listener events

Create the list for menus for the menu listener

Include the frame setup in the coding

Complete the program with the function calling and execute

Program:

package buttonexample3;

import java.awt.*;

import java.awt.event.*;

public class ButtonExample3 {


public static void main(String[] args) {

Frame f = new Frame("Button Example");

final TextField tf=new TextField();

tf.setBounds(50,50, 150,20);

Button b=new Button("Click Here");

b.setBounds(50,100,60,30);

b.addActionListener(new ActionListener() {

public void actionPerformed (ActionEvent e) {

tf.setText("Welcome to Javatpoint.");

});

f.add(b);

f.add(tf);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

10.b. menus

import javax.swing.*;

class MenuExample

JMenu menu, submenu;

JMenuItem i1, i2, i3, i4, i5;

MenuExample(){

JFrame f= new JFrame("Menu and MenuItem Example");

JMenuBar mb=new JMenuBar();

menu=new JMenu("Menu");

submenu=new JMenu("Sub Menu");


i1=new JMenuItem("Item 1");

i2=new JMenuItem("Item 2");

i3=new JMenuItem("Item 3");

i4=new JMenuItem("Item 4");

i5=new JMenuItem("Item 5");

menu.add(i1); menu.add(i2); menu.add(i3);

submenu.add(i4); submenu.add(i5);

menu.add(submenu);

mb.add(menu);

f.setJMenuBar(mb);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String args[])

new MenuExample();

}}

Result:

Thus the applications using JavaFX controls, layouts and menus has been executed.

You might also like