Showing posts with label read file. Show all posts
Showing posts with label read file. Show all posts

Wednesday, 11 February 2015

Read One File And Write Other File

In this Program we two file and we copy data of first.txt file in second.txt file using java code

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

public class CopyData {

    public static void main(String[] args) throws IOException {
    String pathInput="D:\\a\\first.txt";
    String pathOutput="D:\\a\\second.txt";
    FileInputStream in=null;
    FileOutputStream out=null;
    try {
        in=new FileInputStream(pathInput);
        out=new FileOutputStream(pathOutput);
        int c;
        while ((c=in.read())!=-1) {
            out.write(c);
        }
    } catch (Exception e) {
        System.out.println(e);
    }finally{
        if (!(in==null)) {
            in.close();
        }
        if (!(out==null)) {
            out.close();
        }
       
    }
    System.out.println("Copy Data");
}
   
}

Read File LIne By Line In Java

 In this program we read a file line by line using buffered reader

package filles;
import interview_set1.fileRead;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;

public class ReadFiles {
public static void main(String[] args) {
    String path="D:\\a\\hi.txt";
    BufferedReader br=null;
    try {
        FileReader fr=new FileReader(path);
        br=new BufferedReader(fr);
        String line;
        while ((line=br.readLine())!=null) {
            System.out.println(line);
        }
       
    } catch (Exception e) {
        System.out.println(e);
    }
}
}