feat: complete all files

This commit is contained in:
2026-05-15 17:30:41 +03:30
parent 42f07c638d
commit 51cccccea4
9 changed files with 61 additions and 93 deletions
+3 -1
View File
@@ -36,4 +36,6 @@ build/
.vscode/ .vscode/
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
examples*.txt
+3
View File
@@ -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!
@@ -5,39 +5,21 @@ import java.io.IOException;
public class CreateFileExample01 { public class CreateFileExample01 {
static void main() { static void main() {
File file = new File("quotes.txt"); File file = new File("texts/examples.txt");
try { try
{
if(file.createNewFile()) if(file.createNewFile())
{ {
System.out.println("created new file"); System.out.println("File created");
} }
else else
{ {
System.out.println("file already exists"); System.out.println("File already exists");
} }
} catch (IOException e) {
System.err.println(e.getMessage());
} }
catch(IOException e)
{
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()); 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.
} }
@@ -6,20 +6,11 @@ import java.nio.file.Path;
public class CreateFileExample02 { public class CreateFileExample02 {
public static void main(String[] args) { public static void main(String[] args) {
Path path = Path.of("quotes.txt"); Path path = Path.of("exmaples.txt");
try { try {
Files.createFile(path); Files.createFile(path);
} catch (IOException e) { } catch (IOException e) {
System.err.println(e.getMessage()); 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());
}
} }
} }
@@ -8,30 +8,29 @@ import java.lang.reflect.Type;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ParsingJsonExample01 { public class ParsingJsonExample01 {
static void main() { 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"); JsonArray genresArray = rootObject.getAsJsonArray("genres");
Gson gson = new Gson();
Type type = new TypeToken<List<Genre>>(){}.getType(); Type type = new TypeToken<List<Genre>>(){}.getType();
List<Genre> genres = gson.fromJson(genresArray, type); List<Genre> 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); throw new RuntimeException(e);
} }
} }
@@ -6,22 +6,17 @@ import java.util.Scanner;
public class ReadFileExample01 { public class ReadFileExample01 {
static void main() { static void main() {
try File file = new File("quotes.txt");
{ try {
File file = new File("example.txt");
Scanner reader = new Scanner(file); Scanner reader = new Scanner(file);
while(reader.hasNextLine())
while(reader.hasNext())
{ {
String line = reader.nextLine(); String line = reader.nextLine();
System.out.println(line); System.out.println(line);
} }
reader.close(); } catch (FileNotFoundException e) {
}
catch (FileNotFoundException e)
{
throw new RuntimeException(e);
}
}
} }
} }
@@ -7,18 +7,16 @@ import java.util.List;
public class ReadFileExample02 { public class ReadFileExample02 {
static void main() { static void main() {
try Path path = Path.of("quotes.txt");
{ try {
Path path = Path.of("example.txt");
List<String> lines = Files.readAllLines(path); List<String> lines = Files.readAllLines(path);
for(String line: lines)
for(String line : lines)
{ {
System.out.println(line); System.out.println(line);
} }
} } catch (IOException e) {
catch (IOException e) throw new RuntimeException(e);
{
System.err.println("Something went wrong");
} }
} }
} }
@@ -5,21 +5,31 @@ import java.io.IOException;
public class WriteFileExample01 { public class WriteFileExample01 {
static void main() { 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'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.write("I am running away from my responsibilities. And it feels good.\n");
fileWriter.close(); fileWriter.close();
} catch (IOException e) { System.out.println("Done.");
}
catch (IOException e)
{
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }
try
try { {
FileWriter fileWriter = new FileWriter("quotes.txt", true); FileWriter fileWriter = new FileWriter("examples2.txt", true);
fileWriter.write("I DECLARE BANKRUPTCY!"); fileWriter.write("I DECLARE BANKRUPTCY!");
fileWriter.close(); fileWriter.close();
} catch (IOException e) { System.out.println("Done.");
}
catch (IOException e)
{
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }
@@ -11,28 +11,16 @@ import java.util.List;
public class WriteFileExample02 { public class WriteFileExample02 {
static void main() { static void main() {
List<String> lines = List.of(
"Line 1",
"Line 2"
);
Path path = Path.of("examples2.txt");
try { try {
Path path = Path.of("quotes.txt");
List<String> 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); Files.write(path, lines);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); System.err.println(e.getMessage());
}
try {
Path path = Path.of("quotes.txt");
List<String> lines = List.of(
"I DECLARE BANKRUPTCY!"
);
Files.write(path, lines, StandardOpenOption.APPEND);
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
} }