Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
I/O and Streams
how to replace existing content in afile and update content every time it create file ?
Niti Kapoor
Ranch Hand
Posts: 407
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks i had resolved my problem on my own
String Path="C:/temp"; Path=Path+"/"; File newFile = new File(Path+find4+".ini"); try { newFile.createNewFile(); if (newFile.exists()) { newFile.delete(); } try (FileWriter writer = new FileWriter(newFile,true)) { writer.write(html); writer.flush(); writer.close(); } catch (IOException ex) { Logger.getLogger(HV.class.getName()).log(Level.SEVERE, null, ex); } } catch (IOException ex) { Logger.getLogger(HV.class.getName()).log(Level.SEVERE, null, ex); } } }
by adding file.delete
Dave Tolls
Rancher
Posts: 4801
50
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It would make it easier to follow the code if your indentation was correct, but:
newFile.createNewFile(); if (newFile.exists()) { newFile.delete(); }
Why are you creating the file and then deleting it?
Niti Kapoor
Ranch Hand
Posts: 407
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
because if file will already exist than it delete file and than write to new file
Niti Kapoor
Ranch Hand
Posts: 407
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
if there is no file than it will neglect if part and it directly create file and write
Dave Tolls
Rancher
Posts: 4801
50
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
But that's not what that code is doing.
What happens if you remove the
createNewFile
call?
It doesn't seem to be adding anything.
Niti Kapoor
Ranch Hand
Posts: 407
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
if i remove createNewFile call it will not create file. i had checked that
Dave Tolls
Rancher
Posts: 4801
50
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
But the FileWriter creates the file.
It doesn't need you to call createNew File.
If it can't create the file then it will throw an IOException.
If that is the issue then all you are doing is covering up another problem.
Niti Kapoor
Ranch Hand
Posts: 407
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
its creating a file but its overwriting the file every time
reply
reply
◄
1
2
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Java 8, file search and replace
java.nio.file.NoSuchFileException:
replace last occurrence of string in file
Java Error while opening a large file of 2.2GB in appending mode, inside a zipfile used JAVA7 NIO
XSD to java batch importer
More...