Core Java Tutorial

 
 

Input&Output Files

A stream is defined as a sequence of data. Here we will use InputStream & OutputStream,The InputStream is used to read data from source and the OutputStream is used for writing data to a destination file.

Steps to read or write files
  1. Declare either InputReader(to read) or OutputReader(to write)(Ex:FileWriter fw = new FileWriter or FileReader fr = new FileReader .
  2. Create File object to pass file name(File fl = new File("sample.txt).
  3. Pass file object to reader object.
  4. and Finally close that stream
Note:You will get FileNotFoundException if you pass file which is not exist.

Sample Examples

Writing/Reading data into/from file.
WriteDataIntoFile.java
import java.io.*;
public class WriteDataIntoFile {
	public static void main(String[] args) throws IOException {
		FileWriter writer = null;
		BufferedWriter bWriter = null;
		try{
		File file = new File("K:\\sample.txt");
		//FileWriter Writer = null;
		//BufferedWriter bWriter = null;
		
		writer = new FileWriter(file);
		bWriter = new BufferedWriter(writer);
		bWriter.write("\n again this is venkat kallam");
		bWriter.write("\n again this is venkat kallam");
		bWriter.write("\n Done");
		
	}catch(Exception e){
		System.out.println(e);
		
	}
		finally{
			if(bWriter!=null){
				bWriter.close();
			}
			if(writer!=null){
				writer.close();	
			}
		}
	}

}

ReadingDataFromFile.java
package input.output.samples;
import java.io.*;
public class ReadingDataFromFile {
	public static void main(String[] args) throws IOException {
		FileReader fr = null;
		BufferedReader br = null;
		try{
		File file = new File("C:\\Users\\venka\\Desktop\\sample1.txt");
		//FileWriter Writer = null;
		//BufferedWriter bWriter = null;
		
		fr = new FileReader(file);
		br = new BufferedReader(fr);
		String line=null;
		while((line=br.readLine())!=null){
			System.out.println(line);
		}
		
		
	}catch(Exception e){
		System.out.println(e);
		
	}
		finally{
			if(br!=null){
				br.close();
			}
			if(fr!=null){
				fr.close();	
			}
		}
	}

}


FileDemo.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDemo {
    public static void main(String[] args) throws IOException {

        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream("xanadu.txt");
            out = new FileOutputStream("outagain.txt");
            int c;

            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

This is to print files and directories in a folder
import java.io.File;

public class FilesAndDir {
	public static void main(String[]args)
    {
        File dir = new File("K:\\FolderName");
        getAllFiles(dir);
    }
    private static void getAllFiles(File dir) {

        File[] filesList = dir.listFiles();
        for(File f : filesList){
            if(f.isDirectory())
                System.out.println(f.getName());
            if(f.isFile()){
                System.out.println(f.getName());
            }
        }

    }

}