Cant to remove a single row of excel using poi - apache-poi

I am trying to delete a single row from the excel file but the can't do that,
see the code here
try{
String val = request.getParameter("rdel");
int va = 5;
System.out.println("int val"+va);
FileInputStream file = new FileInputStream(new File(fileName));
HSSFWorkbook wb = new HSSFWorkbook(file); // here exception occurs
HSSFSheet sheet = wb.getSheetAt(0);
int lastRowNum=sheet.getLastRowNum();
if(va>=0&&va<lastRowNum){
sheet.shiftRows(va+1,lastRowNum, -1);
}
if(va==lastRowNum){
HSSFRow removingRow=sheet.getRow(va);
if(removingRow!=null){
sheet.removeRow(removingRow);
}
}
FileOutputStream out = new FileOutputStream ("D:/task.xls");
wb.write(out);
}catch(Exception e){
e.printStackTrace();
}
return SUCCESS;
}
see the exception
org.apache.poi.hssf.record.RecordFormatException: Unable to construct record instance
at org.apache.poi.hssf.record.RecordFactory.createRecord(RecordFactory.java:186)
at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:328)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:271)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:196)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:312)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:293)
at com.struts.curd.Delete.execute(Delete.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

This issue was removed after 3.2... Update POI version with recent one (right now it is 3.9) ... hope you will not face this issue again...
For Details plz check here

Related

Extracting the list of CL that matches a certain condition Failing

I am trying to extract a list of CL in a depot that matches a certain description and i dont have a success. below is my code and the error i get
/* ALL MY IMPORTS */
/* MY PERFORCE AND HTTP modules using grapes */
/* SERVER LOGIN CONDITIONS */
List<IFileSpec> fileList = server.getDepotFiles(FileSpecBuilder.makeFileSpecList("//depot/release/prod/..."), false)
List<IChangelistSummary> changelistList = server.getChangelists(fileList,null)
def clList = [:]
def discl = []
if (changelistList != null) {
changelistList.each { IChangelistSummary cl ->
if (cl.getId() != null) {
println cl.getId()
IChangelist cld = server.getChangelist(cl.getId())
println cld.getDescription()
if(!(discl.contains(cl.getId())) && !(cl.getDescription().startsWith("Build with EF")) && !(cl.getUsername().matches("mb_ccatt")) ){
discl << cl.getId
clList.put(cl.getId(),cl.getUsername())
}
}
}
}
clList.each{ key,value ->
println "\t$key: $value"
}
server.disconnect()
Below is the log, as you see till certain extent all the print statments look good, and suddenly it throws a exception
com.perforce.p4java.impl.generic.core.User#55322aab
Which i dont understand where from it is getting User index?
192359
Prod Copy Up using p4 copy from Dev-XXX to ZZZZ-Release using newly created label Dev-XXX.CCversion-XXX.20180212_103004.ICM730.COPY_UP_depot_mode
194118
Merging from ZZZZ-Release PARENT CL 194248 Yadav __ Description: 1802_UAT_Defect1234_Change
com.perforce.p4java.impl.generic.core.User#55322aab
Caught: groovy.lang.MissingPropertyException: No such property: getId for
class: com.perforce.p4java.impl.generic.core.ChangelistSummary
groovy.lang.MissingPropertyException: No such property: getId for class:
com.perforce.p4java.impl.generic.core.ChangelistSummary
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:66)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
at cllist_new$_run_closure1.doCall(cllist_new.groovy:34)
at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2040)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2025)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2066)
at org.codehaus.groovy.runtime.dgm$163.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at cllist_new.run(cllist_new.groovy:27)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
I suspect that the problem here is that getId is a method, not a property, so you can't write:
discl << cl.getId
you have to write
discl << cl.getId()

JAVA Reading a text file and importing a text file

sdf
I am doing this assignment and I got stuck at the very first. I can't even read the given text file. Help me guys. please check the attachment and see where I am wrong
Exception in thread "main" java.io.FileNotFoundException: popData.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at dd.main(dd.java:13)
try use standard Java classes
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(fileName)));
try {
String line;
while ((line = br.readLine()) != null) {
// process line
}
} finally {
br.close();
}

Cassandra Java Exception - NoHostAvailableException

