0% found this document useful (0 votes)
13 views

Obey

The document provides Java code examples for exception handling, file writing, reading, and copying. It demonstrates how to handle division by zero errors, write user input to a file, read from a file, and copy file contents to a new file. Each section includes sample output to illustrate the functionality of the code.

Uploaded by

akashjyothi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Obey

The document provides Java code examples for exception handling, file writing, reading, and copying. It demonstrates how to handle division by zero errors, write user input to a file, read from a file, and copy file contents to a new file. Each section includes sample output to illustrate the functionality of the code.

Uploaded by

akashjyothi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

// EXCEPTION HANDLING :

import java.util.*;
public class exception{
public static void main(String args[]){
try{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first digit : ");
int a=sc.nextInt();
System.out.print("Enter the second digit : ");
int b=sc.nextInt();
int div=a/b;
System.out.println("The result is : "+div);
}
catch(Exception e){
System.out.println(e);
}}}

OUTPUT :

Enter the first digit : 6


Enter the second digit : 0
java.lang.ArithmeticException: / by zero

Enter the first digit : 6


Enter the second digit : 3
The result is : 2
// FILE WRITE :

import java.util.*;
import java.io.*;
public class filewrite{
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("sample.txt");
Scanner sc= new Scanner(System.in);
System.out.println("Enter the data : ");
String data=sc.nextLine();
fw.write(data);
fw.close();
}
catch(Exception e){
System.out.println(e);
}
}
}

// OUTPUT :

Enter the data : Java is a good programming language.

// The text ' Java is a good programming language.' was written into the file,
'sample.txt'
//FILE READING :

import java.util.*;
import java.io.*;
public class Fileread{
public static void main(String args[]){
try{
File ob=new File("sample.txt");
Scanner sc= new Scanner(ob);
while(sc.hasNextLine()){
String data=sc.nextLine();
System.out.println(data);
}
sc.close();
}
catch (Exception e){
System.out.println(e);
}
}
}

//OUTPUT :

Java is a good programming language.

//The contents from the file , sample.txt is displayed.


//FILE COPY :

import java.util.*;
import java.io.*;
public class filecopy{
public static void main(String args[]){
try{
File ob= new File("sample.txt");
Scanner sc= new Scanner(ob);
FileWriter fw = new FileWriter("Samplewrite.txt");
while(sc.hasNextLine()){
String data=sc.nextLine();
fw.write(data);
}
sc.close();
fw.close();
}
catch(Exception e){System.out.println(e);}
}
}

//OUTPUT :

Java is a good programming language.


//A new file Samplewrite.txt is created and the contents from the file sample.txt
is copied into it.

You might also like