Strange problem at write file

D

Derek

Hello:

I want copy files from a directory to other (both in the same machine). This
copy process is successful: first it read the source file and then write the
data to the new file.

The problem is the following. One of the files that I copy is .php
("site.ini.append.php") and this is used for other application that work
with this php file. But, the application fault when use this file. This file
is about configuration.

But, If I edit this file (site.ini.append.php) with an editor, i.e. Windows
notebook, and I click on "File->save", the application, that use this file,
work fine now.

What is the problem?. I don´t have idea.

The code to read and write the file is the following:

/**** Read file *****/
public static byte[] LeerFichero(String file) {
byte[] datos = null;
try {

BufferedInputStream entrada = new BufferedInputStream(new
FileInputStream(file));

datos = new byte[entrada.available()];
entrada.read(datos);

entrada.close();

}
catch(java.io.FileNotFoundException fnfirma) {
System.out.println("Archivo no encontrado: " + fnfirma);}
catch(java.io.IOException ioex) {}

return datos;
}


/***** Write file ******/
public static void EscribirFichero(String file, byte[] datos) {

FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {e.printStackTrace();}

try {
outStream.write(datos);
} catch (IOException e1) {e1.printStackTrace();}

try {
outStream.close();
} catch (IOException e2) {e2.printStackTrace();}

}


Thanks.
 
F

Ferenc Hechler

perhaps there are problems with your user-rights?
Is the user running the application that works with the .php file the same
user your java-application is running under?
 
K

Knute Johnson

Derek said:
Hello:

I want copy files from a directory to other (both in the same machine). This
copy process is successful: first it read the source file and then write the
data to the new file.

The problem is the following. One of the files that I copy is .php
("site.ini.append.php") and this is used for other application that work
with this php file. But, the application fault when use this file. This file
is about configuration.

But, If I edit this file (site.ini.append.php) with an editor, i.e. Windows
notebook, and I click on "File->save", the application, that use this file,
work fine now.

What is the problem?. I don´t have idea.

The code to read and write the file is the following:

/**** Read file *****/
public static byte[] LeerFichero(String file) {
byte[] datos = null;
try {

BufferedInputStream entrada = new BufferedInputStream(new
FileInputStream(file));

datos = new byte[entrada.available()];
entrada.read(datos);

entrada.close();

}
catch(java.io.FileNotFoundException fnfirma) {
System.out.println("Archivo no encontrado: " + fnfirma);}
catch(java.io.IOException ioex) {}

return datos;
}


/***** Write file ******/
public static void EscribirFichero(String file, byte[] datos) {

FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {e.printStackTrace();}

try {
outStream.write(datos);
} catch (IOException e1) {e1.printStackTrace();}

try {
outStream.close();
} catch (IOException e2) {e2.printStackTrace();}

}


Thanks.

Since you don't post all of your code I can't tell for sure but my best
guess is that you do some string or character conversion when you read
or write the file. The other possibility is that by using the
available() you are not reading the entire file.
 
D

Derek

Both applications are running in Windows and with the same user.


Ferenc Hechler said:
perhaps there are problems with your user-rights?
Is the user running the application that works with the .php file the same
user your java-application is running under?

Hello:

I want copy files from a directory to other (both in the same machine).
This
copy process is successful: first it read the source file and then write
the
data to the new file.

The problem is the following. One of the files that I copy is .php
("site.ini.append.php") and this is used for other application that work
with this php file. But, the application fault when use this file. This
file
is about configuration.

But, If I edit this file (site.ini.append.php) with an editor, i.e.
Windows
notebook, and I click on "File->save", the application, that use this
file,
work fine now.

What is the problem?. I don´t have idea.

The code to read and write the file is the following:

/**** Read file *****/
public static byte[] LeerFichero(String file) {
byte[] datos = null;
try {

BufferedInputStream entrada = new BufferedInputStream(new
FileInputStream(file));

datos = new byte[entrada.available()];
entrada.read(datos);

entrada.close();

}
catch(java.io.FileNotFoundException fnfirma) {
System.out.println("Archivo no encontrado: " + fnfirma);}
catch(java.io.IOException ioex) {}

return datos;
}


