0% found this document useful (0 votes)
44 views4 pages

National University of Modern Languages

Write a Java Program. It should provide the following functionality. Read file, Write File, Check for File existence, File readability, Type of file, Length of File.

Uploaded by

Arman Afaq
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)
44 views4 pages

National University of Modern Languages

Write a Java Program. It should provide the following functionality. Read file, Write File, Check for File existence, File readability, Type of file, Length of File.

Uploaded by

Arman Afaq
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/ 4

NATIONAL UNIVERSITY OF MODERN LANGUAGES

Submitted by: Shanila Afaq (11769)


Submitted to: Miss Bushra khan
Class: BSSE 2 A
Assignment no: 3

NATIONAL UNIVERSITY OF MODERN LANGUAGES


ISLAMABAD

7th, December, 2018


Question 1
Create a class StringtoInt. It should have a function StringtoInt(). StringtoIntconversion()
should take values of string type and should convert these vales to int type. Write a try and
cath block that should monitor and catch exceptions if string to int conversion in invalid.

Program:
package javaapplication42;
import java.util.Scanner;
public class StringToInt{
static String st;
public static void Stringtoint(){
Scanner s=new Scanner(System.in);
System.out.println("ENTER A STRING");
st=s.nextLine();
}
public static void stringtointconversion(){
int i=Integer.parseInt(st);
System.out.println(i);
}
public static void main(String args[]){
try{ Stringtoint();
stringtointconversion();
}catch(Exception e){
System.out.println("can't converted"+e);
}}}
Output:

2|Page
Question no 2

Write a Java Program. It should provide the following functionality.


Read file, Write File, Check for File existence, File readability, Type of file, Length of File.

Program:
package javaapplication43;
import java.io.*;
public class JavaApplication43 {
private static final String FILENAME = "D:\\SHANILA\\2nd semester\\object "
+ "oriented programming\\CLASS\\javaaa.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}} catch (IOException e) {
e.printStackTrace();
} finally { try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();}}
try{
FileWriter fw=new FileWriter(FILENAME,true);
fw.write("pakistan zindaabaad");
fw.close();
}catch(IOException e)
{e.printStackTrace();}
System.out.println("Success...");
System.out.println("EXISTENCE OF FILE");
File f = new File(FILENAME);
if(f.exists() && f.isFile()) {
System.out.println("file exists");}
else{System.out.println("file doesn't exists");}
System.out.println("READIBILITY OF FILE");
if(f.canRead()){//RETURN TRUE R FALSE ,BOOLEAN METHOD,READABLE
TRUE,not readable false

3|Page
System.out.println("File " + f.getPath() +" can be read");}
else
{ System.out.println("File " + f.getPath() +" can not be read");}
System.out.println("SIZE OF FILE");
System.out.println(f.length()+" bytes");
System.out.println("TYPE OF FILE");
String fileName = f.getPath();
if(fileName.endsWith(".txt")){//chcks wether the string ends with the specified suffix r not…
boolean
System.out.print("Text file");
}
else if(fileName.endsWith(".odt"))
{
System.out.print("Open document file");
}
else
{
System.out.print("Other file");
} }}
Output:

4|Page

You might also like