Windows Event log file - java

I have a program which reads a file every time and now I want to read the event log file of windows ,how can I get the location and read its contents.
The logs are stored in the %SystemRoot%\System32\Config directory with a .evt extension. Within the Computer Manager you can also export them to a .txt or .csv file.
Windows Vista/7/Server2008 location, here it is: %SystemRoot%\system32\winevt\logs
My Code is:
String fileSeperator = File.separator;
String filePath = "C:" + fileSeperator + "WINDOWS" + fileSeperator + "system32" + fileSeperator + "winevent" + fileSeperator + "logs";
System.out.println("FilePath :" + filePath);
File f = new File(filePath);
System.out.println("Is Directory :" + f.isDirectory());
The Output:
FilePath : C:\WINDOWS\system32\winevent\logs
Is Directory :false
Why does it return it is not a directory?

Because the file doesn't exist. You said yourself that the location was %SystemRoot%\system32\winevt\logs. Yet you use C:\WINDOWS\system32\winevent\logs.
winevt != winevent.

Related

space is replaced by %20 in file path while creating file and file is created at new location

I have installed my software in below mention path.
i am getting resulting path of directory created at different location as my installation path contains space. can some one help me how to resolve this issue.
installation path :
/home/test/glh/QA oist/
expected endpoint reference directory :
/home/test/glh/QA oist/server/Tomcat/webapps/ibis/WEB-INF/services
resulting endpoint reference directory :
/home/test/glh/QA%20oist/server/Tomcat/webapps/ibis/WEB-INF/services
File repDir = new File(axisConf.getRepository().getFile());
String serviceName = IISUtilsHandler.replaceChars(module.getModuleName(), " ", "");
File serviceNameDir = new File(repDir + File.separator + "services" + File.separator + serviceName);
if ((moduleProperties.getBoolProperty("ValidateResponse", false) || moduleProperties.getBoolProperty("ValidateRequest", false))
&& moduleProperties.containsProperty("SchemaFileGenerationError")) {
String schemaGenerationError = moduleProperties.getProperty("SchemaFileGenerationError");
throw new IException("WebServiceConnector.Deploy.ErrorBecauseSchemaGenerationFailed", schemaGenerationError);
}
File serviceDir = new File(serviceNameDir, "META-INF");
if (!serviceDir.mkdirs()) {
throw new InubitException("CreateDirError", serviceDir.toString());
}
IISFileHandler.writeStringToFile(serviceDir + File.separator + "services.xml", createServiceXml(moduleProperties, module));
IISFileHandler.writeStringToFile(serviceDir + File.separator + "service.wsdl", moduleProperties.getProperty("WsdlData"));
Please share more details and sample code you are using to get/generate installation file path.
However you can add below java code to replace special character (%20) with space on the fly
File dir = new File( new URI(installation_path.replaceAll(" ", "%20")) );

Android : Copying a file to the internal storage. File not found

I'm trying to backup my database file on the internal storage :
File fromFile = new File(context.getDatabasePath("database.db").getPath());
FileChannel fromFileChannel = new FileInputStream(fromFile).getChannel();
File toFile = new File(context.getFilesDir() + "/database.db");
if (toFile.getParentFile() != null)
toFile.getParentFile().mkdirs();
FileChannel toFileChannel = new FileOutputStream(toFile).getChannel();
Log.i("LOG",fromFileChannel.transferTo(0, fromFileChannel.size(), toFileChannel)+"");
fromFileChannel.close();
toFileChannel.close();
The log returns "69632" without any error, so it looks like it worked.
The problem is that I can't find the file. And the /Android/data folder doesn't contain a folder with my app package name. What am I doing wrong?
Use
File toFile = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Android/data" + File.separator + context.getPackageName() + File.separator + "/database.db");
context.getFilesDir() will give you the directory private to your app, so the files in that will not be available to other apps.

Deleting specified file

I'm trying to delete files but it isn't working or I'm missing something.
Here is a little test I'm doing:
private void deleteFromDir(String filename) {
String path = "./test/pacientes/" + filename + ".tds";
File f = new File(path);
System.out.println("Abs path " + f.getAbsolutePath());
System.out.println("Exist " + f.exists());
System.out.println("Filename " + f.getName());
System.out.println("Delete " + f.delete());
}
And the system prints:
Abs path C:\Users\XXXX\Documents\PAI\TSoft.\test\pacientes\John Smith.tds
Exist true
Filename John Smith.tds
Delete false
And of course isn't deleting the file, why? How can I make it work?
Perhaps, you do not have the permission to delete this file. You can use the Files.delete() method, which throws an IOException, in case something goes wrong, to see what the real problem is.

Can't read files on network shared folder [webbapp is over tomcat as a service]

I've a web application on Tomcat 7.0.57 on Windows 8.1 running it as a service, that is instructed to copy a file on a locally network shared directory, for example \\Network_machine\Shared_folder, but it's not able to do so.
I am able to access the shared folder in windows explorer without any authentication, as it has permission to read for everyone. The following code is checking for the directory exist:
String sourceURL = this.servletContext.getInitParameter("sourceURL");
log.debug("---------->Source reports directory : " + sourceURL);
File sourceDir = new File(sourceURL);
log.debug("---------->Source directory exists : "+sourceDir.exists());
if (sourceDir.exists()) {
String files[] = sourceDir.list();
log.debug("----------> Total files in source dir :" + files.length);
List<String> newFiles = new ArrayList<String>(Arrays.asList(files));
newFiles.removeAll(processedFiles);
log.debug("----------> Latest files in source dir :" + newFiles.size());
File file = null;
if (newFiles.size() > 0) {
for (String fileName : newFiles) {
file = new File(sourceURL + Constants.FILE_SEP + fileName);
latestSourceFiles.add(file);
}
}
} else {
log.debug("Source reports directory " + sourceURL + " is not found");
}
Additional Info:
log - Source directory exists is returning 'FALSE'.
As Kenneth says, you need to map the shared folder to a drive and then write directly to that drive.
http://windows.microsoft.com/en-us/windows/create-shortcut-map-network-drive

Construct File Path using Java

I am trying to create a file, but file path is constructed through String Concats that uses some internal variables and labels, I am getting following error :
Exception in thread "main" java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at CopyEJ.CopyEJ.main(CopyEJ.java:133)
Is there a standard approach to built such files ?
String s_path = text_dir + "\\" + time_stmp + "_" + "Session" + "_" + file_name;
File ssw = new File(s_path);
ssw.createNewFile(); //Errors Out here
You need to create the folder (directory) first:
String s_path = text_dir + "/" + time_stmp + "_" + "Session" + "_" + file_name;
File ssw = new File(s_path);
ssw.getParentFile().mkdirs();
ssw.createNewFile();
This will help you:
String path = "D://abc" + filename+ ".txt";
System.out.println("Path--- " + path);
File file = new File(path);
file.createNewFile()
If You are using Java 1.7 you can rewrite it as follows:
Path s_path = Paths.get(text_dir, time_stmp + "_" + "Session" + "_" + file_name);
Files.createDirectories(s_path.getParent());
File ssw = s_path.toFile();
ssw.createNewFile();
Paths.get() will use default system path separator.

Categories