/***** Write file ******/
public static void EscribirFichero(String file, byte[] datos) {

FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {e.printStackTrace();}

try {
outStream.write(datos);
} catch (IOException e1) {e1.printStackTrace();}

try {
outStream.close();
} catch (IOException e2) {e2.printStackTrace();}

}


Thanks.
 
D

Derek

I use too this function with the same result:

private static byte[] buff;
public synchronized static byte[] getFileBytes( File file ) throws
IOException
{
if (file != null) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int available = bis.available();
//int chunkSize = CHUNK_SIZE;
int chunkSize = 2048;
if (available <= 0) {
available = chunkSize;
}

if ( buff == null ){
buff = new byte[ chunkSize ];
}
System.out.println( "GetFileBytes: chunkSize " + chunkSize +
"available " + available );
ByteArrayOutputStream baos = new
ByteArrayOutputStream(available );
synchronized ( buff ){
int readCount;
while ((readCount = bis.read( buff )) != -1) {
baos.write(buff, 0, readCount);
}
}
return baos.toByteArray();
} else {
throw new IllegalArgumentException(
"Parameter 'file' cannot be null");
}
}//getFileBytes



Knute Johnson said:
Derek said:
Hello:

I want copy files from a directory to other (both in the same machine). This
copy process is successful: first it read the source file and then write the
data to the new file.

The problem is the following. One of the files that I copy is .php
("site.ini.append.php") and this is used for other application that work
with this php file. But, the application fault when use this file. This file
is about configuration.

But, If I edit this file (site.ini.append.php) with an editor, i.e. Windows
notebook, and I click on "File->save", the application, that use this file,
work fine now.

What is the problem?. I don´t have idea.

The code to read and write the file is the following:

/**** Read file *****/
public static byte[] LeerFichero(String file) {
byte[] datos = null;
try {

BufferedInputStream entrada = new BufferedInputStream(new
FileInputStream(file));

datos = new byte[entrada.available()];
entrada.read(datos);

entrada.close();

}
catch(java.io.FileNotFoundException fnfirma) {
System.out.println("Archivo no encontrado: " + fnfirma);}
catch(java.io.IOException ioex) {}

return datos;
}


/***** Write file ******/
public static void EscribirFichero(String file, byte[] datos) {

FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {e.printStackTrace();}

try {
outStream.write(datos);
} catch (IOException e1) {e1.printStackTrace();}

try {
outStream.close();
} catch (IOException e2) {e2.printStackTrace();}

}


Thanks.

Since you don't post all of your code I can't tell for sure but my best
guess is that you do some string or character conversion when you read
or write the file. The other possibility is that by using the
available() you are not reading the entire file.
 
K

Knute Johnson

Derek said:
I use too this function with the same result:

private static byte[] buff;
public synchronized static byte[] getFileBytes( File file ) throws
IOException
{
if (file != null) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int available = bis.available();
//int chunkSize = CHUNK_SIZE;
int chunkSize = 2048;
if (available <= 0) {
available = chunkSize;
}

if ( buff == null ){
buff = new byte[ chunkSize ];
}
System.out.println( "GetFileBytes: chunkSize " + chunkSize +
"available " + available );
ByteArrayOutputStream baos = new
ByteArrayOutputStream(available );
synchronized ( buff ){
int readCount;
while ((readCount = bis.read( buff )) != -1) {
baos.write(buff, 0, readCount);
}
}
return baos.toByteArray();
} else {
throw new IllegalArgumentException(
"Parameter 'file' cannot be null");
}
}//getFileBytes

Derek:

I don't see anything obvious there either. You don't need the check for
available. Set the size of your BufferedInputStream and use an array
that size to read the data into.

I assume that the file must be pretty small or you wouldn't be
collecting it as a byte[]. If that is the case you could just create
one array that is the same size as the file, read the file data into it
and then return it. Save the multiple buffers and ByteArrayOutputStreams.

As to your real problem, I don't see it here. You could try posting all
of your code.
 
J

John C. Bollinger

Derek said:
Hello:

I want copy files from a directory to other (both in the same machine). This
copy process is successful: first it read the source file and then write the
data to the new file.

The problem is the following. One of the files that I copy is .php
("site.ini.append.php") and this is used for other application that work
with this php file. But, the application fault when use this file. This file
is about configuration.

