Working with text files

Working with text files


Posted in : Core Java Posted on : October 22, 2010 at 3:54 PM Comments : [ 0 ]

This section contains the detail about the Working with text files in java.

Working with text files

 Java can read several types of information from files: binary, Java objects, text, zipped files, etc. Here, we will discuss about text files and how can we use it.

Firstly, we will learn about how to create, write & read a text file.

Creating, writing & reading text file :

FileOutputStream is basically used to write data into file. But it would create a file, if it doesn't already exist, before opening it for output.

Following constructor takes a file name as a string to create an input stream object to write the file :

OutputStream f = new FileOutputStream("C:/java/NewFile")

First we create a file object using File( ) method. Following constructor takes a file object to create an output stream object to write the file.

File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);

To read data from the file, we use this type of stream. Using 'new' keyword, you can create Objects. In spite of that, there are several types of constructors available.

Following constructor takes a file name as a string to create an input stream object to write the file :

InputStream f = new FileInputStream("C:/java/NewFile");

First we create a file object using File() method. Following constructor takes a file object to create an input stream object to read the file :

File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);

Example :

import java.io.*;

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

try {
byte bWrite[] = { 11, 21, 3, 40, 5 };
OutputStream os = new FileOutputStream("C:/Demo.txt");
for (int x = 0; x < bWrite.length; x++) {
os.write(bWrite[x]); // writes the bytes
}
os.close();

InputStream is = new FileInputStream("C:/Demo.txt");
int size = is.available();

for (int i = 0; i < size; i++) {
System.out.print(is.read() + " ");
}
is.close();
} catch (IOException e) {
System.out.print("Exception");
}
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac FileIODemo.java

C:\Program Files\Java\jdk1.6.0_18\bin>java FileIODemo
 11  21  3  40  5

Creating list of subdirectories/file inside  a directory

Through java.io.File class, files and directories are accessed and manipulated. Java File class represents the files and directory pathnames in an abstract manner. For creation of files and directories, file searching, file deletion etc, this class can be used.

The actual file/directory on the disk is represeneted by the File object. Following are the constructors to create  a file object :

Following syntax creates a new File instance from a parent abstract pathname and a child pathname string :

File(File parent, String child);

Following syntax creates a new File instance by converting the given pathname string into an abstract pathname :

File(String pathname)

Following syntax creates a new File instance from a parent pathname string and a child pathname string :

File(String parent, String child)

Following syntax creates a new File instance by converting the given file: URI into an abstract pathname :

File(URI uri)

Example :

import java.io.File;

public class FileClassDemo {
public static void main(String args[]) {
String dirname = "/eclipse_programs";
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");
}
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>java FileClassDemo
Directory of /eclipse_programs
.metadata is a directory
CORE is a directory
Devmanuals is a directory
SCJP is a directory
Servers is a directory
Spring3 is a directory

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics