Java Io File
Java Io File
class java.io.File
An abstract representation of file and directory pathnames.
Table of contents
01 02
java.io.File Methods
What is File object? File object constructors and operations
03 04
JSON Object Example
What is JSON? How to read/write JSON objects
from/to files?
01
java.io.File
Constructors of File object
java.io.File
Instances of the File class are immutable; that is, once created, the abstract
pathname represented by a File object will never change.
02
Methods
File object constructors and operations
Constructors
Constructor Description
It is:
● “self-describing”
● language independent
JSON object example
"name": "John Doe", "age": 30, "isMarried": false, "children": ["Jane", "Mark"],
}
04
Example
How to read/write JSON objects from/to files?
Libraries
import java.io.IOException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.FileReader;
import java.io.FileWriter;
import org.json.JSONArray;
Read JSON from file
JSONObject jsonObject = null;
FileReader reader = new FileReader("src/main/resources/user.json");
// Using JSONTokener to parse the JSON file
JSONTokener tokener = new JSONTokener(reader);
jsonObject = new JSONObject(tokener);