ADVERTISEMENT
ADVERTISEMENT

Java - Print File Content, Display File

In this code snippet, we will print the file size and file content using Java program.

Print/Display File Content

// Java - Print File Content, Display File

import java.io.*;

public class PrintFile {
  public static void main(String args[]) throws IOException {
    File fileName = new File("d:/sample.txt");

    FileInputStream inFile = new FileInputStream("d:/sample.txt");
    int fileLength = (int) fileName.length();

    byte Bytes[] = new byte[fileLength];

    System.out.println("File size is: " + inFile.read(Bytes));

    String file1 = new String(Bytes);
    System.out.println("File content is:\n" + file1);

    //close file
    inFile.close();
  }
}

Output:

File size is: 22
File content is:
This is a sample file.

Java File Handling Programs »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.