
-----------------------------------
C
Tue Mar 09, 2010 8:57 pm

TextInputFile &amp; TextOutputFile help needed
-----------------------------------
I have a project due soon and it uses a method called 
TextInputFile & TextOutputFile, and I am having trouble deleting an existing value from the text file created. 
For example, I have a text file that have an integer 6 and a String "Hello" stored and I want to delete them both and replace them with another integer and another String. Any ideas on how to do this ?

-----------------------------------
facultyofmusic
Fri Mar 12, 2010 5:43 pm

Re: TextInputFile &amp; TextOutputFile help needed
-----------------------------------
There's no need to delete the data.  Just over write it. When you make a new PrintWriter to a file, it equals deleting all contents and rewriting it from the beginning.

-----------------------------------
Daniel Lee Wilkes
Thu Apr 15, 2010 9:36 pm

Re: TextInputFile &amp; TextOutputFile help needed
-----------------------------------
Hey, I know this is kind of old, but I have a very relevant question.. I want to make a program that stores data such as a high score in a text file. So far I have no problem with that, but when ever I re-run the program, that file it totally overridden. This sounds like what facultyofmusic was describing, but I don't know what to do to not do that. Here is some coding example:

 /* write data to file */
         System.out.print("Enter the file name for storing names and grades: ");
         fileName = input.nextLine();
         System.out.print("How many students? ");
         numStu = input.nextInt();
         try {
            dataFile = new File(fileName);
            out = new FileWriter(dataFile);
            writeFile = new BufferedWriter(out);
            for (int i = 0; i < numStu; i++) {
               System.out.print("Enter student name: ");
               stuName = input.next();
               System.out.print("Enter test score: ");
               score = input.next();
               writeFile.write(stuName);
               writeFile.newLine();
               writeFile.write(score);
               writeFile.newLine();
            }  	
            writeFile.close();
            out.close();
            System.out.println("Data written to file.");
         } 
             catch (IOException e) {
               System.out.println("Problem writing to file.");
               System.err.println("IOException: " + e.getMessage());
            }


I assume most programmers can fill in the blanks, if not, I will paste the whole program. Thank you in advance for the help.

P.S. I am not assuming that the text file already exists. It needs to be created if it isn't already there (which is a simple if statement).
P.P.S. I have no idea what part of the code actually makes the file. I use .creatNewFile() but this program doesn't have that and it runs just fine. I got it from an example from my teacher who got it from a resource book.

-----------------------------------
Insectoid
Fri Apr 16, 2010 9:51 am

RE:TextInputFile &amp; TextOutputFile help needed
-----------------------------------
It's supposed to be overwritten. If you want to save the data, you need to have a reader load the content into RAM, then write to it. If the file doesn't exist, Java will create it. No need for a conditional.
