Delete old files before adding new files in directory java without using FileUtils [duplicate]
up vote
-2
down vote
favorite
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?
java file bufferedwriter
marked as duplicate by Phil Ross, Robert Kock, Mark Rotteveel
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 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.
add a comment |
up vote
-2
down vote
favorite
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?
java file bufferedwriter
marked as duplicate by Phil Ross, Robert Kock, Mark Rotteveel
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 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 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 at 9:41
@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.
– Richa Sharma
Nov 21 at 9:42
You can delete that directory, create again and save your new files.
– htpvl
Nov 21 at 9:42
@htpvl Can you tell how you are going to delete the directory (with files in it)?
– prasad_
Nov 21 at 10:08
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
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?
java file bufferedwriter
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
java file bufferedwriter
edited Nov 21 at 9:46
asked Nov 21 at 9:37
Richa Sharma
34
34
marked as duplicate by Phil Ross, Robert Kock, Mark Rotteveel
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 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
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 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 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 at 9:41
@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.
– Richa Sharma
Nov 21 at 9:42
You can delete that directory, create again and save your new files.
– htpvl
Nov 21 at 9:42
@htpvl Can you tell how you are going to delete the directory (with files in it)?
– prasad_
Nov 21 at 10:08
add a comment |
Do you want to delete all files within that directory or only those whose name follow a specific pattern?
– Robert Kock
Nov 21 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 at 9:41
@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.
– Richa Sharma
Nov 21 at 9:42
You can delete that directory, create again and save your new files.
– htpvl
Nov 21 at 9:42
@htpvl Can you tell how you are going to delete the directory (with files in it)?
– prasad_
Nov 21 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 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 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 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 at 9:41
@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.
– Richa Sharma
Nov 21 at 9:42
@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.
– Richa Sharma
Nov 21 at 9:42
You can delete that directory, create again and save your new files.
– htpvl
Nov 21 at 9:42
You can delete that directory, create again and save your new files.
– htpvl
Nov 21 at 9:42
@htpvl Can you tell how you are going to delete the directory (with files in it)?
– prasad_
Nov 21 at 10:08
@htpvl Can you tell how you are going to delete the directory (with files in it)?
– prasad_
Nov 21 at 10:08
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
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"));
1
thanks @Pushpesh it worked:)
– Richa Sharma
Nov 22 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 at 9:20
1
accidently clicked accept icon twice.
– Richa Sharma
Nov 22 at 9:34
add a comment |
up vote
0
down vote
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();
}
}
}
}
thanks, @hamid this worked for me.
– Richa Sharma
Nov 22 at 9:17
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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"));
1
thanks @Pushpesh it worked:)
– Richa Sharma
Nov 22 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 at 9:20
1
accidently clicked accept icon twice.
– Richa Sharma
Nov 22 at 9:34
add a comment |
up vote
0
down vote
accepted
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"));
1
thanks @Pushpesh it worked:)
– Richa Sharma
Nov 22 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 at 9:20
1
accidently clicked accept icon twice.
– Richa Sharma
Nov 22 at 9:34
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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"));
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"));
edited Nov 21 at 10:01
answered Nov 21 at 9:54
Pushpesh Kumar Rajwanshi
3,1411823
3,1411823
1
thanks @Pushpesh it worked:)
– Richa Sharma
Nov 22 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 at 9:20
1
accidently clicked accept icon twice.
– Richa Sharma
Nov 22 at 9:34
add a comment |
1
thanks @Pushpesh it worked:)
– Richa Sharma
Nov 22 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 at 9:20
1
accidently clicked accept icon twice.
– Richa Sharma
Nov 22 at 9:34
1
1
thanks @Pushpesh it worked:)
– Richa Sharma
Nov 22 at 9:18
thanks @Pushpesh it worked:)
– Richa Sharma
Nov 22 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 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 at 9:20
1
1
accidently clicked accept icon twice.
– Richa Sharma
Nov 22 at 9:34
accidently clicked accept icon twice.
– Richa Sharma
Nov 22 at 9:34
add a comment |
up vote
0
down vote
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();
}
}
}
}
thanks, @hamid this worked for me.
– Richa Sharma
Nov 22 at 9:17
add a comment |
up vote
0
down vote
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();
}
}
}
}
thanks, @hamid this worked for me.
– Richa Sharma
Nov 22 at 9:17
add a comment |
up vote
0
down vote
up vote
0
down vote
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();
}
}
}
}
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();
}
}
}
}
edited Nov 21 at 9:47
Mark
2,7011722
2,7011722
answered Nov 21 at 9:45
hamid ghasemi
626423
626423
thanks, @hamid this worked for me.
– Richa Sharma
Nov 22 at 9:17
add a comment |
thanks, @hamid this worked for me.
– Richa Sharma
Nov 22 at 9:17
thanks, @hamid this worked for me.
– Richa Sharma
Nov 22 at 9:17
thanks, @hamid this worked for me.
– Richa Sharma
Nov 22 at 9:17
add a comment |
Do you want to delete all files within that directory or only those whose name follow a specific pattern?
– Robert Kock
Nov 21 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 at 9:41
@RobertKock I want to delete all files,i.e, empty the entire directory before performing bufferedWriter.
– Richa Sharma
Nov 21 at 9:42
You can delete that directory, create again and save your new files.
– htpvl
Nov 21 at 9:42
@htpvl Can you tell how you are going to delete the directory (with files in it)?
– prasad_
Nov 21 at 10:08