get file list from a folder filtered by extensions java

The listFiles() method is used to get file list from a folder. Example: package com.w3spoint;   import java.io.File; import java.io.FilenameFilter;   public class FilteredFileList { public static void main(String args[]){ File file = new File("D:/Test files/"); File[] files = file.listFiles(new FilenameFilter() {   @Override public boolean accept(File dir, String name) { if(name.toLowerCase().endsWith(".docx")){ return true; } … Read more

Categories IO

read file content line by line java program

The readLine() method is used to get file content line by line. The readLine() returns one line at each iteration. Example: package com.w3spoint;   import java.io.BufferedReader; import java.io.FileReader;   public class ReadFile { public static void main(String args[]){ String fileName = "D:/Test files/file 3.txt"; BufferedReader br = null; String strLine = ""; try { br … Read more

Categories IO

read file content in byte array java

Example: package com.w3spoint;   import java.io.FileInputStream; import java.io.InputStream;   public class FileToByteArray { public static void main(String args[]){ String fileName = "D:/Test files/file 3.txt"; InputStream is = null; try { is = new FileInputStream(fileName); byte content[] = new byte[2*1024]; int readCount = 0; while((readCount = is.read(content)) > 0){ System.out.println(new String(content, 0, readCount-1)); } } catch … Read more

Categories IO

How to filter the files by file extensions java

We need to implement FilenameFilter class and override accept() method. Example: package com.w3spoint;   import java.io.File; import java.io.FilenameFilter;   public class FilesFilter { public static void main(String args[]){ File file = new File("D:/Test files/"); String[] files = file.list(new FilenameFilter() {   @Override public boolean accept(File dir, String name) { if(name.toLowerCase().endsWith(".txt")){ return true; } else { … Read more

Categories IO

read all files from folder java get list program

The listFiles() method of File class is used to get the list of files in the folder or directory. Example: package com.w3spoint;   import java.io.File;   public class ListFiles { public static void main(String args[]){ File file = new File("D:/Test files/"); File[] files = file.listFiles(); for(File f: files){ System.out.println(f.getName()); } } }package com.w3spoint; import java.io.File; … Read more

Categories IO

list all file names from folder java

The list() method of File class is used to get the list of file names in the folder or directory. Example: package com.w3spoint;   import java.io.File;   public class ListFileNames { public static void main(String args[]){ File file = new File("D:/Test files/"); String[] fileList = file.list(); for(String name:fileList){ System.out.println(name); } } }package com.w3spoint; import java.io.File; … Read more

Categories IO

Transient keyword in java

Transient variable: A variable declared with transient keyword is a transient variable. It is used in serialization process to provide flexibility for excluding some variables from serialization process. A variable declared with transient keyword will not be serialized. Example: TransientExample.java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;   /** * … Read more

Categories IO

Serialization and Deserialization in java

In java serialization is way used to convert an object into a byte stream which can be transported to any other running JVM through a network or can be persisted into disk and that object can be rebuilt again. Java provides serialization API for this. How to make a class serializable in java? To make … Read more

Categories IO

How To Check If A File Exists In Java

Example: FileCheckExample.java import java.io.File;   /** * This program is used to check that file * exists or not at given location. * @author w3spoint */ class IOTest{ public void checkFileExistance(){ //Creating File object. File file = new File("F:\\New folder\\data1.txt"); if(file.exists()){ System.out.println("file exist."); }else{ System.out.println("file not exist."); } } }   public class FileCheckExample { … Read more

Categories IO

FileReader and FileWriter in java

FileReader: FileReader class is used for reading streams of characters from a file. Commonly used constructors of FileReader: 1. FileReader(File file) Creates a new FileReader, given the File to read from. 2. FileReader(String fileName) Creates a new FileReader, given the name of the file to read from. Example: FileReaderExample.java import java.io.FileReader;   /** * This … Read more

Categories IO