Friday, 27 April 2018

 File Class Concept in Input Output....................(java.io)

package iojava;

import java.io.File;
import java.io.IOException;

public class TestFile {
public static void main(String[] args) throws IOException {
File f=new File("abcd.txt");
System.out.println(f.exists());
f.createNewFile();
System.out.println(f.exists());


}

}

===============================================================================================
o/p:--

ist run:------
false
true

2nd run:------------
true
true

===============================================================================================

package iojava;

import java.io.File;

public class TestFolder {
public static void main(String[] args) {
File f=new File("govind123");
System.out.println(f.exists());
f.mkdir();
System.out.println(f.exists());
}

}
===============================================================================================
o/p:--

ist run:------
false
true

2nd run:------------
true
true

===============================================================================================

wap  to print the names of all files and subdirectories present in e:\\books........................

package iojava;

import java.io.File;

public class AllFiles {
public static void main(String[] args) {
int count=0;
File f=new File("e:\\books");
String[] l = f.list();
for(String f1:l)
{
count++;
System.out.println(f1);
}
System.out.println("Total number of file ="+count);
}

}
================================================================================================

o/p:---------------

A.class
A.java
Aimpoint1.class
Aimpoint1.java
ArrayListDemo.java
ArrayTest.java
AverageCMD.class
AverageCMD.java
B.class
BufferedReaderDemo.java
CheckJvm.class
CheckJvm.java
Child.class
CN
CompCompDemo1.class
CompCompDemo1.java
condition.c
CSO
DBMS
Demo.class
Demo.java
Digital
DS
Employee.class
Employee.java
Employee1.class
Employee1.java
EnumTest.java
eula.1028.txt
eula.1031.txt
eula.1033.txt
eula.1036.txt
eula.1040.txt
eula.1041.txt
eula.1042.txt
eula.2052.txt
eula.3082.txt
even.txt
g.jpg
GetCurrentDateAndTime.class
GetCurrentDateAndTime.java
globdata.ini
Hello.vb
HelloWorld.cs
helloworld1.cs
inheritance_demo 1.java
install.exe
install.ini
install.res.1028.dll
install.res.1031.dll
install.res.1033.dll
install.res.1036.dll
install.res.1040.dll
install.res.1041.dll
install.res.1042.dll
install.res.2052.dll
install.res.3082.dll
JAVA-CLASS - Copy
javaDatabase - Copy
JAVANOTES - Copy
LeftClass.java
Maths
MyComparator1.class
MyComparator1.java
Notepad.class
number.txt
odd.txt
OS
overload.class
Overload_methods.class
Overload_methods.java
Overload_methods1.class
Overload_methods1.java
Parent.class
ReadInt.class
ReadInt.java
ReadInt1.class
ReadInt1.java
src.zip
Student.class
Student.java
StudentCMD.class
StudentCMD.java
TableCMD.class
TableCMD.java
Test.class
Test.java
Test1.class
Test1.java
Test1Exception.class
Test1Exception.java
TreeSetDemo.class
TreeSetDemo.java
vcredist.bmp
VC_RED.cab
VC_RED.MSI
WriteInt.class
WriteInt.java
Total number of file =98

=================================================================================================

//wap to display only file name  e:\\Books..................

package iojava;

import java.io.File;

public class FileNameOnly {

public static void main(String[] args) {
int count=0;
File f=new File("e:\\Books");
String[] l = f.list();
for(String s1:l)
{
File f1=new File(f,s1);
if(f1.isFile())
{
count++;
System.out.println(s1);
}

}
System.out.println("The Total Number Of file="+count);

}

}
================================================================================================

o/p:--------------
A.class
A.java
Aimpoint1.class
Aimpoint1.java
ArrayListDemo.java
ArrayTest.java
AverageCMD.class
AverageCMD.java
B.class
BufferedReaderDemo.java
CheckJvm.class
CheckJvm.java
Child.class
CompCompDemo1.class
CompCompDemo1.java
condition.c
Demo.class
Demo.java
Employee.class
Employee.java
Employee1.class
Employee1.java
EnumTest.java
eula.1028.txt
eula.1031.txt
eula.1033.txt
eula.1036.txt
eula.1040.txt
eula.1041.txt
eula.1042.txt
eula.2052.txt
eula.3082.txt
even.txt
g.jpg
GetCurrentDateAndTime.class
GetCurrentDateAndTime.java
globdata.ini
Hello.vb
HelloWorld.cs
helloworld1.cs
inheritance_demo 1.java
install.exe
install.ini
install.res.1028.dll
install.res.1031.dll
install.res.1033.dll
install.res.1036.dll
install.res.1040.dll
install.res.1041.dll
install.res.1042.dll
install.res.2052.dll
install.res.3082.dll
LeftClass.java
MyComparator1.class
MyComparator1.java
Notepad.class
number.txt
odd.txt
overload.class
Overload_methods.class
Overload_methods.java
Overload_methods1.class
Overload_methods1.java
Parent.class
ReadInt.class
ReadInt.java
ReadInt1.class
ReadInt1.java
src.zip
Student.class
Student.java
StudentCMD.class
StudentCMD.java
TableCMD.class
TableCMD.java
Test.class
Test.java
Test1.class
Test1.java
Test1Exception.class
Test1Exception.java
TreeSetDemo.class
TreeSetDemo.java
vcredist.bmp
VC_RED.cab
VC_RED.MSI
WriteInt.class
WriteInt.java
The Total Number Of file=88

===============================================================================================


//wap to display only directory name  e:\\Books..................

package iojava;

import java.io.File;

public class AllFolderCount {
public static void main(String[] args) {

int count=0;
File f=new File("e:\\books");
String[] l = f.list();
for(String s1:l)
{
File f1=new File(f, s1);
if(f1.isDirectory())
{
count++;
System.out.println(s1);
}

}
System.out.println("Total Number of folder="+count);
}

}

==================================================================================================
o/p------------------------

CN
CSO
DBMS
Digital
DS
JAVA-CLASS - Copy
javaDatabase - Copy
JAVANOTES - Copy
Maths
OS
Total Number of folder=10

================================================================================================

//wap to calculate file size and demonstration of lastModified()

package iojava;

import java.io.File;
import java.text.SimpleDateFormat;

public class FileSize {
public static void main(String[] args) {
File f=new File("e:\\books\\Student.java");
long i = f.length();
System.out.println(i+" bytes");
System.out.println("Before Format:"+f.lastModified());
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println("After Format:"+sdf.format(f.lastModified()));

}
}

===============================================================================================

o/p:------------------

518 bytes
Before Format:1455804101542
After Format:02/18/2016 19:31:41




No comments:

Post a Comment