But, If I edit this file (site.ini.append.php) with an editor, i.e. Windows
notebook, and I click on "File->save", the application, that use this file,
work fine now.

Have you checked to see what the difference is between the results you
obtain by these two methods? I have a pretty good guess, but if you
know then the information would be very relevant to troubleshooting the
problem. If you _don't_ know then the information is _still_ very relevant.
What is the problem?. I don´t have idea.

Your code is wrong.
The code to read and write the file is the following:

/**** Read file *****/
public static byte[] LeerFichero(String file) {
byte[] datos = null;
try {

BufferedInputStream entrada = new BufferedInputStream(new
FileInputStream(file));

datos = new byte[entrada.available()];
entrada.read(datos);

entrada.close();

}
catch(java.io.FileNotFoundException fnfirma) {
System.out.println("Archivo no encontrado: " + fnfirma);}
catch(java.io.IOException ioex) {}

return datos;
}

Your LeerFichero method is both broken and ill-designed. First the
broken part:

(1) You cannot rely on InputStream.available to tell you the length of
the file. It tells you only how many bytes can be read without
blocking, as of the time of the method invocation. That number may be
less than the size of the underlying file, and may even be zero.

(2) InputStream.read(byte[]) may not read enough bytes to fill the
supplied array; the return value tells you how many it actually read.
This may not be a problem right now in your case because you size the
array based on the available bytes, but you are going to need to fix it
eventually.

Now the ill-designedness:

(1) It is a bad idea in the first place to read a file of unknown length
into a byte[], especially if all you want to do with the data is to
write it back out. It can work fine for small files, but you can easily
run into files too large to hold completely in your VM's available memory.

(2) Your exception handling is inappropriate. You completely suppress
all IOExceptions except FileNotFoundException, and for
FileNotFoundException you merely print an error message. You do not, in
any of these exceptional cases, actually prevent the method from
returning a useless value. Throwing an exception from the method would
be much better form than signaling failure by returning null.
/***** Write file ******/
public static void EscribirFichero(String file, byte[] datos) {

FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {e.printStackTrace();}

try {
outStream.write(datos);
} catch (IOException e1) {e1.printStackTrace();}

try {
outStream.close();
} catch (IOException e2) {e2.printStackTrace();}

}

This method is better, though it also has exception handling problems.
The whole method body should be inside one try block, or the method
should just declare that it throws IOException. Consider in particular
what happens if you cannot create the output stream: you nevertheless
proceed to try to write the data!

If copying from file to file is the sole point, then put it all in one
method. Create the input stream and output stream in that method, and
create a moderate-size (1K - 4K) byte array for a buffer. Then loop, at
each iteration reading from the input into the buffer and writing the
bytes read (only) from the buffer to the output, until the read signals
end of file (by returning -1).
 
D

Derek

Hello:

I have used other method to copy files, with the same result. The code is
the following, and I think that it follow your advices:

public static void CopiaYPega(String fileOrigen, String fileDestino)

{

try {

File inputFile = new File(fileOrigen);

File outputFile = new File(fileDestino);

FileInputStream fis = new FileInputStream(inputFile);

FileOutputStream fos = new FileOutputStream(outputFile);

int c;

while ((c = fis.read()) != -1) {

fos.write(c);

}

fis.close();

fos.close();

} catch (FileNotFoundException e) {

System.err.println("FileStreamsTest: " + e);

} catch (IOException e) {

System.err.println("FileStreamsTest: " + e);

}


}


What is wrong now?. Thanks a lot.

Can you send the method that you suggest me to copy files?. Thanks.


John C. Bollinger said:
Derek said:
Hello:

I want copy files from a directory to other (both in the same machine). This
copy process is successful: first it read the source file and then write the
data to the new file.

The problem is the following. One of the files that I copy is .php
("site.ini.append.php") and this is used for other application that work
with this php file. But, the application fault when use this file. This file
is about configuration.

But, If I edit this file (site.ini.append.php) with an editor, i.e. Windows
notebook, and I click on "File->save", the application, that use this file,
work fine now.

