- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to truncate a file in Java?
The flush() method of the FileWriter class flushes the contents of the file. You can use this method to truncate a file.
Example
import java.io.File; import java.io.FileWriter; public class FileTruncate { public static void main(String args[]) throws Exception { File file = new File("myData"); FileWriter fw = new FileWriter(file, false); fw.flush(); System.out.println("File truncated"); } }
Output
File truncated
Advertisements