well this is a strange one.
The first save attampt usually works (1 more try max).
but in a heavy load (many saves in a row) the saved file disappears.
if uncommenting the "Thread.sleep" the error is captured otherwise the validation passes succesfully
public void save(Object key, T objToSave) throws FileNotFoundException, IOException {
IOException ex = null;
for (int i = 0; i < NUM_OF_RETRIES; i++) {
try {
/* saving */
String filePath = getFilePath(key);
OutputStream outStream = getOutStream(filePath);
ObjectOutputStream os = new ObjectOutputStream(outStream);
os.writeObject(objToSave);
os.close();
/* validations warnings etc. */
if (i>0){
logger.warn(objToSave + " saved on attamped " + i);
/* sleep more on each fail */
Thread.sleep(100+i*8);
}
//Thread.sleep(50);
File doneFile = new File(filePath);
if (! (doneFile.exists())){
logger.error("got here but file was not witten to disk ! id was" + key);
throw new IOException();
}
logger.info("6. start persist " + key + " path=" + new File(filePath).getAbsolutePath() + " exists="+doneFile.exists());
return;
} catch (IOException e) {
logger.error(objToSave + " failed on attamped " + i);
ex = e;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
throw ex;
}
It is not a java writers issue.
I was not using threads explicitly but in my test I was deleting the folder i was saving to using: Runtime.getRuntime("rm -rf saver_directory");
I found out the hard way that it is asynchronous and the exact delete and create time was changing in mili-seconds.
so the solution was adding "sleep" after the delete.
the correct answer would be using java for the delete and not making a shortcuts ;)
Thank you all.
Related
I need to use both executable files of Adobe reader to open documents.
acrord32.exe for the users using adobe acrobat reader and acrobat.exe for the users using adobe acrobat pro.
I have used system parameters to get done the job.
But my problem is that whenever I use the acrobat.exe version this error message is coming.
After clicking ok then my pdf file opens from acrobat.exe.
So I just want to delay/overwrite/remove this message by using java...
Here is my code
public static void openPDF(Container container, String fileName) throws Exception, PrintException {
SystemParameterHome paramHome = (SystemParameterHome) container.getHomeForEntityBean();
try {
String pdfParameter = "PDF_COMMAND";
SystemParameterObject pdfCommand = paramHome.findByPrimaryKey(pdfParameter);
String command = pdfCommand.getStringValue();
Process proc = Runtime.getRuntime().exec(command + " \"" + fileName + "\"");
int exitVal = proc.waitFor();
if(exitVal != 0) {
System.out.println("exit value in if: "+ exitVal);
pdfParameter = "PDF_COMMAND_64";
pdfCommand = paramHome.findByPrimaryKey(pdfParameter);
command = pdfCommand.getStringValue();
Runtime.getRuntime().exec(command + " \"" + fileName + "\"");
throw new PrintException("There was an error in printing. Please contact a system administrator.");
}else {
System.out.println("exit value in else "+ exitVal);
}
}
catch (Exception e) {
e.printStackTrace();
}
I use zip4j to unzip zip packages which are safed by a password (AES-256). My problem is not, that the code is not working, rather that it doen't throw any error when the password doesn't match with the actual on the .zip.
the .zip has a password with 123 and for zip4j I set the password 123456789. So he is not able to extract all. I expect an Error or any message, that it could not extract. The actual case is, he doesn't extract it but no exception or error message, nothing.
Any Idea how I can check if the extraction was successful?
protected void unpackZip(String destinationPath, String archivePath) throws InterruptedException {
int onChange = 0;
try {
ZipFile zipFile = new ZipFile( archivePath );
zipFile.setRunInThread( true );
if (zipFile.isEncrypted()) {
zipFile.setPassword( "123456789");
}
zipFile.extractAll( destinationPath );
// http://www.lingala.net/zip4j/forum/index.php?topic=68.0
ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
while (progressMonitor.getState() == ProgressMonitor.STATE_BUSY) {
// To get the percentage done
if (onChange != progressMonitor.getPercentDone()) {
onChange = progressMonitor.getPercentDone();
sendWebStatusUiMessage( "Extracted : " + onChange + "% ", "update" );
}
try {
Thread.sleep( 1000 );
} catch (Exception e) {
}
}
} catch (ZipException e) {
e.printStackTrace();
}
}
This answer is a bit late, but just in case it might be useful for anyone:
When running zip4j in thread mode, you have to check for the status of the task execution and exception (if errored), after the while loop.
while (progressMonitor.getState() == ProgressMonitor.STATE_BUSY) {
// To get the percentage done
if (onChange != progressMonitor.getPercentDone()) {
onChange = progressMonitor.getPercentDone();
sendWebStatusUiMessage( "Extracted : " + onChange + "% ", "update" );
}
try {
Thread.sleep( 1000 );
} catch (Exception e) {
}
}
if (progressMonitor.getResult().equals(ProgressMonitor.Result.SUCCESS)) {
System.out.println("Successfully added folder to zip");
} else if (progressMonitor.getResult().equals(ProgressMonitor.Result.ERROR)) {
System.out.println("Error occurred. Error message: " + progressMonitor.getException().getMessage());
} else if (progressMonitor.getResult().equals(ProgressMonitor.Result.CANCELLED)) {
System.out.println("Task cancelled");
}
I use netty offical example HttpStaticFileServerHandler as file server, but when I download file from server I met a problem, the mp4 file I download from server is not complete and can't display.
https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java
And here is my client code:
FileOutputStream fos = null;
try {
URL website = new URL("http://localhost:8090/export/App/***.mp4");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
fos = new FileOutputStream("/Users/mine/***.mp4");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (Exception e) {
System.out.println("error msg:\n" + e);
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException ioe) {
System.out.println("fos close fail:\n" + ioe);
}
}
Make sure you close your FileOutputStream using fos.close().
Failing to do so means that only part of the data will be written, and that other programs may experience problems when accessing the file.
Another thing you could check is viewing the filesize of the file, those should match at both sides, if the file is way too small, open it with an text editor, and view the contents to check for clues.
I solved this problem. I find the RandomAccessFile is not closed in correct time.
Here is the change:
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (FileNotFoundException ignore) {
sendError(ctx, NOT_FOUND);
return;
}
...
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
#Override
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
if (total < 0) { // total unknown
System.err.println(future.channel() + " Transfer progress: " + progress);
} else {
System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
}
}
#Override
public void operationComplete(ChannelProgressiveFuture future) {
System.err.println(future.channel() + " Transfer complete.");
raf.close();// close raf when transfer completed
}
});
This question already has an answer here:
Writing to a file but only last line saved
(1 answer)
Closed 9 years ago.
I'm using a while(matcher.find()) loop to write certain substrings to a file. I get a list of the matched strings occurring in the file in my System.out console just fine, but when I try to use FileWriter to write to a text file, I only get the very last string in the loop written. I've scoured stackoverflow for similar problems (and it lives up to its name), I couldn't find anything that helped me. And just to clarify this isn't being run on the EDT. Can anyone explain where to look for the problem?
try {
String writeThis = inputId1 + count + inputId2 + link + inputId3;
newerFile = new FileWriter(writePath);
//this is only writing the last line from the while(matched.find()) loop
newerFile.write(writeThis);
newerFile.close();
//it prints to console just fine! Why won't it print to a file?
System.out.println(count + " " + show + " " + link);
} catch (IOException e) {
Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);
} finally {
try {
newerFile.close();
} catch (IOException e) {
Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);
}
}
}
Quick fix:
change
newerFile = new FileWriter(writePath);
to
newerFile = new FileWriter(writePath, true);
This uses the FileWriter(String fileName, boolean append) constructor.
Better fix:
create the FileWriter outside of the while(matcher.find()) loop and close it afterwards (or use it as a try with resources initilzation).
the code would be something like:
try (FileWriter newerFile = new FileWriter(writePath)) {
while (matcher.find()) {
newerFile.write(matcher.group());
}
} ...
You should not create an instance of FileWriter every loop iteration. You need to leave the usage of method write() there and init FileWriter before loop and close it after loop.
Please check as follows:
FileWriter newerFile = new FileWriter(writePath);
while(matcher.find())
{
xxxxx
try {
String writeThis = inputId1 + count + inputId2 + link + inputId3;
//this is only writing the last line from the while(matched.find()) loop
newerFile.write(writeThis);
newerFile.flush();
//it prints to console just fine! Why won't it print to a file?
System.out.println(count + " " + show + " " + link);
} catch (IOException e) {
Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);
} finally {
try {
newerFile.close();
} catch (IOException e) {
Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);
}
}
}
}
I am trying to read from a file to which some user credentials were written. I want to write the file to an internal storage location. The code:
private void writeSendDetails(String name, String number, String emailID) {
//This function writes details to userCredentials.txt and also sends it to server.
String text = "Name: " + userName + "\n" + "Number: " + userNumber + "\n" + "Email ID:" + userEmailID;
FileOutputStream fos = null;
try {
fos = openFileOutput(userCredFile, MODE_PRIVATE);
Log.v(this.toString(), fos.toString());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(fos != null) {
OutputStreamWriter osw = new OutputStreamWriter(fos);
try {
osw.write(text);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v(this.toString(), "IOException caught in osw.write");
e.printStackTrace();
}
try {
osw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.v(this.toString(), "Written everything to userCredentials.txt");
readUserCredentials();
//code to send to server should begin here.
}
private void readUserCredentials() {
//function to read name, number and email ID from userCredentials.txt
/* Steps:
* 1. Check if file exists.
* 2. If it does, read all relevant credentials.
*/
File f = new File(userCredFile);
if(f.canRead()) {
Log.v(this.toString(), "Can open userCredentials for reading from.");
}
try {
FileReader fis = new FileReader(f);
Log.v(this.toString(), "Wrapping a buffered reader around file reader.");
BufferedReader bufRead = new BufferedReader(fis, 100);
String line;
try {
while((line = bufRead.readLine()) != null) {
Log.v(this.toString(), "Line read = " + line);
line = bufRead.readLine();
if(line.indexOf("Name: ") != -1) {
Log.v(this.toString(), "Found name in the string.");
userName = line.substring(6);
} else if(line.indexOf("Number: ") != -1) {
Log.v(this.toString(), "Found number in the string.");
userNumber = line.substring(8);
} else if(line.indexOf("Email ID: ") != -1) {
Log.v(this.toString(), "Found email in the string.");
userEmailID = line.substring(10);
}
}
Log.v(this.toString(), "User credentials = " + userName + " " + userNumber + " " + userEmailID);
} catch (IOException e) {
Log.v(this.toString(), "IOException caught.");
}
} catch (FileNotFoundException e1) {
Log.v(this.toString(), "File not found for reading.");
}
}
The LogCat output shows:
04-14 15:20:43.789: V/com.sriram.htmldisplay.htmlDisplay#44c0c660(675): Written everything to userCredentials.txt
04-14 15:20:43.789: V/com.sriram.htmldisplay.htmlDisplay#44c0c660(675): File not found for reading.
04-14 15:20:43.889: V/com.sriram.htmldisplay.fireflymenu#44c401e0(675): File not found for reading.
My question(s):
1. I need to write the file to internal storage. Am I doing it correctly?
2. Why is the file just written not being read?
Some things for your code:
#Oren is correct, you should use Log.e(TAG, "message", e) instead of the auto-created stuff from eclipse!
you should simply merge the 3 try/catch to one. No need to make it 3 times...
you should use Log.e() as said above for your FileNotFoundException too, so see the stacktrace to check the real reason (which currently covers the hint to solve your issue)
If you would have done at least number 3, you would have seen that the file you try to read could not be found. Thats why your log doesn't show the Can open userCredentials for reading from. output from your if statement.
The reason for that is pretty simple: You create the file by using openFileOutput(userCredFile, MODE_PRIVATE);. If you read the documentation of openFileOutput you will stumble upon:
The name of the file to open; can not contain path separators.
That means that userCredFile can only be something like test.txt. Also this method creates a file in a directory that can't be easily access from "outside".
When you now try to read the file via FileReader(userCredFile) it should be obvious, that android will try to open it in the root directory: /text.txt and it will, of course, fail. No non-root app can write/read in the root directory.
The main question, and also the answer to your issue: Why don't you use the corresponding openFileInput(userCredFile) method to read the file?