In: Computer Science
Write a program FileCounter.java that reads a text file text1.txt and reports the number of characters and lines contained in the file. For example, the output should be as follows:
Lines: 5 Chars: 124
Note: Write a main() method to test in the FileCounter.java. The file path should use relative path. Must use the provided text file text1.txt.
The text file say
This is an example 1
This is an example 1 2.
This is an example 1 2 3.
This is an example 1 2 3 4.
This is an example 1 2 3 4 5.
2. Suppose you have a binary file geo.dat (see provided data file) of data for a geological survey, such that each record consists of a longitude, a latitude, and an amount of rainfall, all represented by double. Write a program GeologicalData.java to read this file’s data and print them on the screen, one record per line.
Note: Write a main() method to test the code in the GeologicalData.java. The file path should use relative path and the actually file path should be passed into the method getData(String fileName) in the main() method. Must use the provided file geo.dat which has already contained the following data.
-143.78914479979002 59.482245058443084 17.645846630300042 -117.80303714838257 -25.94559103704657 4.513879725440151 49.63942128783603 129.1664680590585 6.825838082783708 -79.66606860669573 -18.800024954102696 9.501515919083086 62.82522223390336 -60.409595996464006 2.6984128008196984
The partial Coding is
public class FileCounter {
}
The other Coding
public class GeologicalData {
public static void getData(String fileName){
}
}
(1.)
Java Program:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class FileCounter
{
public static void main(String[] args) throws FileNotFoundException {
String fileName = "text1.txt";
int character_count = 0, line_count = 0;
Scanner in = new Scanner(new File(fileName));
while (in.hasNextLine()) {
String line = in.nextLine();
character_count += line.length();
line_count += 1;
}
System.out.println("Lines : " + line_count + " Chars : " + character_count);
}
}
text1.txt File
This is an example 1
This is an example 1 2.
This is an example 1 2 3.
This is an example 1 2 3 4.
This is an example 1 2 3 4 5.
Output:
(2.)
Java Program:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class GeologicalData
{
public static void main(String[] args) throws FileNotFoundException {
String fileName = "geo.dat";
getData(fileName);
}
public static void getData(String fileName) throws FileNotFoundException {
int character_count = 0, line_count = 0;
Scanner in = new Scanner(new File(fileName));
System.out.println("Longitude Latitude Amount of rainfall");
System.out.println("===========================================================");
while (in.hasNextLine()) {
String line = in.nextLine();
System.out.println(line);
}
}
}
geo.dat File
-143.78914479979002 59.482245058443084 17.645846630300042
-117.80303714838257 -25.94559103704657 4.513879725440151
49.63942128783603 129.1664680590585 6.825838082783708
-79.66606860669573 -18.800024954102696 9.501515919083086
62.82522223390336 -60.409595996464006 2.6984128008196984
Output:
Thumbs Up Please !!!
THANK YOU!! PLEASE VOTE