From 42f07c638dd271763fa968be83577b1400550775 Mon Sep 17 00:00:00 2001 From: Mehrdad Shirvani Date: Sun, 10 May 2026 16:35:22 +0330 Subject: [PATCH 1/2] add files --- data.json | 89 +++++++++++++++++++ pom.xml | 8 ++ .../AddresesingModes/FileAddressing.java | 28 ++++++ .../CreatingFiles/CreateFileExample01.java | 43 +++++++++ .../CreatingFiles/CreateFileExample02.java | 25 ++++++ .../DeletingFiles/DeleteFileExample01.java | 15 ++++ .../DeletingFiles/DeleteFileExample02.java | 20 +++++ .../org/FileExamples/ParsingJson/Genre.java | 37 ++++++++ .../ParsingJson/ParsingJsonExample01.java | 38 ++++++++ .../ReadingFiles/ReadFileExample01.java | 27 ++++++ .../ReadingFiles/ReadFileExample02.java | 24 +++++ .../WritingFiles/WriteFileExample01.java | 27 ++++++ .../WritingFiles/WriteFileExample02.java | 38 ++++++++ src/main/resources/data.txt | 1 + 14 files changed, 420 insertions(+) create mode 100644 data.json create mode 100644 src/main/java/org/FileExamples/AddresesingModes/FileAddressing.java create mode 100644 src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java create mode 100644 src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java create mode 100644 src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample01.java create mode 100644 src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample02.java create mode 100644 src/main/java/org/FileExamples/ParsingJson/Genre.java create mode 100644 src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java create mode 100644 src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java create mode 100644 src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java create mode 100644 src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java create mode 100644 src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java create mode 100644 src/main/resources/data.txt diff --git a/data.json b/data.json new file mode 100644 index 0000000..3146042 --- /dev/null +++ b/data.json @@ -0,0 +1,89 @@ +{ + "genres": [ + { + "id": 1, + "name": "Crime" + }, + { + "id": 2, + "name": "Drama" + }, + { + "id": 3, + "name": "Action" + }, + { + "id": 4, + "name": "Biography" + }, + { + "id": 5, + "name": "History" + }, + { + "id": 6, + "name": "Adventure" + }, + { + "id": 7, + "name": "Fantasy" + }, + { + "id": 8, + "name": "Western" + }, + { + "id": 9, + "name": "Comedy" + }, + { + "id": 10, + "name": "Sci-Fi" + }, + { + "id": 11, + "name": "Mystery" + }, + { + "id": 12, + "name": "Thriller" + }, + { + "id": 13, + "name": "Family" + }, + { + "id": 14, + "name": "War" + }, + { + "id": 15, + "name": "Animation" + }, + { + "id": 16, + "name": "Romance" + }, + { + "id": 17, + "name": "Horror" + }, + { + "id": 18, + "name": "Music" + }, + { + "id": 19, + "name": "Film-Noir" + }, + { + "id": 20, + "name": "Musical" + }, + { + "id": 21, + "name": "Sport" + } + ], + "total": 21 +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 52bc355..c1498c9 100644 --- a/pom.xml +++ b/pom.xml @@ -14,4 +14,12 @@ UTF-8 + + + com.google.code.gson + gson + 2.11.0 + + + \ No newline at end of file diff --git a/src/main/java/org/FileExamples/AddresesingModes/FileAddressing.java b/src/main/java/org/FileExamples/AddresesingModes/FileAddressing.java new file mode 100644 index 0000000..a8fcbe7 --- /dev/null +++ b/src/main/java/org/FileExamples/AddresesingModes/FileAddressing.java @@ -0,0 +1,28 @@ +package org.FileExamples.AddresesingModes; + +import java.io.InputStream; +import java.nio.file.Path; +import java.util.Scanner; + +public class FileAddressing { + static void main() { + //relative + Path relativePath = Path.of("data.txt"); + //absolute + Path absolutePath = Path.of("C:\\Users\\Lenovo\\data.txt"); + + + //using class path / resources + InputStream input = FileAddressing.class + .getClassLoader() + .getResourceAsStream("data.txt"); + + Scanner scanner = new Scanner(input); + + while (scanner.hasNextLine()) { + System.out.println(scanner.nextLine()); + } + + scanner.close(); + } +} diff --git a/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java new file mode 100644 index 0000000..c1bda54 --- /dev/null +++ b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java @@ -0,0 +1,43 @@ +package org.FileExamples.CreatingFiles; + +import java.io.File; +import java.io.IOException; + +public class CreateFileExample01 { + static void main() { + File file = new File("quotes.txt"); + try { + if(file.createNewFile()) + { + System.out.println("created new file"); + } + else + { + System.out.println("file already exists"); + } + } catch (IOException e) { + System.err.println(e.getMessage()); + } + + + File newfile = new File("newfolder/quotes.txt"); + + try { + newfile.getParentFile().mkdirs(); + if(newfile.createNewFile()) + { + System.out.println("created new file"); + } + else + { + System.out.println("file already exists"); + } + } catch (IOException e) { + System.err.println(e.getMessage()); + } + } + //TODO: 1. Create File + //TODO: 2. Specify and address and directory that exists + //TODO: 3. Specify and address and directory that does not exists + //TODO: 4. +} diff --git a/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java new file mode 100644 index 0000000..baaeace --- /dev/null +++ b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java @@ -0,0 +1,25 @@ +package org.FileExamples.CreatingFiles; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class CreateFileExample02 { + public static void main(String[] args) { + Path path = Path.of("quotes.txt"); + try { + Files.createFile(path); + } catch (IOException e) { + System.err.println(e.getMessage()); + } + + + Path newPath = Path.of("newfolder/quotes.txt"); + try { +// Files.createDirectories(newPath.getParent()); + Files.createFile(newPath); + } catch (IOException e) { + System.err.println(e.getMessage()); + } + } +} diff --git a/src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample01.java b/src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample01.java new file mode 100644 index 0000000..fd8aa14 --- /dev/null +++ b/src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample01.java @@ -0,0 +1,15 @@ +package org.FileExamples.DeletingFiles; + +import java.io.File; + +public class DeleteFileExample01 { + static void main() { + File file = new File("example.txt"); + + if (file.delete()) { + System.out.println("Deleted file: " + file.getName()); + } else { + System.out.println("Failed to delete the file."); + } + } +} diff --git a/src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample02.java b/src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample02.java new file mode 100644 index 0000000..a111d2d --- /dev/null +++ b/src/main/java/org/FileExamples/DeletingFiles/DeleteFileExample02.java @@ -0,0 +1,20 @@ +package org.FileExamples.DeletingFiles; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class DeleteFileExample02 { + static void main() { + try + { + Path path = Path.of("example.txt"); + Files.deleteIfExists(path); + System.out.println("File deleted if it existed."); + } + catch (IOException e) + { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/FileExamples/ParsingJson/Genre.java b/src/main/java/org/FileExamples/ParsingJson/Genre.java new file mode 100644 index 0000000..af35b6d --- /dev/null +++ b/src/main/java/org/FileExamples/ParsingJson/Genre.java @@ -0,0 +1,37 @@ +package org.FileExamples.ParsingJson; + + +public class Genre +{ + private Long id; + private String name; + + public Genre(Long id, String name) { + this.id = id; + this.name = name; + } + + public Genre() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return id + ". " + name; + } +} \ No newline at end of file diff --git a/src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java b/src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java new file mode 100644 index 0000000..ba7732e --- /dev/null +++ b/src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java @@ -0,0 +1,38 @@ +package org.FileExamples.ParsingJson; + +import com.google.gson.*; +import com.google.gson.reflect.TypeToken; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +public class ParsingJsonExample01 { + static void main() { + try + { + Path path = Path.of("data.json"); + String jsonString = new String(Files.readAllBytes(path)); + Gson gson = new Gson(); + JsonObject rootObject = JsonParser.parseString(jsonString).getAsJsonObject(); + JsonPrimitive jsonPrimitive = rootObject.getAsJsonPrimitive("total"); + System.out.println(jsonPrimitive.getAsInt()); + + JsonArray genresArray = rootObject.getAsJsonArray("genres"); + + Type type = new TypeToken>(){}.getType(); + List genres = gson.fromJson(genresArray, type); + for(Genre genre : genres) + { + System.out.println(genre.getId() + ". " + genre.getName()); + } + } + catch (IOException e) + { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java new file mode 100644 index 0000000..298ff54 --- /dev/null +++ b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java @@ -0,0 +1,27 @@ +package org.FileExamples.ReadingFiles; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class ReadFileExample01 { + static void main() { + try + { + File file = new File("example.txt"); + Scanner reader = new Scanner(file); + while(reader.hasNextLine()) + { + String line = reader.nextLine(); + System.out.println(line); + } + reader.close(); + } + catch (FileNotFoundException e) + { + throw new RuntimeException(e); + } + + + } +} diff --git a/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java new file mode 100644 index 0000000..f074ab9 --- /dev/null +++ b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java @@ -0,0 +1,24 @@ +package org.FileExamples.ReadingFiles; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class ReadFileExample02 { + static void main() { + try + { + Path path = Path.of("example.txt"); + List lines = Files.readAllLines(path); + for(String line: lines) + { + System.out.println(line); + } + } + catch (IOException e) + { + System.err.println("Something went wrong"); + } + } +} diff --git a/src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java new file mode 100644 index 0000000..a3acd05 --- /dev/null +++ b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java @@ -0,0 +1,27 @@ +package org.FileExamples.WritingFiles; + +import java.io.FileWriter; +import java.io.IOException; + +public class WriteFileExample01 { + static void main() { + try { + FileWriter fileWriter = new FileWriter("quotes.txt"); + fileWriter.write("I'm not superstitious, but I am a little stitious.\n"); + fileWriter.write("I am running away from my responsibilities. And it feels good.\n"); + fileWriter.close(); + } catch (IOException e) { + System.err.println(e.getMessage()); + } + + + try { + FileWriter fileWriter = new FileWriter("quotes.txt", true); + fileWriter.write("I DECLARE BANKRUPTCY!"); + fileWriter.close(); + } catch (IOException e) { + System.err.println(e.getMessage()); + } + + } +} diff --git a/src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java new file mode 100644 index 0000000..8a7c55e --- /dev/null +++ b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java @@ -0,0 +1,38 @@ +package org.FileExamples.WritingFiles; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.List; + +public class WriteFileExample02 { + static void main() { + + + try { + Path path = Path.of("quotes.txt"); + List lines = List.of( + "I'm not superstitious, but I am a little stitious.", + "I am running away from my responsibilities. And it feels good." + ); + Files.write(path, lines); + } catch (IOException e) { + throw new RuntimeException(e); + } + + + try { + Path path = Path.of("quotes.txt"); + List lines = List.of( + "I DECLARE BANKRUPTCY!" + ); + Files.write(path, lines, StandardOpenOption.APPEND); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/resources/data.txt b/src/main/resources/data.txt new file mode 100644 index 0000000..e3c5b78 --- /dev/null +++ b/src/main/resources/data.txt @@ -0,0 +1 @@ +this is a data file. \ No newline at end of file From 51cccccea4c184aaeefd84894af7a34b1992e2f5 Mon Sep 17 00:00:00 2001 From: Mehrdad Shirvani Date: Fri, 15 May 2026 17:30:41 +0330 Subject: [PATCH 2/2] feat: complete all files --- .gitignore | 4 ++- quotes.txt | 3 ++ .../CreatingFiles/CreateFileExample01.java | 32 ++++--------------- .../CreatingFiles/CreateFileExample02.java | 11 +------ .../ParsingJson/ParsingJsonExample01.java | 25 +++++++-------- .../ReadingFiles/ReadFileExample01.java | 17 ++++------ .../ReadingFiles/ReadFileExample02.java | 14 ++++---- .../WritingFiles/WriteFileExample01.java | 24 ++++++++++---- .../WritingFiles/WriteFileExample02.java | 24 ++++---------- 9 files changed, 61 insertions(+), 93 deletions(-) create mode 100644 quotes.txt diff --git a/.gitignore b/.gitignore index 480bdf5..900d356 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,6 @@ build/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +examples*.txt \ No newline at end of file diff --git a/quotes.txt b/quotes.txt new file mode 100644 index 0000000..9b392c1 --- /dev/null +++ b/quotes.txt @@ -0,0 +1,3 @@ +I'm not superstitious, but I am a little stitious. +I am running away from my responsibilities. And it feels good. +I DECLARE BANKRUPTCY! \ No newline at end of file diff --git a/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java index c1bda54..906a71c 100644 --- a/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java +++ b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample01.java @@ -5,39 +5,21 @@ import java.io.IOException; public class CreateFileExample01 { static void main() { - File file = new File("quotes.txt"); - try { + File file = new File("texts/examples.txt"); + try + { if(file.createNewFile()) { - System.out.println("created new file"); + System.out.println("File created"); } else { - System.out.println("file already exists"); + System.out.println("File already exists"); } - } catch (IOException e) { - System.err.println(e.getMessage()); } - - - File newfile = new File("newfolder/quotes.txt"); - - try { - newfile.getParentFile().mkdirs(); - if(newfile.createNewFile()) - { - System.out.println("created new file"); - } - else - { - System.out.println("file already exists"); - } - } catch (IOException e) { + catch(IOException e) + { System.err.println(e.getMessage()); } } - //TODO: 1. Create File - //TODO: 2. Specify and address and directory that exists - //TODO: 3. Specify and address and directory that does not exists - //TODO: 4. } diff --git a/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java index baaeace..c30e324 100644 --- a/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java +++ b/src/main/java/org/FileExamples/CreatingFiles/CreateFileExample02.java @@ -6,20 +6,11 @@ import java.nio.file.Path; public class CreateFileExample02 { public static void main(String[] args) { - Path path = Path.of("quotes.txt"); + Path path = Path.of("exmaples.txt"); try { Files.createFile(path); } catch (IOException e) { System.err.println(e.getMessage()); } - - - Path newPath = Path.of("newfolder/quotes.txt"); - try { -// Files.createDirectories(newPath.getParent()); - Files.createFile(newPath); - } catch (IOException e) { - System.err.println(e.getMessage()); - } } } diff --git a/src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java b/src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java index ba7732e..4f967d2 100644 --- a/src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java +++ b/src/main/java/org/FileExamples/ParsingJson/ParsingJsonExample01.java @@ -8,30 +8,29 @@ import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; public class ParsingJsonExample01 { static void main() { - try - { - Path path = Path.of("data.json"); - String jsonString = new String(Files.readAllBytes(path)); - Gson gson = new Gson(); - JsonObject rootObject = JsonParser.parseString(jsonString).getAsJsonObject(); - JsonPrimitive jsonPrimitive = rootObject.getAsJsonPrimitive("total"); - System.out.println(jsonPrimitive.getAsInt()); + try { + Path path = Path.of("data.json"); + String texts = new String(Files.readAllBytes(path)); + + JsonObject rootObject = JsonParser.parseString(texts).getAsJsonObject(); JsonArray genresArray = rootObject.getAsJsonArray("genres"); + Gson gson = new Gson(); Type type = new TypeToken>(){}.getType(); + List genres = gson.fromJson(genresArray, type); - for(Genre genre : genres) + + for(Genre genre: genres) { - System.out.println(genre.getId() + ". " + genre.getName()); + System.out.println(genre.getId() + ". " + genre.getName()); } - } - catch (IOException e) - { + } catch (IOException e) { throw new RuntimeException(e); } } diff --git a/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java index 298ff54..24cf1fc 100644 --- a/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java +++ b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample01.java @@ -6,22 +6,17 @@ import java.util.Scanner; public class ReadFileExample01 { static void main() { - try - { - File file = new File("example.txt"); + File file = new File("quotes.txt"); + try { Scanner reader = new Scanner(file); - while(reader.hasNextLine()) + + while(reader.hasNext()) { String line = reader.nextLine(); System.out.println(line); } - reader.close(); - } - catch (FileNotFoundException e) - { - throw new RuntimeException(e); - } - + } catch (FileNotFoundException e) { + } } } diff --git a/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java index f074ab9..b245a14 100644 --- a/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java +++ b/src/main/java/org/FileExamples/ReadingFiles/ReadFileExample02.java @@ -7,18 +7,16 @@ import java.util.List; public class ReadFileExample02 { static void main() { - try - { - Path path = Path.of("example.txt"); + Path path = Path.of("quotes.txt"); + try { List lines = Files.readAllLines(path); - for(String line: lines) + + for(String line : lines) { System.out.println(line); } - } - catch (IOException e) - { - System.err.println("Something went wrong"); + } catch (IOException e) { + throw new RuntimeException(e); } } } diff --git a/src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java index a3acd05..3b4061c 100644 --- a/src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java +++ b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample01.java @@ -5,21 +5,31 @@ import java.io.IOException; public class WriteFileExample01 { static void main() { - try { - FileWriter fileWriter = new FileWriter("quotes.txt"); + + try + { + FileWriter fileWriter = new FileWriter("examples2.txt"); fileWriter.write("I'm not superstitious, but I am a little stitious.\n"); fileWriter.write("I am running away from my responsibilities. And it feels good.\n"); + fileWriter.close(); - } catch (IOException e) { + System.out.println("Done."); + } + catch (IOException e) + { System.err.println(e.getMessage()); } - - try { - FileWriter fileWriter = new FileWriter("quotes.txt", true); + try + { + FileWriter fileWriter = new FileWriter("examples2.txt", true); fileWriter.write("I DECLARE BANKRUPTCY!"); + fileWriter.close(); - } catch (IOException e) { + System.out.println("Done."); + } + catch (IOException e) + { System.err.println(e.getMessage()); } diff --git a/src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java index 8a7c55e..608ae73 100644 --- a/src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java +++ b/src/main/java/org/FileExamples/WritingFiles/WriteFileExample02.java @@ -11,28 +11,16 @@ import java.util.List; public class WriteFileExample02 { static void main() { + List lines = List.of( + "Line 1", + "Line 2" + ); - + Path path = Path.of("examples2.txt"); try { - Path path = Path.of("quotes.txt"); - List lines = List.of( - "I'm not superstitious, but I am a little stitious.", - "I am running away from my responsibilities. And it feels good." - ); Files.write(path, lines); } catch (IOException e) { - throw new RuntimeException(e); - } - - - try { - Path path = Path.of("quotes.txt"); - List lines = List.of( - "I DECLARE BANKRUPTCY!" - ); - Files.write(path, lines, StandardOpenOption.APPEND); - } catch (IOException e) { - throw new RuntimeException(e); + System.err.println(e.getMessage()); } } }