-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_7.FileCount.java
More file actions
73 lines (64 loc) · 2.17 KB
/
_7.FileCount.java
File metadata and controls
73 lines (64 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class File_readWrite
{
public static void main(String[] args)
{
try
{
/*creating a new file*/
File f1 = new File("test.txt");
if(f1.createNewFile())
{
System.out.println("**File Created Successfully**");
System.out.println(" Filename : "+f1.getName());
System.out.println(" Readable : "+f1.canRead());
System.out.println(" Writeable : "+f1.canWrite());
}
else
{
System.out.println("An error occurred!");
}
/*writing to the file using object of FileWriter*/
FileWriter writer = new FileWriter("test.txt");
writer.write("This is a test file for demonstrating File Handling in Java!");
writer.close();
System.out.println("-------------------------------------");
System.out.println("Successfully wrote to the file..");
System.out.println("-------------------------------------");
/*reading the file using object of scanner class*/
Scanner sc = new Scanner(f1);
System.out.println("**FILE CONTENT**");
System.out.println();
int character=0, word=0, line=0;
while(sc.hasNextLine())
{
//character++; //counts whitespace also
line++;
word++;
String str = sc.nextLine();
for(int i=0; i<str.length(); i++)
{
if(str.charAt(i)==' ')
word++;
else
character++; //doesnt count whitespace
}
}
System.out.println("No. of characters = "+character);
System.out.println("No. of words = "+word);
System.out.println("No. of lines = "+line);
sc.close();
}
catch(IOException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}
}