pls help!! can u give me some tips how to delete a string or list in a textfile based on this code....i created this code but i dont know how to delete it..pls explain in simple english because im not American..... :D tnx a lot...ur response is very much appreciated... =)

import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TextFile
{
	public static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
	public static String[] person;
	public static String[] tempPerson;
	public static String[] sepTemp;
	public static int xCtr;

	public static void mainMenu()
	{
		System.out.println("1] Insert");
		System.out.println("2] Delete"); ////////////////
		System.out.println("3] Exit");///////////////
	}
public static void insertData() throws IOException ///insert
	{
		System.out.print("Enter name: ");
		String pName = stdin.readLine();
		String temp;
		
		if (xCtr != 0)
		{
	
			temp =tempPerson[0] + ", " + pName;	
				
		}
		else
		{
			temp = pName;
			xCtr = 1;
		}	
			
		FileOutputStream x = new FileOutputStream("myTextFile.dat");
		ObjectOutputStream y = new ObjectOutputStream(x);
		y.writeObject(new String[] {temp});
		
		System.out.println("New Entry Updated...");

		y.close();
	}
	
	public static void displayInitData() //displayInitial data
	{
		try
		{

				FileInputStream x = new FileInputStream("myTextFile.dat");
				ObjectInputStream y = new ObjectInputStream(x);
				tempPerson = (String[])(y.readObject());
				
				sepTemp = new String[100000];
				sepTemp = tempPerson[0].split(", ");
			
			
				int i = 0;
				
				do 
				{
					person[i] = sepTemp[i];
					i = i + 1;
				}
				while(i<sepTemp.length);
				
			
				xCtr = i;
				
			
		}
		catch(Exception m)
		{
			m.getMessage();
		}
		
	}
	
	public static void displayData() 		//display data
	{
		int i=0;
		
		do 
		{
			System.out.println(sepTemp[i]);
			i = i + 1;
		}
		while(i<sepTemp.length);		
	}
public static void main (String[] args) throws IOException, Exception
	{
		int choiceMenu;
		
		person = new String[100000];
		tempPerson = new String[100000];
		displayInitData();
	
		do
		{
			mainMenu();
			System.out.print("Enter Choice: ");
			choiceMenu = Integer.parseInt(stdin.readLine());
			
			switch(choiceMenu)
			{
				case 1:	displayInitData();
						insertData();
						break;
				case 2:delete();
						break;
								case 3:
						if (xCtr > 0)
						{
							displayInitData();
							displayData();
						}
						else
							System.out.println("No Record");
						break;
				default:
			}
		}
		while(choiceMenu!=3);
		
    }
}

Read the file and write a new file leaving out the pieces that should be "deleted". Then delete the original file and rename the newfile to the the name the oldfile had.

hey thanks for the reply.......what about update? hehehe..just improving my program....tnx a lot.

This article has been dead for over six months. Start a new discussion instead.