File already existing - repeated name - java

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)
}

Related

How to correct File .exists() method [duplicate]

This question already has answers here:
Is there a difference between x++ and ++x in java?
(18 answers)
Closed 1 year ago.
I am using the .exists() method to make sure a file does not exists and if it does, it adds a number to the file. However, it is not working and creating the file using the original filename and essentially overwriting the previous when creating another one.
This is my code:
int count = 1;
String name = "myFileName" + count;
String path = "/pathway/";
String ext = ".txt";
File file = new File(path + name + ext);
if (file.exists()) {
System.out.println("file found!");
name = "myFileName" + (count++);
file = new File(path + name + ext);
}
System.out.println("wrote file: " + name);
The output is this whenever I run it over again:
[1:47:40:359] wrote file: myFileName1
[1:47:40:361] wrote file: myFileName1
Your mistake is the count++. The postfixed ++ operator first 'returns' the value and then increments it. You need to first increment, and then return. Do this with the prefixed ++ operator: ++count
A while loop should help, to ensure, that you create a new file, even if the previous existed, even if a second or third also exists.
int count = 1;
String name = "myFileName" + count;
String path = "/pathway/";
String ext = ".txt";
File file = new File(path + name + ext);
while (file.exists()) {
System.out.println("file found!");
name = "myFileName" + (++count);
file = new File(path + name + ext);
}
System.out.println("wrote file: " + name);

Java create multiple new files

