DWQA Ask QuestionCategory: JavaAssignment-Level_Medium: Parse text file using File handling concepts in java
admin Staff asked 5 years ago
  1. There is a log file. See attached.
  2. You need to save it in your local system.
  3. Use Java folder and files classes to do the following:
    1. Check file is present, if not then print file not found.
    2. if present then open file and print how many times error is coming and print all the errors as well.

Hint: There are two ways to two do it. You should try with both ways.

a. Using iteration on each row

b. Use regular expression.

 

admin Staff replied 5 years ago

Here is the log file. Copy the content in a text file.
*********************COPY FROM BELOW***************************
vncServer.cpp : trying port number 5900
Sun Nov 19 07:11:42 2006
vncSockConnect.cpp : started socket connection thread
Sun Nov 19 12:03:45 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file / requested
vncHTTPConnect.cpp : sending main page
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
Sun Nov 19 12:03:52 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /favicon.ico requested
Sun Nov 19 12:03:53 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /VncViewer.jar requested
Sun Nov 19 12:03:55 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /VncViewer.jar requested
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /VncViewer.jar requested
Sun Nov 19 12:04:05 2006
vncSockConnect.cpp : accepted connection from 127.0.0.1
vncClient.cpp : client connected : 127.0.0.1 (id 1)
vncClient.cpp : authentication failed
Sun Nov 19 12:04:08 2006
vncSockConnect.cpp : accepted connection from 127.0.0.1
vncClient.cpp : client connected : 127.0.0.1 (id 1)
Sun Nov 19 12:04:10 2006
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
vncMenu.cpp : KillActiveDesktop
Sun Nov 19 12:04:25 2006
vncDesktop.cpp : display resolution or desktop changed.
Sun Nov 19 12:15:41 2006
vncClient.cpp : client disconnected : 127.0.0.1 (id 1)
vncServer.cpp : deleting desktop server
Sun Nov 19 12:15:44 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file / requested
vncHTTPConnect.cpp : sending main page
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
Sun Nov 19 12:15:52 2006
vncSockConnect.cpp : accepted connection from 127.0.0.1
vncClient.cpp : client connected : 127.0.0.1 (id 1)
vncMenu.cpp : KillActiveDesktop
Sun Nov 19 12:30:45 2006
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
vncClient.cpp : client disconnected : 127.0.0.1 (id 1)
vncServer.cpp : deleting desktop server
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
Sun Nov 19 20:15:35 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file / requested
vncHTTPConnect.cpp : sending main page
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
Sun Nov 19 20:15:43 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /favicon.ico requested
Sun Nov 19 20:15:44 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /VncViewer.jar requested
Sun Nov 19 20:15:45 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /VncViewer.jar requested
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file /VncViewer.jar requested
Sun Nov 19 20:15:51 2006
vncSockConnect.cpp : accepted connection from 127.0.0.1
vncClient.cpp : client connected : 127.0.0.1 (id 1)
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
vncMenu.cpp : KillActiveDesktop
Sun Nov 19 20:15:55 2006
vncDesktop.cpp : display resolution or desktop changed.
Sun Nov 19 20:57:29 2006
vncDesktop.cpp : display resolution or desktop changed.
Sun Nov 19 21:00:59 2006
vncDesktop.cpp : display resolution or desktop changed.
Sun Nov 19 21:01:29 2006
vncClient.cpp : client disconnected : 127.0.0.1 (id 1)
vncServer.cpp : deleting desktop server
Sun Nov 19 22:55:50 2006
vncSockConnect.cpp : accepted connection from 127.0.0.1
vncClient.cpp : client connected : 127.0.0.1 (id 1)
Sun Nov 19 22:56:31 2006
vncHTTPConnect.cpp : HTTP client connected
Mon Nov 20 00:25:31 2006
vncHTTPConnect.cpp : HTTP client connected
Mon Nov 20 00:25:41 2006
vncSockConnect.cpp : accepted connection from 127.0.0.1
vncClient.cpp : client connected : 127.0.0.1 (id 1)
Mon Nov 20 00:48:57 2006
vncHTTPConnect.cpp : HTTP client connected
vncHTTPConnect.cpp : file / requested
vncHTTPConnect.cpp : sending main page
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
Mon Nov 20 00:49:04 2006
vncSockConnect.cpp : accepted connection from 127.0.0.1
vncClient.cpp : client connected : 127.0.0.1 (id 1)
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
vncService.cpp : SelectHDESK failed to close old desktop 28, error=170
vncMenu.cpp : KillActiveDesktop
Mon Nov 20 00:49:08 2006
vncDesktop.cpp : display resolution or desktop changed.
Mon Nov 20 00:55:34 2006
vncClient.cpp : client disconnected : 127.0.0.1 (id 1)
vncServer.cpp : deleting desktop server

1 Answers
Bindiya Patil Staff answered 5 years ago

public class File_Handling_Demo {
static int count=0;
public static List<String> readFileInList(String fileName) throws IOException
{

List<String> lines = Collections.emptyList();
try
{
lines =
Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
}

catch (FileNotFoundException e)
{

System.out.println(“file not found”);
//
}
return lines;
}
public static void main(String[] args) throws IOException
{
List l = readFileInList(“C:\\Users\\shri sai\\Desktop\\file_handling.txt”);

Iterator<String> itr = l.iterator();
while (itr.hasNext())
System.out.println(itr.next());
}
}