Have you checked to see what the difference is between the results you
obtain by these two methods? I have a pretty good guess, but if you
know then the information would be very relevant to troubleshooting the
problem. If you _don't_ know then the information is _still_ very relevant.
What is the problem?. I don´t have idea.

Your code is wrong.
The code to read and write the file is the following:

/**** Read file *****/
public static byte[] LeerFichero(String file) {
byte[] datos = null;
try {

BufferedInputStream entrada = new BufferedInputStream(new
FileInputStream(file));

datos = new byte[entrada.available()];
entrada.read(datos);

entrada.close();

}
catch(java.io.FileNotFoundException fnfirma) {
System.out.println("Archivo no encontrado: " + fnfirma);}
catch(java.io.IOException ioex) {}

return datos;
}

Your LeerFichero method is both broken and ill-designed. First the
broken part:

(1) You cannot rely on InputStream.available to tell you the length of
the file. It tells you only how many bytes can be read without
blocking, as of the time of the method invocation. That number may be
less than the size of the underlying file, and may even be zero.

(2) InputStream.read(byte[]) may not read enough bytes to fill the
supplied array; the return value tells you how many it actually read.
This may not be a problem right now in your case because you size the
array based on the available bytes, but you are going to need to fix it
eventually.

Now the ill-designedness:

(1) It is a bad idea in the first place to read a file of unknown length
into a byte[], especially if all you want to do with the data is to
write it back out. It can work fine for small files, but you can easily
run into files too large to hold completely in your VM's available memory.

(2) Your exception handling is inappropriate. You completely suppress
all IOExceptions except FileNotFoundException, and for
FileNotFoundException you merely print an error message. You do not, in
any of these exceptional cases, actually prevent the method from
returning a useless value. Throwing an exception from the method would
be much better form than signaling failure by returning null.
/***** Write file ******/
public static void EscribirFichero(String file, byte[] datos) {

FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {e.printStackTrace();}

try {
outStream.write(datos);
} catch (IOException e1) {e1.printStackTrace();}

try {
outStream.close();
} catch (IOException e2) {e2.printStackTrace();}

}

This method is better, though it also has exception handling problems.
The whole method body should be inside one try block, or the method
should just declare that it throws IOException. Consider in particular
what happens if you cannot create the output stream: you nevertheless
proceed to try to write the data!

If copying from file to file is the sole point, then put it all in one
method. Create the input stream and output stream in that method, and
create a moderate-size (1K - 4K) byte array for a buffer. Then loop, at
each iteration reading from the input into the buffer and writing the
bytes read (only) from the buffer to the output, until the read signals
end of file (by returning -1).
 
J

John C. Bollinger

Derek said:
Hello:

I have used other method to copy files, with the same result. The code is
the following, and I think that it follow your advices:

It meets my general specifications, yes. It would be rather more
efficient if you read and wrote data in chunks bigger than one byte, but
that's a performance issue, not a correctness issue.
public static void CopiaYPega(String fileOrigen, String fileDestino)

{

try {

File inputFile = new File(fileOrigen);

File outputFile = new File(fileDestino);

FileInputStream fis = new FileInputStream(inputFile);

FileOutputStream fos = new FileOutputStream(outputFile);

int c;

while ((c = fis.read()) != -1) {

fos.write(c);

}

fis.close();

fos.close();

} catch (FileNotFoundException e) {

System.err.println("FileStreamsTest: " + e);

} catch (IOException e) {

System.err.println("FileStreamsTest: " + e);

}


}


What is wrong now?. Thanks a lot.

Make certain that you are actually *using* this revised code, and not
some copy of the old code that happens to be lying around. For testing
purposes you can accomplish this by making the method print a message at
beginning and end. Also make sure that you are using it to copy the
file to the correct place, possibly by confirming the target file's
modification timestamp. Make sure that the program runs without
producing any error message. Make sure that the server does not try to
read the copied file before the program is done writing it.

If all those things check out but you still have problems, then look at
the differences between the original file and the copy. If the contents
are identical then the copy is successful and your problem is elsewhere
-- probably a file ownership or access permission problem.
Can you send the method that you suggest me to copy files?. Thanks.

It would look similar to what you already have, but I would be using the
read(byte[]) and write(byte[], int, int) methods, respectively, to read
and write the data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top