Isaac Sam Camilleri
Isaac Sam Camilleri

Reputation: 21

How to delete content from a text file?

I would like to delete the content of a text file cause don't need its content but I need the text file for the code to work.

import java.io.*;
import javax.swing.JOptionPane;

class code extends program{
   String [][] stock= new String[10][10];

   void show_stock()throws IOException{
       BufferedReader br= new BufferedReader(new FileReader("C:\\Users\\Isaac Sam Camilleri\\Stock\\Documents\\stock.txt"));
       for(int i=0; i<=9;i++){
           stock[i][0]=br.readLine();
           stock[0][i]=br.readLine();
       }//load stock from document
       for(int i=0;i<=9;i++){
         System.out.println(stock[i][0] + " " + stock[0][i]);
       }
   }
} 

Upvotes: 1

Views: 90

Answers (1)

Kaplan
Kaplan

Reputation: 3728

opening a file for writing and closing it sets the file-size to 0

void show_stock() throws IOException {
  …
  try( FileWriter fw = new FileWriter( "C:\\Users\\Isaac Sam Camilleri\\Stock\\Documents\\stock.txt" ) ) {
  }
}

Upvotes: 1

Related Questions