Category Archives: apache-commons-io

Unable to upload a File using apacheComons and Primefaces 6 [duplicate]

I have a form with an upload link like this

  <p:column headerText="attachments">
  <p:fileUpload value="#{abmBean.file}" mode="simple" />
  <p:commandLink  ajax="false" process="@all" update="@all"  action="#{abmBean.upload()}" value="save"/>
 </p:column>

The form has the proper enctype="multipart/form-data"

In the bean I have the properties

  private UploadedFile fichero;

  private File file;




 public void upload()
 {


            File destFile = new File(fichero.getName());
            FileUtils.copyInputStreamToFile(fichero.getInputstream(), destFile);




 }

What I am looking for is to save in memory that file to be able to work with that, but I get a nullpointer exception in the copyInputstream

FileUtils.write writing speed

I am trying to read from mysql and write the result into a txt file. As you can see I use Apache's Commons IO. Result set contains tweets and every sql query below nearly returns 725 rows to be writed into txt file. My problem is the writing speed, it is very slow (2-3 kb per second). Am I missing something here?

Statement stmt2 = connection.createStatement();
        for (int week = 0 ; week<hashTag.length/15 ; week++){

            File container = new File("C:\\Users\\COMP\\Desktop\\threeMonthsSplitTxt\\weeklyBinsTwitter\\week"+week+"-"+hashTag[week]+".txt");

            for(int hash = 0 ; hash<15 ; hash++){
                ResultSet results = stmt2.executeQuery("select tweetContent
                    from threemonthswithhashtag
                    where hashTag = '"+hashTag[hashCount]+"'
                      and tweetCreatedTime between '"+firstDate[hashCount]+"'
                                               and '"+ lastDate[hashCount]+"';");

                while(results.next()){
                    tweetContent = results.getString("tweetContent");
                    try{
                        FileUtils.write(container,newLine,"UTF8",true);
                        FileUtils.write(container,tweetContent,"UTF8",true);
                    }catch(IOException e){e.getMessage();}
                }
                hashCount++;
            }
        }

Java Apache Common I/O – Multiple IOUtils.copy() error

I got a program that needs to read multiple files in a FTP. But for some readson, it will not let me do a second IOUtils.copy().

Code:

StringWriter stringWriter_getWebVersion = new StringWriter();
IOUtils.copy(FTPManager.retrieveFileStream(root + "/Version.txt"), stringWriter_getWebVersion, "UTF-8");
string_WebVersion = stringWriter_getWebVersion.toString();

IOUtils.copy(FTPManager.retrieveFileStream(root + "/New.txt"), stringWriter_getWebVersion, "UTF-8");
string_WhatsNew = stringWriter_getWebVersion.toString();

And the error:

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1906)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1933)
at net.util.update.Update.setWebVersion(Update.java:30)
at -----(----.java:31)
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)

Note: For testing purposes, I put the same text in both files --> Still doesn't work so the file content isn't the problem.

Java Apache Common I/O – Multiple IOUtils.copy() error

I got a program that needs to read multiple files in a FTP. But for some readson, it will not let me do a second IOUtils.copy().

Code:

StringWriter stringWriter_getWebVersion = new StringWriter();
IOUtils.copy(FTPManager.retrieveFileStream(root + "/Version.txt"), stringWriter_getWebVersion, "UTF-8");
string_WebVersion = stringWriter_getWebVersion.toString();

IOUtils.copy(FTPManager.retrieveFileStream(root + "/New.txt"), stringWriter_getWebVersion, "UTF-8");
string_WhatsNew = stringWriter_getWebVersion.toString();

And the error:

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1906)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1933)
at net.util.update.Update.setWebVersion(Update.java:30)
at -----(----.java:31)
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)

Note: For testing purposes, I put the same text in both files --> Still doesn't work so the file content isn't the problem.

Java Apache Common I/O – Multiple IOUtils.copy() error

I got a program that needs to read multiple files in a FTP. But for some readson, it will not let me do a second IOUtils.copy().

Code:

StringWriter stringWriter_getWebVersion = new StringWriter();
IOUtils.copy(FTPManager.retrieveFileStream(root + "/Version.txt"), stringWriter_getWebVersion, "UTF-8");
string_WebVersion = stringWriter_getWebVersion.toString();

IOUtils.copy(FTPManager.retrieveFileStream(root + "/New.txt"), stringWriter_getWebVersion, "UTF-8");
string_WhatsNew = stringWriter_getWebVersion.toString();

And the error:

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1906)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1933)
at net.util.update.Update.setWebVersion(Update.java:30)
at -----(----.java:31)
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)

Note: For testing purposes, I put the same text in both files --> Still doesn't work so the file content isn't the problem.

Java Apache Common I/O – Multiple IOUtils.copy() error

I got a program that needs to read multiple files in a FTP. But for some readson, it will not let me do a second IOUtils.copy().

Code:

StringWriter stringWriter_getWebVersion = new StringWriter();
IOUtils.copy(FTPManager.retrieveFileStream(root + "/Version.txt"), stringWriter_getWebVersion, "UTF-8");
string_WebVersion = stringWriter_getWebVersion.toString();

IOUtils.copy(FTPManager.retrieveFileStream(root + "/New.txt"), stringWriter_getWebVersion, "UTF-8");
string_WhatsNew = stringWriter_getWebVersion.toString();

And the error:

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1906)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1933)
at net.util.update.Update.setWebVersion(Update.java:30)
at -----(----.java:31)
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)

Note: For testing purposes, I put the same text in both files --> Still doesn't work so the file content isn't the problem.