Java - File

Java Conceptuel Diagram


File - File in Java


Object Instantiation

The file corresponding to the file name might not even exist.

File f = new File("myFileName.txt");


with the nio library

Path path = Paths.get("target/example.txt");


FileReader fileReader = new FileReader(file);
FileReader fileReader = new FileReader(path.toFile());
  • With encoding
new InputStreamReader(new FileInputStream(filePath), encoding)


BufferedWriter writer = Files.newBufferedWriter(path, Charset.UTF8, StandardOpenOption.APPEND)

Get the file extension

static private String getFileExtension(Path path) {
	String name = path.getFileName().toString();
	try {
		return name.substring(name.lastIndexOf(".") + 1);
	} catch (Exception e) {
		return "";

Meta Information

Path myFile = Paths.get("D:\\temp\\temp.txt");
System.out.println("The file exists ?: "+Files.exists(myFile));
System.out.println("The file size in bytes is: "+Files.size(myFile));
System.out.println("The file is a regular file? : "+Files.isRegularFile(myFile));
System.out.println("The file is readable? : "+Files.isReadable(myFile));
System.out.println("The file is executable? : "+Files.isExecutable(myFile));
The file exists ?: true
The file size in bytes is: 13346941
The file is a regular file? : true
The file is readable? : true
The file is executable? : true

if(f.exists()) { /* do something */ }
if(f..isFile()) { /* do something */ }


System.out.println("The file and the directory are equal? : "+Files.isSameFile(myFile, myDirectory));
The file and the directory are equal? : false

From file to string

with Java - Java IO

FileInputStream in = new FileInputStream(files[i]);
String sqlStatement = IOUtils.toString(in);


// Write the final in the Java directory system property ''''
File tempFile = File.createTempFile(prefix, suffix)

String tempDir = System.getProperty("");

# see also:

Discover More
Java Conceptuel Diagram
Java - (Nio|New I/O) Package

The Java NIO model is the last and second IO and File System mechanism of Java. It's available since Java 7. Before, the IO model was based on . In NIO, you define a file or a directory by creating...
Java Conceptuel Diagram
Java - File System - Path

in Java. The path is unique identifier of a file (directory) In nio, a Path instance represents a file or directory but doesn't access the file system (for instance in order to verify that a particular...
Java Conceptuel Diagram
Java - IO (Input/Output) - (File System|File|Console)

in Java: Ie File System Operations on file and directory Input / Output (IO) Operations (Input/Output - Read/Write) with stream Java has modeled a file system and IO operations: until Java...
Java Conceptuel Diagram
Java - Java IO

Java IO is the first IO model of Java. It provides for system input and output through data streams, serialization and the file system. See java/io/ With the advent of Java 7, the...

Share this page:
Follow us:
Task Runner