取上下文的小片断代码
What I really want to do is to delete one line from a file. My idea is to read all data before the line I want to delete and save it into a buffer. Then I read all data after the line I want to delete until the end of the file. Once I have saved all data, I try to delete the file but it fails. I want to delete the file just to create another one with the same filename and write the data I have saved into it. But everytime I try to delete the file it fails, and I don't know what to do. Could you help me? Thanks.
Here is part of the code I use to do all this:
Here is part of the code I use to do all this:
class GestorBotonSI implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String linea = "";
String usuarioleido = "";
String rutaleida = "";
String usuarioaeliminar =
(ConfiguracionCliente.getUsuarioSeleccionado()).toLowerCase(); //this works ok
long tamanio = 0;
long bytesaborrar = 0;
long desdebyte = 0;
long hastabyte = 0;
//byte buf[]; //defined as global
//byte buf2[]; //defined as global
try {
File fRand = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf = new RandomAccessFile(fRand, "rw");
tamanio = raf.length();
do {
// Read a line
desdebyte = raf.getFilePointer();
linea = raf.readLine();
hastabyte = raf.getFilePointer();
StringTokenizer st = new StringTokenizer(linea, ",");
usuarioleido = st.nextToken();
if (usuarioaeliminar.compareTo(usuarioleido.toLowerCase()) == 0) {
rutaleida = st.nextToken(); //Line to delete found, take data to do other things
bytesaborrar = hastabyte - desdebyte; //bytes to delete
}
}
while (usuarioaeliminar.compareTo(usuarioleido.toLowerCase()) != 0);
//If it was the last line in the file, write to only one buffer
linea = raf.readLine();
raf.close();
}
catch (Exception except11) {
System.out.println("Error al eliminar usuario");
except11.printStackTrace();
}
//Checking last line
if (linea == null) {
try {
File fRand2 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf2 = new RandomAccessFile(fRand2, "rw");
buf = new byte[ (int) (tamanio - bytesaborrar - hastabyte)];
raf2.readFully(buf, 0,
(int) desdebyte);
raf2.close();
}
catch (Exception except12) {
System.out.println("Error al eliminar usuario");
except12.printStackTrace();
}
}
else { //Write to two buffers
try {
File fRand3 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf3 = new RandomAccessFile(fRand3, "rw");
int lee = (int) desdebyte;
buf = new byte[ (int) desdebyte];
raf3.seek(0);
raf3.readFully(buf, 0, lee);
buf2 = new byte[ (int) (tamanio - hastabyte)];
int bytesaleer = (int) (tamanio - hastabyte);
raf3.seek(hastabyte);
raf3.readFully(buf2, 0, bytesaleer);
raf3.close();
}
catch (Exception except13) {
System.out.println("Error al eliminar usuario");
except13.printStackTrace();
}
}
//Deletes file (That's what I want, but it doesn't work)
try {
File Borrar = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
boolean borrado = Borrar.delete();
if (borrado == false)
System.out.println("No se pudo borrar el archivo");
else
System.out.println("Archivo borrado");
}
catch (Exception except14) {
System.out.println("Error al borrar archivo");
except14.printStackTrace();
}
//Create new empty file
try {
File ArchivoCfg2 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
boolean creadonuevo = ArchivoCfg2.createNewFile();
if(creadonuevo == false) System.out.println("No se pudo crear el archivo nuevo");
}
catch (Exception except15) {
System.out.println("Error al crear archivo nuevo");
except15.printStackTrace();
}
//Re-write data into file
try {
File fRand6 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf6 = new RandomAccessFile(fRand6, "rw");
raf6.write(buf);
if (linea != null)
raf6.write(buf2);
raf6.close();
}
catch (Exception except16) {
System.out.println("Error al escribir datos");
except16.printStackTrace();
}
}
}
{
public void actionPerformed(ActionEvent e)
{
String linea = "";
String usuarioleido = "";
String rutaleida = "";
String usuarioaeliminar =
(ConfiguracionCliente.getUsuarioSeleccionado()).toLowerCase(); //this works ok
long tamanio = 0;
long bytesaborrar = 0;
long desdebyte = 0;
long hastabyte = 0;
//byte buf[]; //defined as global
//byte buf2[]; //defined as global
try {
File fRand = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf = new RandomAccessFile(fRand, "rw");
tamanio = raf.length();
do {
// Read a line
desdebyte = raf.getFilePointer();
linea = raf.readLine();
hastabyte = raf.getFilePointer();
StringTokenizer st = new StringTokenizer(linea, ",");
usuarioleido = st.nextToken();
if (usuarioaeliminar.compareTo(usuarioleido.toLowerCase()) == 0) {
rutaleida = st.nextToken(); //Line to delete found, take data to do other things
bytesaborrar = hastabyte - desdebyte; //bytes to delete
}
}
while (usuarioaeliminar.compareTo(usuarioleido.toLowerCase()) != 0);
//If it was the last line in the file, write to only one buffer
linea = raf.readLine();
raf.close();
}
catch (Exception except11) {
System.out.println("Error al eliminar usuario");
except11.printStackTrace();
}
//Checking last line
if (linea == null) {
try {
File fRand2 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf2 = new RandomAccessFile(fRand2, "rw");
buf = new byte[ (int) (tamanio - bytesaborrar - hastabyte)];
raf2.readFully(buf, 0,
(int) desdebyte);
raf2.close();
}
catch (Exception except12) {
System.out.println("Error al eliminar usuario");
except12.printStackTrace();
}
}
else { //Write to two buffers
try {
File fRand3 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf3 = new RandomAccessFile(fRand3, "rw");
int lee = (int) desdebyte;
buf = new byte[ (int) desdebyte];
raf3.seek(0);
raf3.readFully(buf, 0, lee);
buf2 = new byte[ (int) (tamanio - hastabyte)];
int bytesaleer = (int) (tamanio - hastabyte);
raf3.seek(hastabyte);
raf3.readFully(buf2, 0, bytesaleer);
raf3.close();
}
catch (Exception except13) {
System.out.println("Error al eliminar usuario");
except13.printStackTrace();
}
}
//Deletes file (That's what I want, but it doesn't work)
try {
File Borrar = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
boolean borrado = Borrar.delete();
if (borrado == false)
System.out.println("No se pudo borrar el archivo");
else
System.out.println("Archivo borrado");
}
catch (Exception except14) {
System.out.println("Error al borrar archivo");
except14.printStackTrace();
}
//Create new empty file
try {
File ArchivoCfg2 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
boolean creadonuevo = ArchivoCfg2.createNewFile();
if(creadonuevo == false) System.out.println("No se pudo crear el archivo nuevo");
}
catch (Exception except15) {
System.out.println("Error al crear archivo nuevo");
except15.printStackTrace();
}
//Re-write data into file
try {
File fRand6 = new File(System.getProperty("user.dir") +
System.getProperty("file.separator") +
System.getProperty("file.separator") +
"cliente.cfg");
RandomAccessFile raf6 = new RandomAccessFile(fRand6, "rw");
raf6.write(buf);
if (linea != null)
raf6.write(buf2);
raf6.close();
}
catch (Exception except16) {
System.out.println("Error al escribir datos");
except16.printStackTrace();
}
}
}