Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete One Line in a File

 
Mike Cutter
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have text files that contains a list of the current employees. One line is one employee and the file has about 3000 lines. Using the Apache Common Net FTPClient, I am able to append to the file when a new employee is hired.

If an employee resigns, quits, or retires, I would need a way to delete this one line in the file. Is there an easy way of just purging this single line in an enormous file?

I was thinking about first reading the file to count the number of lines and load each line into a String array. Then, search the array for that particular employee number I want to purge. Create a new file and append each line of the array to the file if it is not this employee. This seems pretty cumbersome and kind of advanced for my Java expertise. To make it more complicated, I wanted to access this as a remote file on a Unix system, so that our operators can run this Java program from their PCs. That is why I used Apache Common Net FTPClient.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the problems, which was solved by inventing databases.

If a database isn't an alternative, I would think about setting a boolean flag to 'invalid' on deleting, and condense the file only few times on low system-load.
 
Mike Cutter
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That may not be a bad idea. I could start getting familiar with JDBC and possibly decide on an Oracle, mySQL, SQL Server, or Access database.

Could this also be resolved by having a Lightweight Directory Access Protocol (LDAP) and implementing the JNDI?

I just dread inputting 3000 lines of the file into 3000 records.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike

I just dread inputting 3000 lines of the file into 3000 records.


why so? With database tools you could import this file into a table. Alternatively, if it comes down to it, you could even write a simple java program to parse the file and carry out the SQL inserts in a loop using JDBC also. There would be no need to manually enter these 300 records
 
Mike Cutter
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to copy all 3000 lines into a String array. First, I delete the file and the array is populated. The only way I could think of doing this was searching for the particular line I want to delete through a for loop of each index of the array. If it's not that the line, I write that line to the file. The file is on a remote system from where the applet is running. To write all 3000 lines over the network to a new file, it took about 6 minutes. I think six minutes in not adequate when UNIX shell scripts could do it in a few seconds.

I know in UNIX shell scripting; I could do a sed or awk to delete a single line a huge file. Is there a way in Java to utilize the UNIX editing tools just to purge one line of a file?

String inputLine;
ftp.connect("server");
ftp.login("root", "password");
buffStream = new BufferedReader(new InputStreamReader(ftp.retrieveFileStream("/tmp/file")));
while (null != (inputLine = buffStream.readLine()))) {
lines++;
}
ftp.disconnect();
String data[] = new String[lines];
ftp.connect("server");
ftp.login("root", "password");
buffStream = new BufferedReader(new InputStreamReader(ftp.retrieveFileStream("/tmp/file")));
for (int i=0; i < lines; i++) {
inputLine = buffStream.readLine();
data[i] = inputLine;
}
ftp.disconnect();
ftp.connect("server");
ftp.login("root", "password");
ftp.deleteFile("/tmp/file");
String employee = empFld.getText();
for (int i=0; i < lines; i++) {
if (employee.compareTo(data[i].substring(1,8)) != 0) {
String dataLine = data[i] + "\n";
inStream = new ByteArrayInputStream(dataLine.getBytes("UTF-8"));
ftp.appendFile("/tmp/file", inStream);
}
}
ftp.disconnect();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!