0% found this document useful (0 votes)
32 views

Java - Io.File File (String Directorypath) File (File Dirobj, String Filename)

The document discusses the File class in Java and how to work with files and directories. It provides examples of creating File objects from strings and other File objects, checking if a file exists, getting file attributes like name and path, listing directory contents, reading and writing files using FileInputStream, FileOutputStream, FileReader and FileWriter. It also demonstrates using a FilenameFilter to list files with a specific extension from a directory.

Uploaded by

arun
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Java - Io.File File (String Directorypath) File (File Dirobj, String Filename)

The document discusses the File class in Java and how to work with files and directories. It provides examples of creating File objects from strings and other File objects, checking if a file exists, getting file attributes like name and path, listing directory contents, reading and writing files using FileInputStream, FileOutputStream, FileReader and FileWriter. It also demonstrates using a FilenameFilter to list files with a specific extension from a directory.

Uploaded by

arun
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

java.io.

File

File(String directoryPath)
File(String directoryPath, String filename)
File(File dirObj, String filename)
File f1 = new File("/root");
File f2 = new File("/root","test.html");
File f3 = new File(f1,"test.html");
Demonstration of File class
import java.io.*;
class file1
{
public static void main(String ar[])
{
File f1=new File("abc.txt");
if(f1.exists())
System.out.println("File Exist");
else
System.out.println("File DOes not Exist");
}
}
[root@jinsamba Java]# cat abc.txt
hai]
sdhs
[root@jinsamba Java]# java file1
File Exist
[root@jinsamba Java]#
// Demonstrate File.
import java.io.File;
class FileDemo {
static void p(String s) {
System.out.println(s);
}
public static void main(String args[]) {
File f1 = new File("FileDemo.class");
p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("Abs Path: " + f1.getAbsolutePath());
p("Parent: " + f1.getParent());
p(f1.exists() ? "exists" : "does not exist");
p(f1.canWrite() ? "is writeable" : "is not writeable");
p(f1.canRead() ? "is readable" : "is not readable");
p("is " + (f1.isDirectory() ? "" : "not" + " a directory"));
p(f1.isFile() ? "is normal file" : "might be a named pipe");
p("File size: " + f1.length() + " Bytes");
}
}
G:\JvaTestPgm\File>java FileDemo
File Name: FileDemo.class
Path: FileDemo.class
Abs Path: G:\JvaTestPgm\File\FileDemo.class
Parent: null
exists
is writeable
is readable
is not a directory
is normal file
File size: 1642 Bytes
import java.io.File;
class DirList {
public static void main(String args[]) {
String dirname = "/home/jino/hai";
File f1 = new File(dirname);
if (f1.isDirectory()) {
System.out.println("Directory of " + dirname);
String s[] = f1.list();
for (int i=0; i < s.length; i++) {
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory()) {
System.out.println(s[i] + " is a directory");
} else {
System.out.println(s[i] + " is a file");
}
}
} else {
System.out.println(dirname + " is not a directory");
}
}
}
[root@jinsamba Java]# java DirList
Directory of /home/jino/hai
hai.txt is a file
hallo is a directory
1.html is a file
FilenameFilter

String[ ] list(FilenameFilter FFObj)


boolean accept(File directory, String
filename)
import java.io.*;
class OnlyExt implements FilenameFilter {
String ext;
public OnlyExt(String ext1) {
ext = "." + ext1;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}

class DirListOnly {
public static void main(String args[]) {
String dirname = "/home/jino/hai";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt("html");
String s[] = f1.list(only);
for (int i=0; i < s.length; i++) {
System.out.println(s[i]);
}
}
}
[root@jinsamba Java]# java DirListOnly
1.html
Stream Classes

InputStream

OutputStream

Reader

Writer
FileInputStream

FileInputStream(String filepath) throws FileNotFoundException


FileInputStream(File fileObj) throws FileNotFoundException

FileInputStream f0 = new FileInputStream("/first.sh")


File f = new File("/first.sh");
FileInputStream f1 = new FileInputStream(f);
import java.io.*;
class ShowFile {
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: ShowFile File");
return;
}
do {
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
[root@jinsamba Java]# java ShowFile hai.taxt
contents
in
the
text
file

[root@jinsamba Java]# java ShowFile hai1.txt


File Not Found
FileOutputStream

FileOutputStream(String fileName) throws
FileNotFoundException

void write(int byteval) throws IOException
import java.io.*;
class CopyFile {
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("Input File Not Found");
return;
}
try {
fout = new FileOutputStream(args[1]);
} catch(FileNotFoundException e) {
return;
}
// Copy File
try {
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println("File Error");
}
fin.close();
fout.close();
}
}
[root@jinsamba Java]# java CopyFile abc1.txt abc7.txt
[root@jinsamba Java]# cat abc7.txt
hai]
sdhs
[root@jinsamba Java]# java CopyFile abc7.txt abc8.txt
[root@jinsamba Java]# cat abc8.txt
hai]
sdhs
FileReader
FileReader(String filePath) FileNotFoundException
FileReader(File fileObj) FileNotFoundException
import java.io.*;
class FileReaderDemo {
public static void main(String args[]) throws Exception {
FileReader fr = new FileReader("abc.txt");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
}
}
[root@jinsamba Java]# java FileReaderDemo
hai]
sdhs
idfhdvvf

dfdf

last line
FileWriter

FileWriter(String filePath) throws IOException


FileWriter(String filePath, boolean append)

FileWriter(File fileObj)
FileWriter(File fileObj, boolean append)
import java.io.*;
class CopyCharacters {
public static void main(String[] args) throws
IOException {
FileReader inputStream;
FileWriter outputStream;
inputStream = new FileReader("abc.txt");
outputStream = new FileWriter("abcoutput1.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
inputStream.close();
outputStream.close();
}
}
[root@jinsamba Java]# java CopyCharacters
[root@jinsamba Java]# cat abcoutput1.txt
hai]
sdhs
idfhdvvf

dfdf

last line

You might also like