Deleting data from text files

Hi there,
I am having a real battle trying to delete data from a text file I am using. There seems to be this giant cloud with Java, where there is no real easy way to delete text from a file??
I have a JFrame with two textfields on it where you enter the username and pcname. There is a OK button once you have entered those details.
On action event I would like those two details, to be removed from the text file that has been read in.
I have a text file like this:
bob
pc1
bill
pc2
The data above gets written no problem, one at at a time using the below.
File f = new File("\\c:\\filename.txt");
String strToWrite;
try
FileOutputStream fstr = new FileOutputStream(f);
strToWrite = username1+ '\r'+'\n' ;
byte[] bytes = strToWrite.getBytes(); fstr.write(bytes);
strToWrite = pcname1 + '\r'+'\n' ;
byte[] bytes2 = strToWrite.getBytes(); fstr.write(bytes2);
strToWrite = username2+ '\r' +'\n' ;
byte[] bytes3 = strToWrite.getBytes(); fstr.write(bytes3);
strToWrite = pcname2 + '\r'+'\n' ;
byte[] bytes4 = strToWrite.getBytes(); fstr.write(bytes4);
catch (IOException e)
System.out.println("Error -- " + e.toString());
To read them back in I have made the follwoing:
BufferedReader br= null;
Count = 0;
try
br = new BufferedReader(new InputStreamReader(new FileInputStream("\\c:\\filename.txt")));
String s = "";
while((s=br.readLine()) != null )
Count = 1;     
     //System.out.println("Data is " + s);
     inboundtext = s;
     if ( Count == 1 )
break;
while((s=br.readLine()) != null )
//System.out.println("Data is " + s);
inboundtext = s;
Count = 2;
if ( Count == 2 )
break;
catch(IOException ie)
finally
try
     if(br!=null) br.close();
catch(Exception e)
Now the above I know, is reading each line of the text file one at a time.
Now what I would like is that when the text that we got from the textfields, using getText(), be removed from the file without destroying the rest of the records in there? Why is it so easy to add and replace files, but not do any deleting from them?
please help!!

sorry, FileOutputStream fstr = new FileOutputStream(f);
should be FileOutputStream fstr = new FileOutputStream(f, true);

