How to delete the contents of a text file without deleting

I want to copy the contents of file 'A' to file 'B'. after copying is complete, I want to clear the contents of file "A" and want to write on it from the very beginning. I can not delete the file "A", because it is associated with some other task.

I managed to copy the contents using the java file API (readLine ()), but I donโ€™t know how to clear the contents of the file and set the file pointer to the beginning of the file.

+81
java android
Aug 09 2018-11-11T00:
source share
17 answers

Just print an empty line in the file:

PrintWriter writer = new PrintWriter(file); writer.print(""); writer.close(); 
+127
Aug 09 '11 at 10:20
source share

I do not believe that you even need to write an empty line to a file.

 PrintWriter pw = new PrintWriter("filepath.txt"); pw.close(); 
+65
Dec 15 '13 at 5:27
source share

You want the setLength () method in the RandomAccessFile class.

+30
Aug 09 '11 at 10:20
source share

Just donโ€™t write anything!

 FileOutputStream writer = new FileOutputStream("file.txt"); writer.write(("").getBytes()); writer.close(); 
+17
Aug 09 '11 at 10:11
source share

One insert for trimming:

 FileChannel.open(Paths.get("/home/user/file/to/truncate"), StandardOpenOption.WRITE).truncate(0).close(); 

Additional information is available in the Java documentation: https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html

+10
Apr 12 '16 at 8:04 on
source share

After copying from A to B, open file A again to write the mode, and then write an empty line to it

+5
Aug 09 '11 at 10:17
source share

Just write:

 FileOutputStream writer = new FileOutputStream("file.txt"); 
+5
Jun 20 '13 at 14:09
source share

How about below:

 File temp = new File("<your file name>"); if (temp.exists()) { RandomAccessFile raf = new RandomAccessFile(temp, "rw"); raf.setLength(0); } 
+5
Jan 15 '15 at 10:07
source share

One of the best companions for java is Apache Projects and please refer to it. To work with files, you can refer to the Commons IO project.

Below one line code will help us make the file empty.

 FileUtils.write(new File("/your/file/path"), "") 
+4
Sep 20 '14 at 18:10
source share

Write a blank line to the file, clean and close. Make sure the file writer is not in add mode. I think this was supposed to do the trick.

+2
Aug 09 '11 at 10:11
source share

If you do not need to use the author, then the shortest and cleanest way to do this will be as follows:

 new FileWriter("/path/to/your/file.txt").close(); 
+1
Mar 13 '17 at 3:48
source share

you can use

 FileWriter fw = new FileWriter(/*your file path*/); PrintWriter pw = new PrintWriter(fw); pw.write(""); pw.flush(); pw.close(); 

Do not forget to use

 FileWriter fw = new FileWriter(/*your file path*/,true); 

True in the constructor constructor allows you to add.

0
Nov 23 '14 at 1:45
source share
 FileOutputStream fos = openFileOutput("/*file name like --> one.txt*/", MODE_PRIVATE); FileWriter fw = new FileWriter(fos.getFD()); fw.write(""); 
0
Mar 22 '17 at 19:16
source share

With Try-with-Resources Writer will be automatically closed:

 import org.apache.commons.lang3.StringUtils; final File file = new File("SomeFile"); try (PrintWriter writer = new PrintWriter(file)) { writer.print(StringUtils.EMPTY); } // here we can be sure that writer will be closed automatically 
0
May 16 '18 at 14:55
source share

using: New Java 7 NIO library, try

  if(!Files.exists(filePath.getParent())) { Files.createDirectory(filePath.getParent()); } if(!Files.exists(filePath)) { Files.createFile(filePath); } // Empty the file content writer = Files.newBufferedWriter(filePath); writer.write(""); writer.flush(); 

The above code checks if Directoty exists, if it does not create a directory, checks if the file exists, yes, it writes an empty line and clears the buffer, at the end you get a writer that points to an empty file

0
Aug 26 '18 at 9:33
source share

All you have to do is open the file in truncate mode. Any class of Java files will automatically do this for you.

0
Aug 26 '19 at 15:16
source share

you can write a generic method as (its too late, but below code will help you / others)

 public static FileInputStream getFile(File fileImport) throws IOException { FileInputStream fileStream = null; try { PrintWriter writer = new PrintWriter(fileImport); writer.print(StringUtils.EMPTY); fileStream = new FileInputStream(fileImport); } catch (Exception ex) { ex.printStackTrace(); } finally { writer.close(); } return fileStream; } 
-2
Apr 7 '13 at 18:39
source share



All Articles