0% found this document useful (0 votes)
5 views18 pages

Java Io File

Presentation

Uploaded by

ltan22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views18 pages

Java Io File

Presentation

Uploaded by

ltan22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Introduction to

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

An abstract representation of file and directory pathnames.

User interfaces and operating systems use system-dependent pathname strings to


name files and directories. This class presents an abstract, system-independent
view of hierarchical pathnames.

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

Creates a new File instance by converting the


File(String pathname)
given pathname string into an abstract pathname.

Creates a new File instance from a parent


File(String parent, String child)
pathname string and a child pathname string.

Creates a new File instance from a parent abstract


File(File parent, String child)
pathname and a child pathname string.
Methods
Type Methods Description
Tests whether the application can
boolean canRead() / canWrite() read/modify the file denoted by the abstract
pathname.

Atomically creates a new, empty file named


boolean createNewFile() by the abstract pathname if and only if a file
with this name does not yet exist.

Deletes the file or directory denoted by the


boolean delete()
abstract pathname.
Methods
Type Methods Description
Returns an array of strings naming the files
String[] list() and directories in the directory denoted by
the abstract pathname.

Tests whether the file denoted by the


boolean isFile()
abstract pathname is a normal file.

Tests whether the file denoted by the


boolean isDirectory()
abstract pathname is a directory.
Methods
Type Methods Description

Creates the directory named by the abstract


boolean mkdir()
pathname.

Creates the directory named by the abstract


boolean mkdirs() pathname, including any necessary but
nonexistent parent directories.

Returns the pathname string of the abstract


String toString()
pathname.
03
JSON
What is JSON?
JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is


easy for humans to read and write and easy for machines to parse and generate.

It is:

● “self-describing”
● language independent
JSON object example

"name": "John Doe", "age": 30, "isMarried": false, "children": ["Jane", "Mark"],

"address": { "street": "123 Main St", "city": "Anytown", "zipcode": "12345" }

}
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);

// Read values from the JSONObject


String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
boolean isMarried = jsonObject.getBoolean("isMarried");
JSONArray children = jsonObject.getJSONArray("children");
JSONObject address = jsonObject.getJSONObject("address");
Read JSON from a file
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Married: " + isMarried);
System.out.println("Children: ");
for (int i = 0; i < children.length(); i++) {
System.out.println("Child " + (i + 1) + ": " + children.getString(i));
}
System.out.println("Address: ");
System.out.println("Street: " + address.getString("street"));
System.out.println("City: " + address.getString("city"));
System.out.println("Zipcode: " + address.getInt("zipcode"));
Write JSON to a file

FileWriter file = new FileWriter("src/main/resources/user_output.json");


// Write the JSON object to a file
file.write(jsonObject.toString(4));
System.out.println("JSON written to user_output.json");
Thanks!

You might also like