i want to overwrite the existing zip file(which mean i add new file in existing zip file) but here show this error (java.util.zip.ZipError: zip END header not found)
private void updateZip(String fileName, String scenarioDirectory){
System.out.println("File Name : " +fileName);
System.out.println("Scenario Directory : " +scenarioDirectory);
String scenarioName ="12345";
Path myFilePath = Paths.get(fileName);
Path zipFilePath = Paths.get(scenarioDirectory);
FileSystem fs;
try {
fs = FileSystems.newFileSystem(zipFilePath,null);
Path fileInsideZipPath = fs.getPath(scenarioName);
Files.copy(myFilePath, fileInsideZipPath);
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try this :
if your file exist then delete it ant after save the new.
Or Added a file directly in the zip code since this example https://stackoverflow.com/a/17500924/4017037
Related
I will get dynamic paths from database. Example: 1.xyz/abc/file1.txt
2.pqr/file2.txt
Now I need to append these paths to existing file (eg:/users/rama/) and save that file
my final directory should like /users/rama/xyz/abc/file1.txt
I am able to create directories such as xyz/abc if they don't exist, but the problem is file1.txt is also created as directory instead of file.
I am able to create directories such as xyz/abc if they don't exist,
but the problem is file1.txt is also created as directory instead of
file.
Because you are creating the directories until *.txt. Below an example of code to acheive what you want:
String prefix = "/users/rama/";
String filePath = "xyz/abc/file1.txt";
// concatenation => /users/rama/xyz/abc/file1.txt
String fullPath = prefix.concat(filePath);
PrintWriter writer;
try {
// Getting the directory path : /users/rama/xyz/abc/
int lastIndexOfSlash = fullPath.lastIndexOf("/");
String path = filePath.substring(0, lastIndexOfSlash);
File file = new File(path);
// If /users/rama/xyz/abc/ don't exist then creating it.
if(!file.exists()) {
file.mkdirs();
}
// Creating the file.
writer = new PrintWriter(fullPath, "UTF-8");
writer.println("content");
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I want to get the filename of the file that do not exist when a file exception occur in my java application so that i can give a short message to the user.
catch (FileNotFoundException e) {
/* what code to put here to get the filename of the file */
}
This should display the non-existing file path:
try {
//access file
} catch(FileNotFoundException e) {
System.out.println(e.getMessage());
}
Output, for creating a Scanner with new Scanner(new File("C:/filetest")):
C:\filetest (The system cannot find the file specified)
You cannot grap properly the filename unless you parse the stacktrace in your catch block.
You can either store the filename outside of the try block, i.e.:
String filename = ...
try {
// process file
} catch (FileNotFoundException e) {
String message = String.format("The file % could not be found.", filename);
// Show message to user
}
Or you can check whether the file exists before trying to access it:
File file = new File(filename);
if (!file.exists()) {
// Show error to user.
}
I am generating a file using the following syntax
File file = new File("input.txt");
The problem is that it is saying that it is writing to the file but I am not able to locate where the file is created, I searched my entire workspace. The expectation was that it would be created in the same folder as my code which is executing.
Any ideas?
Rest of the code :
File file = new File("input.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
You could do a sop on the absolute path and you would get the path:
File file = new File("input.txt");
System.out.println("" + file.getAbsolutePath());
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
When you create file through relative paths, Java uses System.getProperty("user.dir"). So, in your case the full path to file will be System.out.println(System.getProperty("user.dir") + "/input.txt");.
I have a zip file that gets created at runtime that I need to copy to another directory however whenever I run my code I get a DirectoryNotEmptyException. Is there some extra parameter I need to specify to copy into a non-empty directory?
Here's the layout
Path sourceZip = new File(path).toPath();
String destinDir = new File(System.getProperty("user.dir")).getParent();
Path target = Paths.get(destinDir);
try {
Files.copy(sourceZip, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) //DirectoryNotEmptyException occurs here
{}
the destination needs to contain the full path of the file that will eventually be there.
so you say if you needed to copy /home/dauser/faq.txt to /home/faq.txt
File file = new File(path);
Path sourceZip = file.toPath();
StringBuilder sb = new StringBuilder();
sb.append(new File(System.getProperty("user.dir")).getParent());
sb.append("/");
sb.append(file.getName());
Path target = Paths.get(sb.toString());
try {
Files.copy(sourceZip, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) //DirectoryNotEmptyException occurs here
{}
I am trying to create a file inside a directory using the following code:
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("themes", Context.MODE_WORLD_WRITEABLE);
Log.d("Create File", "Directory path"+directory.getAbsolutePath());
File new_file =new File(directory.getAbsolutePath() + File.separator + "new_file.png");
Log.d("Create File", "File exists?"+new_file.exists());
When I check the file system of emulator from eclipse DDMS, I can see a directory "app_themes" created. But inside that I cannot see the "new_file.png" . Log says that new_file does not exist. Can someone please let me know what the issue is?
Regards,
Anees
Try this,
File new_file =new File(directory.getAbsolutePath() + File.separator + "new_file.png");
try
{
new_file.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Create File", "File exists?"+new_file.exists());
But be sure,
public boolean createNewFile ()
Creates a new, empty file on the file system according to the path information stored in this file. This method returns true if it creates a file, false if the file already existed. Note that it returns false even if the file is not a file (because it's a directory, say).
Creating a File instance doesn't necessarily mean that file exists. You have to write something into the file to create it physically.
File directory = ...
File file = new File(directory, "new_file.png");
Log.d("Create File", "File exists? " + file.exists()); // false
byte[] content = ...
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(content);
out.flush(); // will create the file physically.
} catch (IOException e) {
Log.w("Create File", "Failed to write into " + file.getName());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
Or, if you want to create an empty file, you could just call
file.createNewFile();
Creating a File object doesn't mean the file will be created. You could call new_file.createNewFile() if you wanted to create an empty file. Or you could write something to it.