I have setup single Cassandra node on VM. i have to create a table with 70000 columns. for this i have written java code that read json file and create table.
here is my java code snippet.
When i run my java code it throws exception after creation some columns.
Exception stack is
public void createTable(String keyspaceName, String tableName) throws FileNotFoundException{
JSONParser jsonParser = new JSONParser();
FileReader fileReader;
String filePath = "";
String columnHeader = "";
//String completeColumnHeader = "";
try{
System.out.println("Inside Create Table");
session.executeAsync("DROP TABLE IF EXISTS "+keyspaceName+"."+tableName+";");
String createQuery = "CREATE TABLE "+keyspaceName+"."+tableName +"(\"P:LanguageID\" text, "
+ "\"P:PdmarticleID\" text, PRIMARY KEY(\"P:PdmarticleID\",\"P:LanguageID\"));";
session.execute(createQuery);
System.out.println("Table created");
filePath = "CassandraTableColumnHeader/FixColumnHeader.json";
fileReader = new FileReader(filePath);
JSONObject jsonObject = (JSONObject) jsonParser.parse(fileReader);
JSONArray jsonArray = (JSONArray) jsonObject.get("columnHeaderName");
int columnHeaderSize = jsonArray.size();
int columnHeaderBatchSize = 1000;
int fromIndex = 0;
int toIndex = columnHeaderBatchSize;
while(columnHeaderSize > 0){
columnHeaderSize -=columnHeaderBatchSize;
for(int i = fromIndex; i < toIndex; i++) {
columnHeader = (String) jsonArray.get(i);
if(columnHeader.equals("P:PdmarticleID")||columnHeader.equals("P:LanguageID")){
continue;
}
session.execute("ALTER TABLE "+keyspaceName+"."+tableName +" ADD "+"\""+columnHeader+"\""+" text;");
}
fromIndex = toIndex;
if(columnHeaderSize < columnHeaderBatchSize){
toIndex += columnHeaderSize;
}else{
toIndex = toIndex + columnHeaderBatchSize;
}
}
}catch(FileNotFoundException fnfe){
throw fnfe;
}catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.exceptions.DriverException: Host replied with server error: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.io.FileNotFoundException: C:\apache-cassandra-new\data\data\system\schema_columnfamilies-45f5b36024bc3f83a3631034ea4fa697\system-schema_columnfamilies-tmplink-ka-4839-Data.db (The process cannot access the file because it is being used by another process)))
at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(NoHostAvailableException.java:84)
at com.datastax.driver.core.DefaultResultSetFuture.extractCauseFromExecutionException(DefaultResultSetFuture.java:265)
at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:179)
at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:52)
at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:36)
at com.exportstagging.SparkTest.DataLoaderInCassandra.createTable(DataLoaderInCassandra.java:89)
at com.exportstagging.SparkTest.DataLoaderInCassandra.main(DataLoaderInCassandra.java:216)
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.exceptions.DriverException: Host replied with server error: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.io.FileNotFoundException: C:\apache-cassandra-new\data\data\system\schema_columnfamilies-45f5b36024bc3f83a3631034ea4fa697\system-schema_columnfamilies-tmplink-ka-4839-Data.db (The process cannot access the file because it is being used by another process)))
at com.datastax.driver.core.RequestHandler.reportNoMoreHosts(RequestHandler.java:216)
at com.datastax.driver.core.RequestHandler.access$900(RequestHandler.java:45)
at com.datastax.driver.core.RequestHandler$SpeculativeExecution.sendRequest(RequestHandler.java:276)
at com.datastax.driver.core.RequestHandler$SpeculativeExecution$1.run(RequestHandler.java:374)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I have stuck here. Please help me. Thanks in advance.
If I were you I might reevaluate creating a table with 70k column headers. Your partition key P:PdmarticleID and full primary key (P:PdmarticleID, P:LanguageID) are the only two pieces of information you will be able to use to get results anyway. So having these other pieces of information explicitly stored in columns is not buying you anything.
A collection (eg. map) can hold onto 64k items, with certain other limitations (see http://wiki.apache.org/cassandra/CassandraLimitations). Is there a way you can split the columns such that you can create multiple tables, with some pieces of information stored in one table and some in another?

JAXB -Unmarshalling with a general URL

I have a method in Java for unmarshalling XML-files with the given URL.
For URL's like "http:// ..." everything works fine, but for URL's like "file://localhost/C:/Users/.../filename.xml" I receive following exception.
I have no idea why he won't accept my "file://localhost/"-URL's.
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
at preferee.data.access.IO_transfer.jaxb.XMLconverter.getItemFromStream(XMLconverter.java:40)
at preferee.data.access.IO_transfer.jaxb.XMLconverter.getItemFromURL(XMLconverter.java:57)
at preferee.data.access.testServer.LocalTestServer.<init>(LocalTestServer.java:42)
at preferee.data.access.testServer.TestProvider.<init>(TestProvider.java:16)
at preferee.data.access.Providers.createTestProvider(Providers.java:29)
at preferee.tests.FakeServerTests.MovieDao_TEST.run(MovieDao_TEST.java:22)
at preferee.tests.FakeServerTests.MovieDao_TEST.main(MovieDao_TEST.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:999)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
By the way this is my method-implementation:
Class classObject = ... ;
public T getItemFromURL(String url) throws DataAccessException {
JAXBContext jc = null;
T item = null;
try (InputStream XML_Stream = new URL(url).openStream();)
{
jc = JAXBContext.newInstance(classObject);
item = (T) jc.createUnmarshaller().unmarshal(XML_Stream);
} catch (IOException e) {
throw new DataAccessException("( originele error: " + e.getClass() +" ) " + e.getMessage() + ": Kon Bestand niet ophalen of lezen." );
} catch (JAXBException e) {
throw new DataAccessException(e.getMessage());
}
return item;
}
Your URL for hitting your file system is not correct. It should be like:
file:///c|/path/to/file
Update
Will this "file:///" work on other systems like mac, linux?
You can use a file URL on any OS. Of course the URL needs to match the file layout there (i.e. no C drive in Linux).
And is there any way to convert c:\ ... to c|/ ... / easily?
File file = new File("C:/Users/.../filename.xml");
String url = file.toURI().toURL().toString();

Unexpected end of ZLIB input stream while reading InputStream into a file

I'm trying to put an InputStream into a file and getting the following stacktrace:
org.springframework.amqp.rabbit.listener.ListenerExecutionFailedException: Listener method 'handleMessage' threw exception
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.UndeclaredThrowableException
... 1 more
Caused by: java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
Here is the snippet that is causing it
HSLFSlideShow ppt = new HSLFSlideShow(fs)
ObjectData[] embeddes = ppt.getEmbeddedObjects()
embeddes.eachWithIndex { ObjectData entry, int i ->
File f = new File (fileToSave)
f.withOutputStream { w ->
w << entry.getData() //This causes the error
}
}
Here is the link to the ObjectData class getData() method https://poi.apache.org/apidocs/org/apache/poi/hslf/usermodel/ObjectData.html#getData()
I'm using Apache POI 3.10-FINAL

Resources