feat: complete all files
This commit is contained in:
+3
-1
@@ -36,4 +36,6 @@ build/
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
|
||||
examples*.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!
|
||||
@@ -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.
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<List<Genre>>(){}.getType();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -11,28 +11,16 @@ import java.util.List;
|
||||
|
||||
public class WriteFileExample02 {
|
||||
static void main() {
|
||||
List<String> lines = List.of(
|
||||
"Line 1",
|
||||
"Line 2"
|
||||
);
|
||||
|
||||
|
||||
Path path = Path.of("examples2.txt");
|
||||
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);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user