473,416 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to overwrite a file in Java

I'm writing a program that needs to overwrite a file at the end of each usage. Currently, the program work as a whole, except at the end of each game, when the overwrite method(shown below) is called, the file is not overwritten unless the user goes to the file, where a pop up box asks if you want to overwrite the file. Is there a way I can either delete the file at the beginning of the method or make the file automatically be written over without asking the player? Here is my code for that method currently:

Expand|Select|Wrap|Line Numbers
  1. private File learningFile; //previously in code
  2. learningFile = new File("learningFile"); //previously in code
  3.  
  4. public void overWriteLearning() 
  5.  { 
  6.   learningFile.delete();
  7.   FileOutputStream fout;  
  8.   try
  9.   {
  10.       fout = new FileOutputStream ("learningFile");
  11.       int a;
  12.    int b; 
  13.    int c;
  14.  
  15.     for( a=0; a<4; a++)
  16.     {
  17.      for(b=0; b<6; b++)
  18.      {
  19.       for(c=0; c<8; c++)
  20.       {
  21.        new PrintStream(fout).println (learning[a][b][c]);
  22.       }
  23.       c=0;
  24.      }
  25.      b=0;
  26.     }
  27.       fout.close();  
  28.   }
  29.   // Catches any error conditions
  30.   catch (IOException e)
  31.   {
  32.    System.err.println ("Unable to write to file");
  33.    System.exit(-1);
  34.   }
  35.  }
  36.  
May 23 '10 #1
2 6290
Dheeraj Joshi
1,123 Expert 1GB
Please try

Expand|Select|Wrap|Line Numbers
  1. fout = new FileOutputStream ("learningFile",false);
  2.  
BTW,
Expand|Select|Wrap|Line Numbers
  1. fout = new FileOutputStream ("learningFile");
  2.  
This should also overwrite the file. Probably i did not understood your question :)

Regards
Dheeraj Joshi
May 24 '10 #2
jkmyoung
2,057 Expert 2GB
Is there a particular reason why the learningFile.delete(); call isn't working?
Do you get any error when calling it?

How is the player "going to the file" ? Do you have some sort of filechooser?

I don't think we're seeing the correct part of the code.
May 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Simon Wigzell | last post by:
I recently wrote a program with MS Visual Studio C++, sent it off to the client where it didn't run, after some probing I discover they are on a Mac! My program is a MSF interface that is really...
2
by: Steven | last post by:
Hello, I'm a student at the university of Antwerp Belgium and I'm writing a program for java web start that has to support plugins. Now my question is why does java web start download 3 versions...
0
by: Peter Royle | last post by:
Hello, I have a deployment project (VB.NET - I don't know whether the language matters) and I have a file in it which goes in the user's folder. This file gets modified by the application, so I...
0
by: snkssa | last post by:
Hi, I got the following error while building an application with ant. please can you give the solution or reason why it is failing? Thank you create_zip_linux: ...
0
by: rajuak12 | last post by:
I am trying to generate xml from java file.Then I wanted to use the genererated xml file to generate java wrapper class and format it with XSL. For example: interface Hello { void...
41
by: Chris Lasher | last post by:
A friend of mine with a programming background in Java and Perl places each class in its own separate file in . I informed him that keeping all related classes together in a single file is more in...
1
by: banging | last post by:
Hi there, I have a question regarding locking of tables so that when two or more people try to write or update the mysql tables, it locks up. Basically I only want one person to write to the...
2
by: praveenkumarvpk | last post by:
Hi friends Please help me! Following is my servlet-code import java.util.Enumeration; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
3
by: vijaykumardahiya | last post by:
Hello To Every One, I want to know that when I upload the File like a image from html page Its not show on servlet page using appropriate logic. I read the FileUpload Home page.But I am still...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.