A stream can be defined as an object through which you can read and write information. The destination can be “a file, an array, and standard input-output devices or network sockets”. All stream-oriented classes are available in the package “java.io”. Which needs to be imported.
Syntax: import java.io. *;
File Class
The file class gives elaborate information regarding files such as the name of the file, size of file…
Constructors:
File(String fileName);Constructs a ‘File’ object from the specified ‘fileName’.
File(String dirPath, String fileName);Constructs a ‘File object for the ‘fileName’ specified in the ‘dirPath’.
File(File object); Constructs a ‘File’ object from another ‘File’ object.
Methods:
- String getName(); Returns the name of the file.
- String getAbsolutePath(); Returns the path of the file.
- boolean exists(); Checks whether the specified file name exists in the specified directory or not.
- boolean isFile(); Checks whether the specified name is a file or not.
- boolean isDirectory(); Checks whether the specified name is a directory or not.
- boolean canRead(); Checks whether contents can be read from the file or not.
- boolean canWrite(); Checks whether contents can be written to the file or not.
- long length(); Returns the size of the file in bytes.
- Date lastModified(); Returns the date and time when the file was last modified.
- boolean renameTo(File object); Renames the invoking file with the specified ‘object’.
- boolean delete(); Removes the specified file.
- String[ ] list(); Returns the names of all the files available in the specified directory.
- File[ ] listFiles(); Returns an array of ‘File’ object for all the files available in the specified directory.
import java.io.*;
class App67
{
public static void main(String yck[])
{
File f=new File(yck[0]);
System.out.println("File Name :"+f.getName());
System.out.println("File Path :"+f.getAbsolutePath());
System.out.println("Does the file exist ?"+f.exists());
System.out.println("Is it a file ?"+f.isFile());
System.out.println("Is it a directory ?"+f.isDirectory());
System.out.println("Can u read from the file ?"+f.canRead());
System.out.println("Can u write to the file?"+f.canWrite());
System.out.println("Length of the file :"+f.length());
System.out.println("Date last modified :"+f.lastModified());
}
}
output:
\>javac App67.java
\>java App67 App67.java
File Name :App67.java
File Path :C:\Institute Java programs\App67.java
Does the file exist ?true
Is it a file ?true
Is it a directory ?false
Can u read from the file ?true
Can u write to the file?true
Length of the file :696
Date last modified :1076685096000
import java.io.*;
class App68
{
public static void main(String yck[])
{
File f1=new File(yck[0]);
if (f1.exists())
{
File f2=new File(yck[1]);
if (!f2.exists())
{
f1.renameTo(f2);
System.out.println("File successfully renamed ...");
}
else
System.out.println("Destination filename already exists ...");
}
else
System.out.println("Source filename does not exist ...");
}
}
output:
\>javac App68.java
\>java App68 App68.java chaitanya.txt
File successfully renamed ...
import java.io.*;
class App69
{
public static void main(String yck[])
{
File f=new File(yck[0]);
String fileNames[]=f.list();
for(int i=0;i<fileNames.length;i++)
System.out.print(fileNames[i]+",");
}
}
output:
\>javac App69.java
\>java App69 c:\
SUHDLOG.DAT,BOOTLOG.TXT,DETLOG.OLD,WIN98,BOOTLOG.PRV,DETLOG.TXT,COMMAND.COM,IO.S YS,MSDOS.SYS,NCDTREE,MSDOS.---,SETUPLOG.TXT,WINDOWS,AUTOEXEC.BAT,CONFIG.SYS,My D ocuments,jdk1.3,SYSTEM.1ST,Program Files,JSDK2.0,JavaWebServer1.1.3,RECYCLED,ffa stun.ffl,ffastun0.ffx,PSFONTS,ORAWIN95,ffastun.ffo,ffastun.ffa,Folder.htt,unzipp ed,DBLSPACE.INI,PM65,KPCMS,Desktop.ini,cd-key.txt,STDCodes,icons,Institute Java programs,applet,
Note: list(); will collect even hidden files also.
import java.io.*;
class App70
{
public static void main(String yck[])
{
File f=new File(yck[0]);
File f1[]=f.listFiles();
for(int i=0;i System.out.println(f1[i].getName()+"("+f1[i].length()+"), ");
}
}
output:
\>javac App70.java
\>java App70 c:\instit~1
App1.java(192), App10.java(388), App11.java(516), App12.java(473), App13.java(52 0), App14.java(339), App15.java(417), App16.java(455), App17.java(489), App18.ja va(692), App19.java(574), App2.java(185), App20.java(421), App21.java(453), App2 2.java(432), App23.java(506), App24.java(775), App25.java(663), App26.java(855), App27.java(791), App28.java(638), App29.java(739), App3.java(203), App30.java(4 03), App31.java(253), App32.java(216), App33.java(309), App34.java(306), App35.j ava(534), App36.java(569), App37.java(406), App38.java(339), App39.java(246), Ap p4.java(303), App40.java(276), App41.java(257), App42.java(221), App43.java(246) , App44.java(284), App45.java(481), App46.java(262), App47.java(252), App48.java (303), App49.java(220), App5.java(227), App50.java(289), App51.java(754), App52. java(278), App53.java(306), App54.java(193), App55.java(370), App56.java(1012), App57.java(439), App58.java(753), App59.java(787), App6.java(355), App60.java(86 1), App61.java(432), App62.java(305), App63.java(780), App64.java(753), App65.ja va(858), App7.java(186), App8.java(189), App9.java(164), App69.class(680), App67 .java(696), App66.java(897), App70.java(324), App69.java(264), Box.class(834), A pp71.java(315), App72.java(362), App73.java(972), App74.java(381), App75.java(38 7), App76.java(364), App77.java(347), App78.java(353), App79.java(456), App91.ja va(477), App81.java(380), App82.java(506), App83.java(255), App84.java(355), App 85.java(356), App86.java(310), App87.java(326), App88.java(332), App89.java(220) , App90.java(402), App80.java(718), App92.java(469), App93.java(331), App94.java (272), App95.java(1140), App96.java(366), App97.java(200), App15.class(335), App 98.java(1259), App16.class(718), App99.java(465), App17.class(763), App18.class( 475), notes.txt(2260), App19.class(385), extra1.java(389), JDBC.txt(5283), JAVA NOTES.doc(31232), App20.class(309), App21.class(309), extra2.java(398), MyInterf ace.class(123), Greetings.class(389), Test.class(406), App61.class(353), App62.c lass(559), ChildThread.class(853), App63.class(863), App64.class(863), App65.cla ss(935), App67.class(1367), App68.class(704), chaitanya.txt(482), App70.class(79 2),
Note: list(); will collect even hidden files also.
