Reading a Text file using java code


How to read a text file using java code?

Here it is.

Full code available at -
https://github.com/namitsharma99/fileHandlingUsingJava

The class - com.java.code.ReadTextFile.java,  reads a text file and displays its contents, making the best use of IO weapon - FileInputStream.

package com.java.code;

import java.io.FileInputStream;

public class ReadTextFile {

public static void main(String[] args) {

String myExcelPath = "sample";
textReader(myExcelPath);

}

private static void textReader(String myTextPath) {

FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(myTextPath);
int c;
while ((c = fileInputStream.read()) != -1) {
System.out.print((char) c);
}

if (fileInputStream != null) {
fileInputStream.close();
}
} catch (Exception e1) {
System.out.println(e1);
}
}

}



Happy Coding!!

4 comments:

  1. Replies
    1. If you want to extend this program or others to other languages, platforms etc, let me know and I would definitely be interested in being a contributor.

      Delete
    2. sounds great, the forum is open to every developer who is interested :)

      Delete

Featured post

Oracle SQL Scheduled Jobs - An Interesting Approach

  Oracle SQL Scheduled Jobs A DB Scheduler is the best way to automate any backend database job. For instance, if you want to process the p...