I read this question here How to create a file in a directory in java?
I have a method that creates a QR Code. The method is called several times, depends on user input.
This is a code snippet:
String filePath = "/Users/Test/qrCODE.png";
int size = 250;
//tbd
String fileType = "png";
File myFile = new File(filePath);
The problem: If the user types "2" then this method will be triggered twice.
As a result, the first qrCODE.png file will be replaced with the second qrCODE.png, so the first one is lost.
How can I generate more than one qr code with different names, like qrCODE.png and qrCODE(2).png
My idea:
if (!myFile.exists()) {
try {
myFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Any tips?
EDIT: I solved it by using a for loop and incrementing the number in the filename in every loop step.
You can create more files eg. like follows
int totalCount = 0; //userinput
String filePath = "/Users/Test/";
String fileName= "qrCODE";
String fileType = "png";
for(int counter = 0; counter < totalCount; counter++){
int size = 250;
//tbd
File myFile = new File(filePath+fileName+counter+"."+fileType);
/*
will result into files qrCODE0.png, qrCODE1.png, etc..
created at the given location
*/
}
Btw to add check if file exists is also good point.
{...}
if(!myFile.exists()){
//file creation
myFile.createNewFile()
}else{
//file already exists
}
{...}
Your idea of solving the problem is a good one. My advice is to break up the filePath variable into a few variables in order to manipulate the file name easier. You can then introduce a fileCounter variable that will store the number of files created and use that variable to manipulate the name of the file.
int fileCounter = 1;
String basePath = "/Users/Test/";
String fileName = "qrCODE";
String fileType = ".png";
String filePath = basePath + fileName + fileType;
File myFile = new File(filePath);
You can then check if the file exists and if it does you just give a new value to the filePath variable and then create the new file
if(myFile.exists()){
filePath = basePath + fileName + "(" + ++fileCounter + ")" + fileType;
myFile = new File(filePath);
}
createFile(myFile);
And you're done!
You can check /Users/Test direcroty before create file.
String dir = "/Users/Test";
String pngFileName = "qrCode";
long count = Files.list(Paths.get(dir)) // get all files from dir
.filter(path -> path.getFileName().toString().startsWith(pngFileName)) // check how many starts with "qrCode"
.count();
pngFileName = pngFileName + "(" + count + ")";

Create multiple copies of a singlle file in java

i have a fileserver and client and want to rename files, if they already exists in downloadfolder. what is the best way to do that? I tried that code but it always create one copy and the next copy overwrites the first one.
File f = new File(FILE_DIR + fileName);
if(f.exists()) {
System.out.print("file already exists");
fileName = "copy_of_" + fileName;
}
In your class you declare :
private static int X = 0;
and then change the code to this:
File f = new File(FILE_DIR + fileName);
if(f.exists()) {
System.out.print("file already exists");
fileName = "copy_of_ " + X + fileName;
x++;
}
so everytime the x will increase by 1, (x++), so they will have different names.

Java Create new file automatically depend on size [duplicate]

This question already has answers here:
Check file size while writing file and create new file
(2 answers)
Closed 9 years ago.
Trying to Write java Code to write ".dat" File.. While wring check size as well.. every 200 KB create new ".dat" continue to write file. Is there any way that i can do that..
when file more than 200KB and create new one filename like this
1st file name.. 1_filename
2nd file name.. 2_filename
3rd file name.. 3_filename........
or if file create only one file means less than 200KB than
Filenamd.. filename as in code..
I wrote code to write files but.. its only writing 2 files.. and that's not right way to do it..
Here is my code..
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class xmlfile1filebytes {
public static void main(String[] args) throws IOException {
File folder = new File ("07072013");
File[] listOfFiles = folder.listFiles();
System.out.println("There are " + listOfFiles.length + " files");
String filesin;
String timeStamp = new SimpleDateFormat("MM-dd-yyyy[HH.mm.ss]")
.format(Calendar.getInstance().getTime());
System.out.println(timeStamp);
BufferedWriter xmlfile = null;
BufferedWriter datfile = null;
String outxmlfile = ("07072013\\" + timeStamp + ".xml");
xmlfile = new BufferedWriter(new FileWriter(outxmlfile));
String outdatfile = ("07072013\\" + timeStamp + ".dat");
datfile = new BufferedWriter(new FileWriter(outdatfile));
int offset = 0;
int size = 0;
for (int i = 0; i < listOfFiles.length; i++) {
File f = listOfFiles[i];
// System.out.println(i + " " + f.getAbsolutePath());
if (f.isFile()) {
filesin = listOfFiles[i].getName();
if (filesin.endsWith("pdf")) {
Path aPath = Paths.get(f.getAbsolutePath());
System.out.println(filesin);
byte[] actualBytes = Files.readAllBytes(aPath);
size = actualBytes.length;
xmlfile.append((i + 1) + ")" + " File = " + filesin + ", Offset = " + offset + ", Size = " + size + "\n");
offset = offset + size;
xmlfile.newLine();
String s = new String(actualBytes);
datfile.append(s);
datfile.newLine();
File datfileinfolder = new File ("07072013\\" + timeStamp + ".dat");
long datfilesize = datfileinfolder.length();
long datfilesizeinkb = datfilesize /1024;
System.out.println(datfilesizeinkb);
if (datfilesizeinkb >= 200) {
datfile.close();
BufferedWriter datfile1 = null;
String outdatfile1 = ("07072013\\" + "1_"+ timeStamp + ".dat");
datfile1 = new BufferedWriter(new FileWriter(outdatfile1));
String s1 = new String(actualBytes);
datfile1.append(s1);
datfile1.close();
}
}
}
}
xmlfile.close();
}
}
And I get error when write file more than 400 KB..
ERROR:
There are 10 files
07-09-2013[16.03.00]
1192970_eBill_20130709.pdf
96
1321470_eBill_20130709.pdf
208
1724897_eBill_20130709.pdf
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(Unknown Source)
at java.io.BufferedWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at java.io.Writer.append(Unknown Source)
at xmlfile1filebytes.main(xmlfile1filebytes.java:65)
I suggest you create a separate class whose only job it is to create files (in sequence), write data to the file, keep an eye on the size, and close / open files as needed. This will make your code more organized - you will be able to see your mistakes more clearly. The class would have the following methods:
setMax(maxVal) - set the maximum file size (default 400k)
setRoot(rootPath) - folder where files will be created
setName(rootName) - "format string" used to generate file names
(e.g. "myFile%03d.dat")
writeData(dataSize, dataBlock) - write data. If no file open, open it.
If file too big, split it.
closeFile() - flush buffers, close the last file.
Could be part of the destructor
currentFile() - returns name of current file (for debug)
currentSize() - returns current file size (for debug)
If you can figure out how to write this class you will solve your initial problem and have something you can re-use at the same time.
If you want to stay with (mostly) the code structure you already have, then your fix is to remove your current line 85.
83: String s1 = new String(actualBytes);
84: datfile1.append(s1);
85: datfile1.close(); <<<<<<<<<<< remove this line
86: }
Once you have closed the file, your next attempt to write to it will fail - and that's the error you are seeing.
You do datfile.close(), and on the next iteration through the for loop you .append() to it. You'll have to initialize (i.e. open) your datfile inside the for loop.
Okay looks like a book keeping issue.
Here is how it should look like
.
.
.
long datfilesize = datfileinfolder.length();;
final int BLOCK_SIZE = 200 * 1024;
for (int curBlock = 0; curBlock < actualBytes.length; curBlock += BLOCK_SIZE) {
String toWrite = new String(
Arrays.copyOfRange(actualBytes, curBlock, Math.min(curBlock + BLOCK_SIZE, actualBytes.length)
);
String suffix = "";
if (curBlock > 0) {
//append underscores other file information and then perform writes
suffix = String.valueOf(curBlock / BLOCK_SIZE);
}
//Your code more or less
BufferedWriter datfile1 = null;
String outdatfile1 = ("07072013\\" + suffix + timeStamp + ".dat");
datfile1 = new BufferedWriter(new FileWriter(outdatfile1));
datfile1.append(toWrite);
datfile1.close();
}
Edit: Does this make more sense ?
Here's the gist. The for loop iterates in block size of BLOCK_SIZE(200 KB). If file size is less than BLOCK_SIZE, you need not append any suffixes(See the if block). Else you append the suffix and then write to the file. The string is given by the local variable toWrite. You have read the whole file already and stored the bytes in actualBytes. Hope this makes more sense.

Add index to filename for existing file (file.txt => file_1.txt)

I want to add an index to a filename if the file already exists, so that I don't overwrite it.
Like if I have a file myfile.txt and same time myfile.txt exists in destination folder - I need to copy my file with name myfile_1.txt
And same time if I have a file myfile.txt, but destintation folder contains myfile.txt and myfile_1.txt - generated filename has to be myfile_2.txt
So the functionality is very similar to the creation of folders in Microsoft operating systems.
What's the best approach to do that?
Using commons-io:
private static File getUniqueFilename( File file )
{
String baseName = FilenameUtils.getBaseName( file.getName() );
String extension = FilenameUtils.getExtension( file.getName() );
int counter = 1
while(file.exists())
{
file = new File( file.getParent(), baseName + "-" + (counter++) + "." + extension );
}
return file
}
This will check if for instance file.txt exist and will return file-1.txt
You might also benefit from using the apache commons-io library. It has some usefull file manipulation methods in class FileUtils and FilenameUtils.
Untested Code:
File f = new File(filename);
String extension = "";
int g = 0;
int i = f.lastIndexOf('.');
extension = fileName.substring(i+1);
while(f.exists()) {
if (i > 0)
{ f.renameTo(f.getPath() + "\" + (f.getName() + g) + "." + extension); }
else
{ f.renameTo(f.getPath() + "\" + (f.getName() + g)); }
g++;
}
Try this link partly answers your query.
https://stackoverflow.com/a/805504/1961652
DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[]{"**/myfile*.txt"});
scanner.setBasedir("C:/Temp");
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();
once you have got the correct set of files, append a proper suffix to create a new file.

Categories