• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can we refresh the directory from middle of java code

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Saloon Keeper
Posts: 15510
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}


 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No longer a “beginning” question. Moving discussion.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya Clampham, its valid file but i am unable to read it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you can read it from Excel itself, right? If not, then the file is not valid at all.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic