Read and Write Files in Java

Here is a sample code I just wrote. This class copies one file to another.
By understanding this code below , you will comphrend both reading from and writing to file concepts in java. I used FileInputStream and FileOutputStream in this code.

package javacream;

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

public class FileUtil {

public static void copyFile(String strInputFileName , String strOutputFileName) throws IOException {

//Prepare file objects

File fInputFile = new File(strInputFileName);

File fOutputFile = new File(strOutputFileName);

//Prepare read write streams from file objects

FileInputStream fis = new FileInputStream(fInputFile);

FileOutputStream fos = new FileOutputStream(fOutputFile);

int c;

// read from the input file and write to the output file

while ((c = fis.read()) != -1) {

fos.write(c);

}

//we are done , close the streams.

fis.close();

fos.close();

}

public static void main(String[] args) {

try {

// use \\ for windows filenames , otherwise use single /

copyFile("d:\\sample.txt", "d:\\copyof_sample.txt");

} catch (IOException e) {

e.printStackTrace();

}

}

}




Alternatively , you can use FileReader object and BufferedReader. The sample code below counts the number of lines of a given file.

public static int lineCount(String fileName) throws IOException{

// file reader to read the file

FileReader input = new FileReader(fileName);

// Filter FileReader through a BufferedReader to read line by line

BufferedReader bufRead = new BufferedReader(input);

String line; // String that holds current file line

int count = 0; // Line count

// Read first line

line = bufRead.readLine();

count++;

// Read through file one line at time.

while (line != null){

line = bufRead.readLine();

count++;

}

bufRead.close();

return count;

}

Comments

Sumish said…
/* This is a better and simpler approach to read n write from and to a file in java */

import java.io.*;

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

FileReader fr = new FileReader(“testtry.txt”); // use any filename
BufferedReader br = new BufferedReader(fr);
int inputline;
FileWriter fw = new FileWriter(“hello.txt”); //use any filename

while((inputline =br.read()) !=-1) // check to see if the file has ended
{
fw.flush();
fw.write(inputline);

}
fr.close();
fw.close();
}catch (IOException e){};

/* Program by Sumish Darak*/
}

}
SD said…
/* Some modification to the previous file */
/* This is a better and simpler approach to read n write from and to a file in java */

import java.io.*;

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

FileReader fr = new FileReader(“testtry.txt”);
BufferedReader br = new BufferedReader(fr);
int inputline;
FileWriter fw = new FileWriter(“hello.txt”);

while((inputline =br.read()) !=-1)
{
fw.flush();
fw.write(inputline);

}
fr.close();
fw.close();
}catch (IOException e){};

/* Program by Sumish Darak*/
}

}
Anonymous said…
Sumish.......you are a great scholar.......
Neeta Pandit said…
Thanks a lot Sumish........you are the best.......you r really a scholar...
Knight Hacker said…
Whoz Sumish???

Popular posts from this blog

Connect via SSH without a password from MacOS to Linux with an SSHKEY

Install Mac OS on windows laptop