Similar Messages

  • Cant get data from text file to print into Jtable

    Instead of doing JDBC i am using text file as database. I cant get data from text file to print into JTable when i click find button. Goal is to find a record and print that record only, but for now i am trying to print all the records. Once i get that i will change my code to search desired record and print it. when i click the find button nothing happens. Can you please take a look at my code, dbTest() method. thanks.
    void dbTest() {
    DataInputStream dis = null;
    String dbRecord = null;
    String hold;
    try {
    File f = new File("customer.txt");
    FileInputStream fis = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fis);
    dis = new DataInputStream(bis);
    Vector dataVector = new Vector();
    Vector headVector = new Vector(2);
    Vector row = new Vector();
    // read the record of the text database
    while ( (dbRecord = dis.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(dbRecord, ",");
    while (st.hasMoreTokens()) {
    row.addElement(st.nextToken());
    System.out.println("Inside nested loop: " + row);
    System.out.println("inside loop: " + row);
    dataVector.addElement(row);
    System.out.println("outside loop: " + row);
    headVector.addElement("Title");
    headVector.addElement("Type");
    dataTable = new JTable(dataVector, headVector);
    dataTableScrollPane.setViewportView(dataTable);
    } catch (IOException e) {
    // catch io errors from FileInputStream or readLine()
    System.out.println("Uh oh, got an IOException error!" + e.getMessage());
    } finally {
    // if the file opened okay, make sure we close it
    if (dis != null) {
    try {
    dis.close();
    } catch (IOException ioe) {
    } // end if
    } // end finally
    } // end dbTest

    Here's a thread that loads a text file into a JTable:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=315172
    And my reply in this thread shows how you can use a text file as a simple database:
    http://forum.java.sun.com/thread.jsp?forum=31&thread=342380

  • How to delete data from a file using IO package

    Hi All,
    i am trying to remove some content of the file.
    this content is not at starting of file not even at end of file.
    can anybody tell me how can i delete number of lines from file using IO package.

    iam having some data in text file .ex:in flowrist.txt
    12/5/07,500,300,6000 like many set of datas are
    there.In these if i want to delete the data based on
    the date which i specified.How to do this specific
    deletion?You need to open a stream to read in the file and then use the indexOf method provided in the Sting class to check if the line contains the date or whatever String you are looking for, if so then skip that line and store or re-write the lines you wish to keep, as well as some extra lines you may wish to add.
    Take a look below at this example found on Google.
    http://www.java-tips.org/java-se-tips/java.io/how-to-read-file-in-java.html
    The above read a file line by line and prints it to console. You should be able to modify this, instead of using System.out to print the line you should use index of to check the lines for a date/String. Index of return -1 if the String you specify is not in the line you parse.

  • Loading data from text file into ListBox

    I have data in a text file that I want to load into a
    listbox... I have fully mastered handling strings and arrays so I'm
    going to need some help...
    I was wondering how do I get flash to load a text file that
    contains the data below.. and display it line for line like I want
    it to list down the component
    "Launch;7.1.7.6"
    "Engine;7.1.7.6"
    "OSX;7.0.0.2" (or something close to that)
    and I was wondering how do i just get it to take it fromt he
    file.. line for line from where it says exeversion in the file and
    list it in the listbox...
    I'm really thankful to anybody that helps.
    Data in text file:
    quote:
    exeversion=Launch;7.1.7.6;
    exeversion=Engine;7.1.7.6;
    exeversion=LinuxX86;7.0.0.2;
    exeversion=LinuxPPC;7.0.0.2;
    exeversion=LinuxMIPS;7.0.0.2;
    exeversion=OSX;7.0.0.2;
    exeversion=Config;7.1.7.6;
    exeversion=UI;7.1.7.7;
    exeversion=JAVA;7.0.4.5;

    nobody cna help me? i really need to know or have a tutorial
    or something so i can learn from it... i really appreciate anyone
    who helps

  • Charting data from text file in LabVIEW 5.1 for Windows

    I am having some difficulties trying to figure out how to plot a chart with
    data from a file. I would like to be able to read data from a text file
    into a chart using LabVIEW 5.1 for Windows. I have looked in the help
    within the program and online. I have also looked in the user manual and
    the book "LabVIEW for Everyone". Could you please give me specific
    instructions on how I would construct the program to be able to do this.
    Thank you.

    Hi Ellie,
    It depends on how data is stored in that txt file. Usually you load the data into an array either using or , the array if necessary and send it to the graph indicator in required form. If you want a more detailed answer, send me a sample data file ([email protected]).

  • Unable to find file error while loading data from text file to Oracle

    Hi,
    I am having a interface where i am loading a data of Text file to Oracle.
    But when i am trying to do this i am getting following error.
    ODI-1227: Task SrcSet0 (Loading) fails on the source FILE connection SAPMM.
    Caused By: java.sql.SQLException: File not found: d:/mdb/#General.get_filename
         at com.sunopsis.jdbc.driver.file.FileResultSet.<init>(FileResultSet.java:160)
         at com.sunopsis.jdbc.driver.file.impl.commands.CommandSelect.execute(CommandSelect.java:57)
         at com.sunopsis.jdbc.driver.file.CommandExecutor.executeCommand(CommandExecutor.java:33)
    SAPMM is connection name.
    I am using get_filename to get the filename and it is fetching correct value as the this variable refreshes in previous step of this interface.
    KM used for loading is File to SQL
    What would be cause of this error?
    Thanks,
    Mahesh

    Hi,
    Did a testing and following are the result
    I have
    A> created package having steps like
    1. Declaration of variable v_filename.
    2. Refreshing variable v_filename.
    3. Execution of Interface which gets the file name from v_filename and load into target table
    Package executes successfully.
    B> created package having steps like
    1. Declaration of variable v_filename.
    2. Refreshing variable v_filename.
    3. Scenario of Interface which gets the file name from v_filename and load into target table
    Package executes with erre as it is not able to find the file
    C> created package having steps like
    1. Declaration of variable v_filename.
    2. Refreshing variable v_filename.
    3. Execution of Interface which gets the file name from v_filename and load into target table
    4. Now create a scenario of the package , use the generated scenario in another package say main_package
    Execution of main_package is successful.
    Thanks,
    Sutirtha

  • How to extract data from  text file to database table

    Hi ,
    I am trying to upload  data in text file to database table  using GUI_UPLOAD function .what would be the program for that.
    thanks in advance.

    Hi,
    I don't think you have a standard sap program to upload data from file to database table...
    Instead you can create a custom program like this..
    DATA: T_FILEDATA(1000) OCCURS 0 WITH HEADER LINE.
    DATA: T_ZTABLE LIKE ZTABLE OCCURS 0 WITH HEADER LINE.
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        filename                      = 'C:\TEST.TXT'
      tables
        data_tab                      = T_FILEDATA
    EXCEPTIONS
       FILE_OPEN_ERROR               = 1
       FILE_READ_ERROR               = 2
       NO_BATCH                      = 3
       GUI_REFUSE_FILETRANSFER       = 4
       INVALID_TYPE                  = 5
       NO_AUTHORITY                  = 6
       UNKNOWN_ERROR                 = 7
       BAD_DATA_FORMAT               = 8
       HEADER_NOT_ALLOWED            = 9
       SEPARATOR_NOT_ALLOWED         = 10
       HEADER_TOO_LONG               = 11
       UNKNOWN_DP_ERROR              = 12
       ACCESS_DENIED                 = 13
       DP_OUT_OF_MEMORY              = 14
       DISK_FULL                     = 15
       DP_TIMEOUT                    = 16
       OTHERS                        = 17
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    LOOP AT T_FILEDATA.
      T_ZTABLE = T_FILEDATA.
      APPEND T_ZTABLE.
    ENDLOOP.
    MODIFY ZTABLE FROM TABLE T_ZTABLE.
    COMMIT WORK..
    Thanks,
    Naren

  • CO-PA load data from text file

    Hello
    At present, in our company, we load data from Excel files to CO-PA. Is there any way to load those data to CO-PA from text files ?
    I will be grateful for any suggestions.

    You can create a structure using KEFA, assign the Value fields in KEFB and then use KEFC to upload data against that structure.
    You have various file format options, but I don't think Excel is one of them... you have have to save the file as txt and upload.
    Play around with those transactions and come back if you need additional help.
    Cheers.

  • Unable to Upload data from text file into BEx Analyzer selection screen

    Hi,
    No response from BEx Analyzer when I am trying to upload around 40,000 material from text file into BEx Analyzer selection screen using "Upload selections" options. But I am able to upload only 10,000 material from text file.  I never faced same kind of issue when I am using BEx Analyzer 3.x.  Please let me know I have to change any settings related to BEx or any other.
    Thanks
    Sri Krishna Ponnada.

    Hello
    It seems you are reaching the .NET memory limitation informed in note 1040454.
    Because 3.5 does not use .NET it can work that.
    Regards,
    Ricardo

  • To delete Data From a file

    i want to delete lines from a file after reading the line. the next time i read the file i should find a unread line as the 1st line of the file.
    how do i do tat? this is very similar to delete from table name query in sql. but i have to do this with java and txt files. how do i code this??

    You can't delete part of a file. The way to do that in general is like this:
    1. Read a line.
    2. If you want to delete it, do nothing. Otherwise write it to a new file.
    3. Repeat until end-of-file.
    After you've finished all that, the new file will contain everything you didn't want to delete. Now delete the original file and rename the new file to have its name.

  • Remove duplicates while loading data from text file

    Hi,
    Data in text file (some times has duplicates) is being loaded into Oracle 9i database using Informatica. To improve performance, we would like to remove duplicates at the time of each load using Oracle procedure. Could you please help me with this?
    Thanks,
    Lakshmi

    No, our table doesn't have that. Most of the functionality is managed at the informatica level. Is there any other way? Thanks,

  • Import data from text file to cube

    Hi,
    I'm trying to load data from a text file to a cube.
    Just as an experiment, I exported data from a cube into a file. Then wrote a script to import the same file back into the cube - It works fine.
    After this i deleted some time members from the outline and tried loading - but it errored out -
    ERROR MESSAGE : Unknown Member [2007-11-06] in Data Load, [22] Records Completed.
    I want the valid records to be loaded and the invalid ones rejected but this is not happening.
    This is the command i use:
    IMPORT 3 "/PATH/Filename.txt" 4 "n" 2 "IMP001.rul" "n" "/PATH/error.txt";
    I'm not using a rule file for this as it was not working fine with a rule file(IMP001).
    Please let me know how i can proceed with the import by just rejecting the records where members are not found in the outline.
    Thanks in advance,
    Rags.

    Yes, the question wasn't intended to recommend it as a solution, but a recurring need for this type of manipulation could point to a more robust answer without making it overly complex.
    The "end version" of what I envisioned included a temporary cube that included all members ever loaded, and UDA's assigned to the "current set", for use in an export/import process. The export would be the source for the rebuild of the production cube. This end vision seemed a little on the complex side, but it would be robust and work long term. There are obviously different ways to simplify this approach.
    The simplification that my question was aimed for is to eliminate the use of a template outline from a theoretical approach I've used in the past (in a round about way). In essence, if you can leave the members in the production cube and just clear the data, you could use a flag value that is loaded to create a D-C value (say, on scenario) to perform the extract. Loading a "1" would generate output, and a "0" to suppress output (using "scenario" * "flag" for the report value). Deleting the member is thus not needed but done "virtually" with the flag on the output side.
    I know, my crazy thinking is not easy to follow, even for me -- but thinking out of the box serves me well most of the time.

  • Delete data from a file

    Hi,
    I would like to know , how can I delete a line from a text_file,
    and when it is deleted, I don�t want that there is a gap between
    lines.
    Could it be possible to use class RandomAccessFile to do it?
    Please help me.
    thanks.

    You could do it with a BufferedReader. But there's no way to just magically delete the line. No matter what method you use, you'll have to:
    1) Read all the data in the file up to but not including the line, then write it to the new file.
    2) Skip the line you want to delete.
    3) Read all of the data in the file starting after the line you want to delete going to the end of the file, then write that to the new file as well.
    That's pretty general. Those steps could change if you, say, want the input and output file to be the same (in that case, you could just seek to the end of the line and move all the data after the line back to close the gap, then truncate the end of the file off), or if you are implementing stuff a bit different (for example, reading all the lines into a list, removing the lines from the list, and writing them back to a file).
    Jason

  • How to delete data from flat file ?

    Hi all,
    I deleted a record from a tables and I want to delete it from data file.
    Can I do it ?
    Pls help me !

    What do you mean by deleting it from datafile ?, Is it flat file or something else you want to ask? I believe you have records in flat files and you want to delete them as soon as you load them into oracle table.Oracle dosent offer such facility, however you can syncronise your tables and flat file contents by using merge statements and update it in a fashion, so that only records which reflects in flat files syncronises with table in oracle database .
    hare krishna
    Alok

  • Error while loading the data from text file

    Hi,
    I got an error " Data Value Encountered before all Dimensions selected" while loading the data from the text file.
    Can any one please suggest me the solution.

    Possible Solutions
    Make sure that the data source is valid.
    Is a member from each dimension specified correctly in the data source or rules file?
    Is the numeric data field at the end of the record? If not, move the numeric data field in the data source or move the numeric data field in the rules file.
    Are all members that might contain numbers (such as "100") enclosed in quotation marks in the data source?
    If you are using a header, is the header set up correctly? Remember that you can add missing dimension names to the header.
    Does the data source contain extra spaces or tabs?
    Has the updated outline been saved?

