File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can we refresh the directory from middle of java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we refresh the directory from middle of java code" Watch "Can we refresh the directory from middle of java code" New topic
Author

Can we refresh the directory from middle of java code

s lekkala
Greenhorn

Joined: Jul 25, 2011
Posts: 14
Hi,

can we refresh the directory in middle of executing the java code?
If excel sheet is not exists the below code creates and need to read same doc and modifies the data. My problem is i am unable to read the doc immediatly which was created at the instant(means fresh doc). So is there any alternate to read the existing doc.




Thanks,
Sreenu.

[Edit - added code tags, fixed formatting - MB]
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Why do you create a new file anyway? Why can't you just work with the data in the program, until you want to save it at a later stage?
s lekkala
Greenhorn

Joined: Jul 25, 2011
Posts: 14
Actually i am using this code for selenium, here i have to save the result of each selences code line. So every time i have to read the existed excel sheet and have to modify the sheet. Every day we have to create the new file and have to maintain the same file for daily status.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Yes, but you're mixing the loading and saving operations.

Instead, you should create a blank "workbook" or whatever you want to call it, and if a file already exists, populate it with data from the file. If the file doesn't exist, tough break.
You can then modify the workbook as you please, and when you want to save the data to a file, you can do that at any point you wish, even if that is after every modification. The point is that you should not create a new file just because it doesn't already exist.

Your workbook shouldn't care about files. It should only care about streams of data. So provide load and save methods that take InputStream and OutputStream respectively, or let the class implement Externalizable, or something similar.
s lekkala
Greenhorn

Joined: Jul 25, 2011
Posts: 14
Thanks for your reply Stephen, but i got to know the problem by using jxl.jar so i have switched to POI. Here i am getting the below Exception so can you please solve my problem if it is known:
java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes
at org.apache.poi.poifs.storage.HeaderBlockReader.alertShortRead(HeaderBlockReader.java:149)
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:85)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:151)
at POIExcelReader.displayFromExcel(POIExcelReader.java:48)
at POIExcelReader.main(POIExcelReader.java:72)

I have used below code:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
*
* @author Sreenu
*/
public class POIExcelReader {

/** Creates a new instance of POIExcelReader */
public POIExcelReader() {
}

/**
* This method is used to display the Excel content to command line.
*
* @param xlsPath
*/
public void displayFromExcel(String xlsPath) {
InputStream inputStream = null;

try {
inputStream = new FileInputStream(xlsPath);
} catch (FileNotFoundException e) {
System.out.println("File not found in the specified path.");
e.printStackTrace();
}

POIFSFileSystem fileSystem = null;

try {
fileSystem = new POIFSFileSystem(inputStream);

HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
HSSFRow newrow = sheet.createRow(5);
System.out.println("Row created");
HSSFCell cell = newrow.createCell(5);
System.out.println("Cell created");
cell.setCellValue("Hi this is the first trail in POI");
System.out.println("Writes data into excel");
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* The main executable method to test displayFromExcel method.
*
* @param args
*/
public static void main(String[] args) {
POIExcelReader poiExample = new POIExcelReader();
String xlsPath = "C:/Eclipse_workspace/Project/Result.xls";

poiExample.displayFromExcel(xlsPath);
}
}


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
No longer a “beginning” question. Moving discussion.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Chances are you're trying to read from an empty file. Did you look at the file you were reading from to confirm it was a valid Excel file?

And in future could you please you the code tags to make your code readable? Matthew Brown fixed your first post for you; have a look at it.
s lekkala
Greenhorn

Joined: Jul 25, 2011
Posts: 14
Ya Clampham, its valid file but i am unable to read it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

But you can read it from Excel itself, right? If not, then the file is not valid at all.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we refresh the directory from middle of java code