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.
Related
I've tried numerous forms but I'm not getting the size of my photos at all, they're coming out with more than 3mb and I need them to upload numerous pictures via FTP. Follow code for someone could demonstrate a functional gradient
File createImageFile() throws IOException {
Logger.getAnonymousLogger().info("Generating the image - method started");
// New Image
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
String imageFileName = os_id+ "_"+ txtCheckList_id.getText().toString() + "_" + txtCheckListItens_id.getText().toString() + "_1A_" + timeStamp;
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "GRP");
Logger.getAnonymousLogger().info("Storage directory set");
if (!storageDirectory.exists()) storageDirectory.mkdir();
File image = new File(storageDirectory, imageFileName + ".png");
Logger.getAnonymousLogger().info("File name and path set");
mImageFileLocation = image.getAbsolutePath();
return image;
}
You can simply use file.length() to get the size of the file, please check the mentioned example,
int file_size = Integer.parseInt(String.valueOf(file.length()/1024));
i am creating a file and when i create that file, i check if it already exists. If it already exists, i want to create it with the same name, but with the (1) after it. I am able to do that and here is the code :
File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + ".apk");
if(apkReceived.exists()){
apkReceived=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + "(1)"+".apk");
}
byte[] buffer = new byte [8192];
FileOutputStream fos=new FileOutputStream(apkReceived);
then it would continue... (i am writing things on the file).
This works but the problem is that in this situation :
FileTest.apk
FileTest(1).apk
If I receive another Filetest, it will sub my FileTest(1), since it will create it again.
A solution for this would be to check if the file exists again, but then i would have to be doing that for ever.
My goal would be to create (1) and then (2) , etc.
Does any one of you know how to do this ?
EDIT: Obviously i could use a cicle to check it. The problem is on how to get the (1) and then the (2) and don't get the (1)(2)
To avoid reinventing the wheel I suggest using Timestamp it hardly ever will have collisions.
java.util.Date date= new java.util.Date();
Timestamp tstamp = new Timestamp(date.getTime());
File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + tstamp + ".apk");
Do Something like this
File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + ".apk");
if(apkReceived.exists()){
int new_int_postfix;
//Below _MAX is max numbers of file eg. _MAX = 100
for(int i = 1; i < _MAX; i++) {
apkReceived = = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName +"("+ i +")"+".apk");
if(!apkReceived.exists()) {
String []name_without_pre = receivedApkName.split("\\(");
receivedApkName = name_without_pre[0];
new_int_postfix = i;
break;
}
}
apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + "("+new_int_postfix+")"+".apk");
}
byte[] buffer = new byte [8192];
FileOutputStream fos=new FileOutputStream(apkReceived);
Some pseudocode to get you started:
Fetch a list of all files in the directory
For the one you want to copy: check if you already have one or more copies
If you already have "file_(n)"; use "file_(n+1)" as new filename.
Obviously: you should clarify your requirements on the "maximum" n you want to allow; and what to happen when n copies were created; and another is asked for.
If you only store that one type of file in your directory you can do:
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
count the size and size + 1 for your next filename.
you can also separate each file with similiar filename on their own directory.
try this
String filename =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"+receivedApkName + ".apk";
File f = new File(filename);
String extension = ".apk";
int g = 0;
while(f.exists()) {
int i = f.lastIndexOf('.');
if (i > 0)
{ extension = fileName.substring(i+1); }
f.renameTo(f.getPath() + "\" + (f.getName() + g) + "." + extension)
}
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();
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.