Monday, 7 October 2013

Read File Using InputStreamReader,FileInputStream



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class InputStreamReaderExample {


public static void main(String[] args) {

InputStream inputStream=null;
Reader reader=null;
try {
inputStream = new FileInputStream("d:/ram.txt");

reader = new InputStreamReader(inputStream);

int data = reader.read();

while(data != -1){
   char out = (char) data;
   System.out.print(out);
   data = reader.read();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Read File Using FileInputStream

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

public class ReadFileExample {

public static void main(String[] args) {

File file = new File("d:/ram.txt");
FileInputStream fis = null;

try {
fis = new FileInputStream(file);
 
  int content;
while ((content = fis.read()) != -1) {
// convert to char and display it
System.out.print((char) content);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

Read File Using FileReader

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileEx {

public static void main(String[] args) {

File f = new File("d:/ram.txt");

FileReader fr = null;

try {
fr = new FileReader(f);

for (int val = fr.read(); val!= -1;val=fr.read()) {
System.out.print((char) val);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

Read File Using Files Class,BufferedReader



import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class BufferedReaderWithFilesExample {

public static void main(String args[]) {

Charset cs= StandardCharsets.UTF_8;
Path path = Paths.get("d:/ram.txt");
        BufferedReader br=null;
try {
br = Files.newBufferedReader(path,cs);
String line;
       while((line = br.readLine()) != null){
           //process the line
           System.out.println(line);
       }
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}

Read File Using FileReader,BufferedReader

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

public static void main(String[] args) {

BufferedReader br = null;

try {

String s;
br = new BufferedReader(new FileReader("d:/ram.txt"));

while ((s = br.readLine()) != null) {
System.out.println(s);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}

Read File Using Java 1.7 Files Class


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;

//Files class is available from 1.7
public class FilesClassExample {

public static void main(String args[]){
Path path = Paths.get("d:/ram.txt");
        
        try {
               /*
                //Read file to byte array
byte[] bytes = Files.readAllBytes(path);

String val=new String(bytes);
System.out.println(val);

*/

//Read file to String list
               List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
      
      Iterator<String> i=allLines.iterator();
      while(i.hasNext()) {
          System.out.println(i.next());
      }
      
} catch (IOException e) {
e.printStackTrace();
}
}
}

Read File Contents Using Scanner



import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class UsingScanner {

public static void main(String[] args) throws FileNotFoundException {
Scanner s=new Scanner(new File("d:/ram.txt"));
while (s.hasNextLine()) {
                           String line = s.nextLine();
                           System.out.println(line);
                }
s.close();
}

}