Hello guys! I just want to ask if how can I use java file reader? I badly need this for my program...

Thank you in advance!


Recommended Answers

All 4 Replies

maybe this help

import java.io.*;

public class ReadFile {

  public static void main(String[] args) {

    File file = new File("C:\\sample_text.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

      // this statement reads the line from the file and print it to
        // the console.

      // dispose all the resources after using them.

    } catch (FileNotFoundException e) {
    } catch (IOException e) {

If it is ok to you, please give me a sample program that uses java file reader?

Please keep in mind the Daniweb principle:
"to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well."
Just giving lazy people code to copy teaches them little or nothing (except maybe that cheating works).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.