Delete old files before adding new files in directory java without using FileUtils [duplicate]












-2
















This question already has an answer here:




  • Delete all files in directory (but not directory) - one liner solution

    9 answers




I am writing new files into a directory on daily basis.



BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));


Before creating new files, I want to delete the previously added files existing in the folder. And the previous files will not override with new ones because the file names are different. How can I achieve this?










share|improve this question















marked as duplicate by Phil Ross, Robert Kock, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Do you want to delete all files within that directory or only those whose name follow a specific pattern?

    – Robert Kock
    Nov 21 '18 at 9:40











  • If you want the directory empty, list the files in the directory and delete them all. Otherwise you'd also need some sort of filename matching.

    – Mark Jeronimus
    Nov 21 '18 at 9:41













  • @RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.

    – Richa Sharma
    Nov 21 '18 at 9:42













  • You can delete that directory, create again and save your new files.

    – htpvl
    Nov 21 '18 at 9:42











  • @htpvl Can you tell how you are going to delete the directory (with files in it)?

    – prasad_
    Nov 21 '18 at 10:08


















-2
















This question already has an answer here:




  • Delete all files in directory (but not directory) - one liner solution

    9 answers




I am writing new files into a directory on daily basis.



BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));


Before creating new files, I want to delete the previously added files existing in the folder. And the previous files will not override with new ones because the file names are different. How can I achieve this?










share|improve this question















marked as duplicate by Phil Ross, Robert Kock, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Do you want to delete all files within that directory or only those whose name follow a specific pattern?

    – Robert Kock
    Nov 21 '18 at 9:40











  • If you want the directory empty, list the files in the directory and delete them all. Otherwise you'd also need some sort of filename matching.

    – Mark Jeronimus
    Nov 21 '18 at 9:41













  • @RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.

    – Richa Sharma
    Nov 21 '18 at 9:42













  • You can delete that directory, create again and save your new files.

    – htpvl
    Nov 21 '18 at 9:42











  • @htpvl Can you tell how you are going to delete the directory (with files in it)?

    – prasad_
    Nov 21 '18 at 10:08
















-2












-2








-2









This question already has an answer here:




  • Delete all files in directory (but not directory) - one liner solution

    9 answers




I am writing new files into a directory on daily basis.



BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));


Before creating new files, I want to delete the previously added files existing in the folder. And the previous files will not override with new ones because the file names are different. How can I achieve this?










share|improve this question

















This question already has an answer here:




  • Delete all files in directory (but not directory) - one liner solution

    9 answers




I am writing new files into a directory on daily basis.



BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));


Before creating new files, I want to delete the previously added files existing in the folder. And the previous files will not override with new ones because the file names are different. How can I achieve this?





This question already has an answer here:




  • Delete all files in directory (but not directory) - one liner solution

    9 answers








java file bufferedwriter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:46







Richa Sharma

















asked Nov 21 '18 at 9:37









Richa SharmaRicha Sharma

34




34




marked as duplicate by Phil Ross, Robert Kock, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Phil Ross, Robert Kock, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:01


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Do you want to delete all files within that directory or only those whose name follow a specific pattern?

    – Robert Kock
    Nov 21 '18 at 9:40











  • If you want the directory empty, list the files in the directory and delete them all. Otherwise you'd also need some sort of filename matching.

    – Mark Jeronimus
    Nov 21 '18 at 9:41













  • @RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.

    – Richa Sharma
    Nov 21 '18 at 9:42













  • You can delete that directory, create again and save your new files.

    – htpvl
    Nov 21 '18 at 9:42











  • @htpvl Can you tell how you are going to delete the directory (with files in it)?

    – prasad_
    Nov 21 '18 at 10:08





















  • Do you want to delete all files within that directory or only those whose name follow a specific pattern?

    – Robert Kock
    Nov 21 '18 at 9:40











  • If you want the directory empty, list the files in the directory and delete them all. Otherwise you'd also need some sort of filename matching.

    – Mark Jeronimus
    Nov 21 '18 at 9:41













  • @RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.

    – Richa Sharma
    Nov 21 '18 at 9:42













  • You can delete that directory, create again and save your new files.

    – htpvl
    Nov 21 '18 at 9:42











  • @htpvl Can you tell how you are going to delete the directory (with files in it)?

    – prasad_
    Nov 21 '18 at 10:08



















Do you want to delete all files within that directory or only those whose name follow a specific pattern?

