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.
Related
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")) );
I am using grails 2.1.1. I want to change a file name on a event. That's why I have attempt some. I just want to rename the file. But my process is not working. Can anyone please help me on this please?!!! Here is my attempt below in controller >>>
String previousFileName = inceptionInstance.fileName
StringBuffer newFileName = new StringBuffer(previousFileName);
int start = 0;
int end = previousFileName.indexOf("_");
newFileName.replace(start, end, params.firmName);
File f = new File(grailsApplication.config.files.location.toString() + File.separatorChar + previousFileName);
f.renameTo( new File( grailsApplication.config.files.location.toString() + File.separatorChar + newFileName) )
I have solved the issue. Just take a look at this ...
new File(grailsApplication.config.files.location.toString() + File.separatorChar + previousFileName).renameTo(new File(grailsApplication.config.files.location.toString() + File.separatorChar + newFileName))
I am trying to convert a wav file into mp3 format using the below code. A mp3 file is created but it is blank file without any content. Any ideas what could be causing this.
Format[] FORMATS = new Format[] { new AudioFormat(
AudioFormat.LINEAR) };
String srcFilePath = "C:" + File.separator + "Users"
+ File.separator + "vkiran" + File.separator + "Desktop"
+ File.separator + "test" + File.separator + "4a714ea2.wav";
String mp3FilePath = "C:" + File.separator + "Users"
+ File.separator + "vkiran" + File.separator + "Desktop"
+ File.separator + "test" + File.separator + "4a714ea2.mp3";
File srcFile = new File(srcFilePath);
File destFile = new File(mp3FilePath);
MediaLocator source = new MediaLocator(srcFile.toURL());
DataSource d = Manager.createDataSource(source);
ContentDescriptor cd = new ContentDescriptor(
FileTypeDescriptor.MPEG_AUDIO);
Processor p = Manager.createRealizedProcessor(new ProcessorModel(d,
FORMATS, cd));
p.configure();
System.out.println((p.getDuration()).getSeconds());
MediaLocator destination = new MediaLocator(destFile.toURL());
DataSource ds = p.getDataOutput();
DataSink sink = Manager.createDataSink(p.getDataOutput(),
destination);
sink.open();
sink.start();
sink.stop();
sink.close();
p.stop();
p.close();
Here i'm able to change the name of image but for the new image i'm not getting the same image as first one.
Here my initial image is ABC.png and new image is XYZ.png i'm able to change the image name from ABC.png to XYZ.png but i need XYZ.png as similar to ABC.png just i need to change name of image.
Here is my code
File prevfile = new File(ImagesPath + "/" + oldvalue + ".png");
File nextfile = new File(ImagesPath + "/" + newValue + ".png");
prevfile.renameTo(nextfile);
File prevfile = new File(ImagesPath + "/" + oldvalue + ".png");
File nextfile = new File(ImagesPath + "/" + newValue + ".png");
//prevfile.renameTo(nextfile);
Rather than renameTo function better use
FileUtils.copyFile(prevfile , nextfile );
prevfile .delete();
Rename Image with new name and delete previous name.
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.