Skip to content

Commit 3795ec7

Browse files
committed
lesson026
1 parent 7ed54d5 commit 3795ec7

24 files changed

+590
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.bilgeadam.boost.java.lesson026;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public class FileExample {
7+
8+
public static void main(String[] args) {
9+
10+
File myFile = new File("C:\\Users\\semih\\Desktop\\FileExample");
11+
if (myFile.isFile()) {
12+
System.out.println("Is a file.");
13+
}
14+
15+
if (myFile.isDirectory()) {
16+
System.out.println("Is a folder");
17+
String[] files = myFile.list();
18+
for (String fileName : files) {
19+
System.out.println(fileName);
20+
File file = new File(myFile, fileName);
21+
if (file.isFile()) {
22+
System.out.println(file.getName());
23+
System.out.println(file.getAbsolutePath());
24+
System.out.println(file.canExecute() ? "Erisebilir " : "Erisilemez");
25+
System.out.println(file.isHidden() ? "Gizli" : "Gizli degil");
26+
System.out.println(file.canRead() ? "Okunabilir" : "Okunamaz");
27+
System.out.println(file.canWrite() ? "Yazilabilir" : "Yazilamaz");
28+
System.out.println("\n==============================");
29+
30+
}
31+
}
32+
33+
File newFile = new File("C:\\Users\\semih\\Desktop\\FileExample\\My Third File.txt");
34+
if (newFile.exists()) {
35+
System.err.println("Dosya zaten var");
36+
}
37+
else {
38+
boolean success = false;
39+
try {
40+
success = newFile.createNewFile();
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
if (success) {
45+
System.out.println("Dosya yaratildi");
46+
}else {
47+
System.out.println("Dosya yaratilamadi");
48+
}
49+
}
50+
}
51+
52+
}
53+
54+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.bilgeadam.boost.java.lesson026;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
9+
public class FileReaderExample {
10+
public static void main(String[] args) {
11+
12+
// File myFile = new File ("mektup.txt"); // göreceli/relative erişim. Programın çalıştığı yerden okur.
13+
File myFile = new File(
14+
"C:\\Users\\semih\\Desktop\\eclipse-workspace\\Java\\src\\com\\bilgeadam\\boost\\java\\lesson026\\mektup.txt"); // absolute/mutlak erişim.
15+
16+
if (!myFile.exists()) {
17+
System.err.println("Dosya yok");
18+
System.exit(0);
19+
}
20+
21+
FileReader fileReader = null;
22+
BufferedReader reader = null;
23+
try {
24+
fileReader = new FileReader(myFile); // dosyayı okumak için bir katman oluşturdum.
25+
reader = new BufferedReader(fileReader); // dosyayı daha hızlı okumak için bir ceket giydirdik.
26+
while (true) {
27+
String line = reader.readLine();
28+
if (line == null) {
29+
break;
30+
}
31+
else {
32+
System.out.println("==> "+line);
33+
}
34+
}
35+
} catch (FileNotFoundException ex) {
36+
ex.printStackTrace();
37+
} catch (IOException ex) {
38+
ex.printStackTrace();
39+
} finally {
40+
if (fileReader != null) {
41+
try {
42+
fileReader.close();
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
}
46+
}if (reader != null) {
47+
try {
48+
reader.close();
49+
} catch (IOException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
}
54+
}
55+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.bilgeadam.boost.java.lesson026;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.DataInputStream;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
8+
public class ReadDataExample {
9+
10+
public static void main(String[] args) {
11+
12+
File inputFile = new File("C:\\Users\\semih\\Desktop\\FileExample\\values.dat");
13+
try (DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(inputFile)));) {
14+
while (true) {
15+
Double value = in.readDouble();
16+
System.out.println(value);
17+
}
18+
19+
} catch (Exception e) {
20+
System.err.println("Something went wrong! Cause: " + e.getMessage());
21+
}
22+
23+
}
24+
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.bilgeadam.boost.java.lesson026;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.BufferedReader;
5+
import java.io.DataOutputStream;
6+
import java.io.File;
7+
import java.io.FileOutputStream;
8+
import java.io.FileReader;
9+
10+
public class ReadWriteExample {
11+
12+
public static void main(String[] args) {
13+
14+
File inputFile = new File("C:\\Users\\semih\\Desktop\\FileExample\\Ölçüm Değerleri.txt");
15+
File outputFile = new File("C:\\Users\\semih\\Desktop\\FileExample\\values.dat");
16+
17+
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
18+
FileOutputStream fos = new FileOutputStream(outputFile);
19+
BufferedOutputStream bos = new BufferedOutputStream(fos);
20+
DataOutputStream out = new DataOutputStream(bos);
21+
) {
22+
while (true) {
23+
String line = reader.readLine();
24+
if (line == null) {
25+
break;
26+
}
27+
28+
out.writeDouble(Double.parseDouble(line));
29+
System.out.println(line);
30+
}
31+
} catch (Exception e) {
32+
System.err.println("Something went wrong! Cause: " + e.getMessage());
33+
}
34+
35+
}
36+
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.bilgeadam.boost.java.lesson026;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
9+
public class TryWithResourcesExample {
10+
public static void main(String[] args) {
11+
12+
// File myFile = new File ("mektup.txt"); // göreceli/relative erişim. Programın çalıştığı yerden okur.
13+
File myFile = new File(
14+
"C:\\Users\\semih\\Desktop\\eclipse-workspace\\Java\\src\\com\\bilgeadam\\boost\\java\\lesson026\\mektup.txt"); // absolute/mutlak
15+
// erişim.
16+
17+
if (!myFile.exists()) {
18+
System.err.println("Dosya yok");
19+
System.exit(0);
20+
}
21+
22+
try (FileReader fileReader = new FileReader(myFile); BufferedReader reader = new BufferedReader(fileReader);) {
23+
while (true) {
24+
String line = reader.readLine();
25+
if (line == null) {
26+
break;
27+
} else {
28+
System.out.println("==> " + line);
29+
}
30+
}
31+
} catch (FileNotFoundException ex) {
32+
ex.printStackTrace();
33+
} catch (IOException ex) {
34+
ex.printStackTrace();
35+
}
36+
37+
}
38+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.bilgeadam.boost.java.lesson026.example.file;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public class CreateAFile {
7+
8+
public static void main(String[] args) {
9+
10+
File file = new File("C:\\Users\\semih\\Desktop\\eclipse-workspace\\Java\\src\\com\\bilgeadam\\boost\\java\\lesson026\\example\\output.txt");
11+
12+
try {
13+
boolean value = file.createNewFile();
14+
15+
if(value) {
16+
System.out.println("The new file is created");
17+
}else {
18+
System.out.println("The file already exists.");
19+
}
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
24+
}
25+
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bilgeadam.boost.java.lesson026.example.file;
2+
3+
import java.io.File;
4+
5+
public class DeleteAFile {
6+
7+
public static void main(String[] args) {
8+
9+
File file = new File("newFile.txt");
10+
11+
boolean value = file.delete();
12+
13+
if (value) {
14+
System.out.println("The file is deleted");
15+
} else {
16+
System.out.println("The file is not deleted");
17+
}
18+
}
19+
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.bilgeadam.boost.java.lesson026.example.file;
2+
3+
4+
import java.io.FileReader;
5+
6+
public class ReadAFile {
7+
8+
public static void main(String[] args) {
9+
10+
char[] array = new char[100];
11+
12+
try {
13+
FileReader input = new FileReader("input.txt");
14+
input.read(array);
15+
System.out.println("Data in the file: ");
16+
System.out.println(array);
17+
input.close();
18+
} catch (Exception e) {
19+
e.printStackTrace();
20+
}
21+
22+
}
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.bilgeadam.boost.java.lesson026.example.file;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
public class WriteToAFile {
7+
8+
public static void main(String[] args) {
9+
10+
String data = "This is the data in the output file";
11+
12+
try {
13+
FileWriter output = new FileWriter("output.txt");
14+
output.write(data);
15+
System.out.println("Data is written to the file.");
16+
output.close();
17+
} catch (IOException e) {
18+
e.printStackTrace();
19+
}
20+
21+
22+
}
23+
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is the data in the Input file

0 commit comments

Comments
 (0)