Maybe you are looking for

  • PPCI and PM01

    Hi gurus- My understanding about the TC's PPCI and PM01 is as follows: PPCI- Transaction code for creating custom infotypes in OM. PM01- Transaction code for creating custom infotypes in PA. Is that correct or am I missing something?? Thanks. Ramesh.

  • Retrieving Data from Collections

    Greetings to all!!! I am trying to write some java code that creates an ArrayList of Time objects and then reads them back. (this as part of my assignment creating a Squash Court Booking System). The class Time has a method that adds minutes to the o

  • Problem: cannot implement TABLE_PER_CLASS inheritance

    Hello, I'm using Jdev 11g, JPA (with EclipseLink 1.1.1) & EJB 3.0. I'm trying to build TABLE_PER_CLASS inheritance. Here is entities' code: Parent:@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @NamedQueries( { @NamedQuery(name = "D

  • How to return or rollback the transport object in PRD?

    Hi Experts, I transported a revised program to PRD. But I decided to return it to my previous code. How would I do that in PRD? I'm not a real basis. PLease guide me. Points for the helpful ideas. Thanks!

  • Another temp question on MBPr 15...211F peak operating temp?

    Hello, I know this is a somewhat redundant question, but my own situation is slightly different from other situations. I purchased a MBPr a few days ago (11,3 with the 2.5 i7 quad.) This is my fourth MBP. All have ran hot as I realize the aluminum ca