Thursday, 23 March 2017

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

JAVA based project, that can be used to hit DB using JDBC, from WSO2 ESB

Hi Buddies, Here is a small project that will enable you to hit MySQL DB using WSO2 ESB - https://github.com/namitsharma99/customM...