– Robert Kock
Nov 21 '18 at 9:40





Do you want to delete all files within that directory or only those whose name follow a specific pattern?

– Robert Kock
Nov 21 '18 at 9:40













If you want the directory empty, list the files in the directory and delete them all. Otherwise you'd also need some sort of filename matching.

– Mark Jeronimus
Nov 21 '18 at 9:41







If you want the directory empty, list the files in the directory and delete them all. Otherwise you'd also need some sort of filename matching.

– Mark Jeronimus
Nov 21 '18 at 9:41















@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.

– Richa Sharma
Nov 21 '18 at 9:42







@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.

– Richa Sharma
Nov 21 '18 at 9:42















You can delete that directory, create again and save your new files.

– htpvl
Nov 21 '18 at 9:42





You can delete that directory, create again and save your new files.

– htpvl
Nov 21 '18 at 9:42













@htpvl Can you tell how you are going to delete the directory (with files in it)?

– prasad_
Nov 21 '18 at 10:08







@htpvl Can you tell how you are going to delete the directory (with files in it)?

– prasad_
Nov 21 '18 at 10:08














2 Answers
2






active

oldest

votes


















0














You can delete all existing files from the given directory using this one liner code,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);


So, just place this line of code before your BufferedWriter line, like this,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));





share|improve this answer





















  • 1





    thanks @Pushpesh it worked:)

    – Richa Sharma
    Nov 22 '18 at 9:18











  • Glad to help Richa :) It showed me that you accepted my answer then suddenly I saw it got unaccepted. Any reasons? You didn't find my solution good enough?

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:20






  • 1





    accidently clicked accept icon twice.

    – Richa Sharma
    Nov 22 '18 at 9:34



















0














The following code can be helpful.



(This is just the idea of what you can do and I'm not sure about bugs.)



public class rename 
{
public static void main(String argv) throws IOException
{
// Path of folder where files are located
String folder_path =
"C:\Users\Anannya Uberoi\Desktop\myfolder";

// creating new folder
File myfolder = new File(folder_path);

File file_array = myfolder.listFiles();
for (int i = 0; i < file_array.length; i++)
{

if (file_array[i].isFile())
{

File myfile = new File(folder_path +
"\" + file_array[i].getName());
myfile.delete();
}
}
}
}





share|improve this answer


























  • thanks, @hamid this worked for me.

    – Richa Sharma
    Nov 22 '18 at 9:17


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can delete all existing files from the given directory using this one liner code,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);


So, just place this line of code before your BufferedWriter line, like this,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));





share|improve this answer





















  • 1





    thanks @Pushpesh it worked:)

    – Richa Sharma
    Nov 22 '18 at 9:18











  • Glad to help Richa :) It showed me that you accepted my answer then suddenly I saw it got unaccepted. Any reasons? You didn't find my solution good enough?

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:20






  • 1





    accidently clicked accept icon twice.

    – Richa Sharma
    Nov 22 '18 at 9:34
















0














You can delete all existing files from the given directory using this one liner code,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);


So, just place this line of code before your BufferedWriter line, like this,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));





share|improve this answer





















  • 1





    thanks @Pushpesh it worked:)

    – Richa Sharma
    Nov 22 '18 at 9:18











  • Glad to help Richa :) It showed me that you accepted my answer then suddenly I saw it got unaccepted. Any reasons? You didn't find my solution good enough?

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:20






  • 1





    accidently clicked accept icon twice.

    – Richa Sharma
    Nov 22 '18 at 9:34














0












0








0







You can delete all existing files from the given directory using this one liner code,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);


So, just place this line of code before your BufferedWriter line, like this,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));





share|improve this answer















You can delete all existing files from the given directory using this one liner code,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);


So, just place this line of code before your BufferedWriter line, like this,



Arrays.asList(new File("C:\difftest\newfiles").listFiles()).stream().forEach(File::delete);
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\difftest\newfiles\"+decisionList.get(i)+"_"+i+".txt"));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 10:01

























answered Nov 21 '18 at 9:54









Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

7,6032927




7,6032927








  • 1





    thanks @Pushpesh it worked:)

    – Richa Sharma
    Nov 22 '18 at 9:18











  • Glad to help Richa :) It showed me that you accepted my answer then suddenly I saw it got unaccepted. Any reasons? You didn't find my solution good enough?

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:20






  • 1





    accidently clicked accept icon twice.

    – Richa Sharma
    Nov 22 '18 at 9:34














  • 1





    thanks @Pushpesh it worked:)

    – Richa Sharma
    Nov 22 '18 at 9:18











  • Glad to help Richa :) It showed me that you accepted my answer then suddenly I saw it got unaccepted. Any reasons? You didn't find my solution good enough?

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:20






  • 1





    accidently clicked accept icon twice.

    – Richa Sharma
    Nov 22 '18 at 9:34








