Showing posts with label JAVA-File. Show all posts
Showing posts with label JAVA-File. Show all posts

Monday, May 4, 2015

File–Reading

 

File–Reading

 
public class reading2 {
    public static void main(String[] args) {
        Reading obj = new Reading();
        obj.openfile();
        obj.readfile();
        obj.closefile();
        
        
    }
}

 



import java.io.File;
import java.util.Scanner;
 
 
public class Reading {
 
private Scanner x;
    public void openfile()
    {
        try {
            x= new Scanner(new File("Catch.txt"));
        }
    catch(Exception e)
        {
        System.out.println("you got an error");
        }
    }
    public void readfile()
    {
        while (x.hasNext())
        {
            String a = x.next();
            String b = x.next();
            String c = x.next();
            
            System.out.println(a + b + c);
        }
    }
    public void closefile()
    {
        x.close();
    }
    }
 

JAVA–FILE exists or Not !

JAVA–FILE exists or Not !

 

 

 

//main method

import java.io.File;
public class A {
public static void main(String[] args) {
File xFile = new File("D:\\ONLINE\\Dropbox\\CSE - WORLD\\Java File\\tx.txt");
if(xFile.exists())
{
System.out.println(xFile.getName()+" exist");
}
else {
System.out.println("no");
}
}
}


//AnotherClass


import java.util.Formatter;

public class B {

public static void main(String[] args) {
final Formatter x;
try {
x = new Formatter("shadh.txt");
System.out.println("you create a file");
}
catch (Exception e)
{
System.out.println("you got an error");
}
}

}