Java - Io.file Class in Java: It Is An Abstract Representation of File and Directory Pathnames
Java - Io.file Class in Java: It Is An Abstract Representation of File and Directory Pathnames
A pathname, whether abstract or in string form can be either absolute or relative. The parent of an
abstract pathname may be obtained by invoking the getParent() method of this class.
First of all, we should create the File class object by passing the filename or directory name to it.
A file system may implement restrictions to certain operations on the actual file-system object, such
as reading, writing, and executing. These restrictions are collectively known as access permissions.
Instances of the File class are immutable; that is, once created, the abstract pathname represented by
a File object will never change.
How to create a File Object?
A File object is created by passing in a String that represents the name of a file, or a String or another File
object. For example,
File a = new File("/usr/local/bin/abc.txt");
class fileProperty
{
public static void main(String[] args)
{
//accept file name or directory name through command line args
//String fname =args[0];
import java.io.*;
public class FileExample
{
public static void main(String[] args)
{
File f=new File("/home/hjd/Desktop");
String filenames[]=f.list();
for(String filename:filenames)
{
System.out.println(filename);
}
}
}
Java File Example 3:- List all Files and Directories on Location
import java.io.*;
public class FileExample
{
public static void main(String[] args)
{
File dir=new File("/home/hjd/Desktop");
File files[]=dir.listFiles();
for(File file:files)
{
System.out.println(file.getName()+" Can Write: "+file.canWrite()+ "Is Hidden: "
+file.isHidden()+" Length: "+file.length()+" bytes");
}
}
}
Import java.io.*;
public class FilterExample
{
public static void main(String[] args) throws IOException
{
File data = new File("D:\\testout.txt");
FileInputStream file=new FileInputStream(data);
FilterInputStream filter = new BufferedInputStream(file);
int k =0;
while((k=filter.read())!=-1)
{
System.out.print((char)k);
}
file.close();
filter.close();
}
}
InputStreamReader class
InputStreamReader class can be used to read data from keyboard.It performs two tasks:
BufferedReader class
BufferedReader class can be used to read data line by line by readLine() method.
Import java.io.*;
class abc
{
public static void main(String args[])throws Exception
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name="";
while(!name.equals("stop"))
{
System.out.println("Enter data: ");
name=br.readLine();
System.out.println("data is: "+name);
}
br.close();
r.close();
}
}
Unlike FileOutputStream class, you don't need to convert string into byte array because it provides
method to write string directly.
import java.io.FileWriter;
public class FileWriterExample
{
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("a1.txt");
fw.write("Welcome to java.");
fw.close();
}
catch(Exception e)
{
System.out.println(e); }
System.out.println("Success...");
}}