National University of Modern Languages
National University of Modern Languages
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
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