1




1





thanks @Pushpesh it worked:)

– Richa Sharma
Nov 22 '18 at 9:18





thanks @Pushpesh it worked:)

– Richa Sharma
Nov 22 '18 at 9:18













Glad to help Richa :) It showed me that you accepted my answer then suddenly I saw it got unaccepted. Any reasons? You didn't find my solution good enough?

– Pushpesh Kumar Rajwanshi
Nov 22 '18 at 9:20





Glad to help Richa :) It showed me that you accepted my answer then suddenly I saw it got unaccepted. Any reasons? You didn't find my solution good enough?

– Pushpesh Kumar Rajwanshi
Nov 22 '18 at 9:20




1




1





accidently clicked accept icon twice.

– Richa Sharma
Nov 22 '18 at 9:34





accidently clicked accept icon twice.

– Richa Sharma
Nov 22 '18 at 9:34













0














The following code can be helpful.



(This is just the idea of what you can do and I'm not sure about bugs.)



public class rename 
{
public static void main(String argv) throws IOException
{
// Path of folder where files are located
String folder_path =
"C:\Users\Anannya Uberoi\Desktop\myfolder";

// creating new folder
File myfolder = new File(folder_path);

File file_array = myfolder.listFiles();
for (int i = 0; i < file_array.length; i++)
{

if (file_array[i].isFile())
{

File myfile = new File(folder_path +
"\" + file_array[i].getName());
myfile.delete();
}
}
}
}





share|improve this answer


























  • thanks, @hamid this worked for me.

    – Richa Sharma
    Nov 22 '18 at 9:17
















0














The following code can be helpful.



(This is just the idea of what you can do and I'm not sure about bugs.)



public class rename 
{
public static void main(String argv) throws IOException
{
// Path of folder where files are located
String folder_path =
"C:\Users\Anannya Uberoi\Desktop\myfolder";

// creating new folder
File myfolder = new File(folder_path);

File file_array = myfolder.listFiles();
for (int i = 0; i < file_array.length; i++)
{

if (file_array[i].isFile())
{

File myfile = new File(folder_path +
"\" + file_array[i].getName());
myfile.delete();
}
}
}
}





share|improve this answer


























  • thanks, @hamid this worked for me.

    – Richa Sharma
    Nov 22 '18 at 9:17














0












0








0







The following code can be helpful.



(This is just the idea of what you can do and I'm not sure about bugs.)



public class rename 
{
public static void main(String argv) throws IOException
{
// Path of folder where files are located
String folder_path =
"C:\Users\Anannya Uberoi\Desktop\myfolder";

// creating new folder
File myfolder = new File(folder_path);

File file_array = myfolder.listFiles();
for (int i = 0; i < file_array.length; i++)
{

if (file_array[i].isFile())
{

File myfile = new File(folder_path +
"\" + file_array[i].getName());
myfile.delete();
}
}
}
}





share|improve this answer















The following code can be helpful.



(This is just the idea of what you can do and I'm not sure about bugs.)



public class rename 
{
public static void main(String argv) throws IOException
{
// Path of folder where files are located
String folder_path =
"C:\Users\Anannya Uberoi\Desktop\myfolder";

// creating new folder
File myfolder = new File(folder_path);

File file_array = myfolder.listFiles();
for (int i = 0; i < file_array.length; i++)
{

if (file_array[i].isFile())
{

File myfile = new File(folder_path +
"\" + file_array[i].getName());
myfile.delete();
}
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 9:47









Mark

3,69921126




3,69921126










answered Nov 21 '18 at 9:45









hamid ghasemihamid ghasemi

6791624




6791624













  • thanks, @hamid this worked for me.

    – Richa Sharma
    Nov 22 '18 at 9:17



















  • thanks, @hamid this worked for me.

    – Richa Sharma
    Nov 22 '18 at 9:17

















thanks, @hamid this worked for me.

– Richa Sharma
Nov 22 '18 at 9:17





thanks, @hamid this worked for me.

– Richa Sharma
Nov 22 '18 at 9:17



Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$