Skip to content

Commit d3d799f

Browse files
Merge pull request #3 from jayanpraveen/master
added file handling
2 parents 050d140 + 4b6597a commit d3d799f

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

FileHandling/FileWriterDemo.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jayan;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class FileWriterDemo {
7+
public static void main(String[] args) throws IOException {
8+
String pathToFile;
9+
pathToFile = "/Users/jayanpraveen/Desktop/hello-world/FILE_NAME.txt";
10+
11+
File file = new File(pathToFile);
12+
FileWriter writer = new FileWriter(file, true);
13+
14+
writer.write("Hello\n");
15+
writer.write("Java\n");
16+
17+
writer.close();
18+
}
19+
}

FileHandling/PrintWriterDemo.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jayan;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class PrintWriterDemo {
7+
public static void main(String[] args)
8+
throws IOException {
9+
10+
String pathToFile;
11+
pathToFile = "set-path-file.txt";
12+
13+
File file = new File(pathToFile);
14+
PrintWriter printWriter = new PrintWriter(file);
15+
16+
printWriter.print("Hello");
17+
printWriter.println("Java");
18+
printWriter.println(123);
19+
printWriter.printf("You have %d %s", 400, "gold coins");
20+
21+
printWriter.close();
22+
23+
}
24+
25+
// File file = new File("/Users/jayanpraveen/Desktop/hello-world/printwriter.txt");
26+
// try(
27+
// PrintWriter printWriter = new PrintWriter(file))
28+
// {
29+
// printWriter.print("Hello"); // prints a string
30+
// printWriter.println("Java"); // prints a string and then terminates the line
31+
// printWriter.println(123); // prints a number
32+
// printWriter.printf("You have %d %s", 400, "gold coins"); // prints a formatted string
33+
// }catch(
34+
// IOException e)
35+
// {
36+
// System.out.printf("An exception occurs %s", e.getMessage());
37+
// }
38+
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.jayan;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class countEvenNumsFileDemo {
7+
public static void main(String[] args)
8+
{
9+
String pathToFile;
10+
pathToFile = "set-path-file.txt";
11+
12+
int countWords = 0;
13+
14+
try
15+
{
16+
File file = new File(pathToFile);
17+
Scanner scanner = new Scanner(file);
18+
int value = scanner.nextInt();
19+
20+
while (scanner.hasNext())
21+
{
22+
23+
System.out.print(value + " ");
24+
25+
if (value % 2 == 0)
26+
countWords ++;
27+
28+
value = scanner.nextInt();
29+
30+
}
31+
System.out.println("WORDS: " + countWords);
32+
}
33+
catch(FileNotFoundException e)
34+
{
35+
System.out.println("No file found:" );
36+
}
37+
}
38+
}

FileHandling/readFileDemo.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.jayan;
2+
3+
import java.io.IOException;
4+
import java.nio.file.*;
5+
// import java.nio.file.Paths;
6+
7+
public class readFileDemo
8+
{
9+
public static String readFileAsString(String fileName) throws IOException
10+
{
11+
return new String(Files.readAllBytes(Paths.get(fileName)));
12+
}
13+
14+
public static void main(String[] args)
15+
{
16+
String pathToHelloWorldJava = "set-path-file.txt";
17+
try
18+
{
19+
System.out.println(readFileAsString(pathToHelloWorldJava));
20+
21+
}
22+
catch (IOException e)
23+
{
24+
System.out.println("Cannot read file: " + e.getMessage());
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)