Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

Complete in Java using IntelliJ

 

Here is the code template

 

Modify the given template java file (Utils.java) to allow it to read a file (MyTextFile.txt) that resides on the VPL server. The contents of the file are
Hello. This is a text file. My first line.
Read the file. As you read the file, count the number of characters in the file. Return that integer value back to the VPL test script.
expand button
Transcribed Image Text:Modify the given template java file (Utils.java) to allow it to read a file (MyTextFile.txt) that resides on the VPL server. The contents of the file are Hello. This is a text file. My first line. Read the file. As you read the file, count the number of characters in the file. Return that integer value back to the VPL test script.
Utils.java
1- import java.io.*;
import java.nio.file.Files;
2
4- public class utils {
1* The method has no input arguments. It only has an output (int).
* You need to specify the file name before reading the file stream. */
7
public static int myMethod ()
{
10 -
11
int bytecounter = 0;
final string fileName = "myTextFile.txt";
12
13
14
15
// This is a try-catch block.
// often used when something could go wrong ...
16
17
like when reading files.
18
19
// The try part: you want this to succeed. Uncomment the try.
/* try */{
20
21 -
22
23
FileInputstream myFileobject
= new FileInputstream(fileName);
24
25
int myData;
26
// in the while loop control parentheses, put a -1 to the right of the !=
// This is because the loop will end when nothing is left to read in the file. (a -1 signal)
while( (myData = myFileobject.read ()) != /* what goes here? */){
system.out.print((char)myData);
bytecounter++;
}
27
28
29 -
30
// the integer is cast to 8-bit char
31
32
33
// Print out the contents.
system.out.println();
34
35
36
// Close the file.
myFileobject.close();
37
38
39
40
// The catch part: catch the program if it fails. Uncomment the catch.
/* catch */(Exception ex)
{
system.out.println("Exception!");
}
41
42
43
44
45
46
// How many 8-bit bytes are there in the text file?
return bytecounter;
47
48
49
50
51 }
expand button
Transcribed Image Text:Utils.java 1- import java.io.*; import java.nio.file.Files; 2 4- public class utils { 1* The method has no input arguments. It only has an output (int). * You need to specify the file name before reading the file stream. */ 7 public static int myMethod () { 10 - 11 int bytecounter = 0; final string fileName = "myTextFile.txt"; 12 13 14 15 // This is a try-catch block. // often used when something could go wrong ... 16 17 like when reading files. 18 19 // The try part: you want this to succeed. Uncomment the try. /* try */{ 20 21 - 22 23 FileInputstream myFileobject = new FileInputstream(fileName); 24 25 int myData; 26 // in the while loop control parentheses, put a -1 to the right of the != // This is because the loop will end when nothing is left to read in the file. (a -1 signal) while( (myData = myFileobject.read ()) != /* what goes here? */){ system.out.print((char)myData); bytecounter++; } 27 28 29 - 30 // the integer is cast to 8-bit char 31 32 33 // Print out the contents. system.out.println(); 34 35 36 // Close the file. myFileobject.close(); 37 38 39 40 // The catch part: catch the program if it fails. Uncomment the catch. /* catch */(Exception ex) { system.out.println("Exception!"); } 41 42 43 44 45 46 // How many 8-bit bytes are there in the text file? return bytecounter; 47 48 49 50 51 }
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY