标签: fileinputstream

Apache POI - java.lang.NoClassDefFoundError:org/apache/commons/compress/utils/InputStreamStatistics

我正在尝试使用 Apache POI 读取 Word 文档 (.docx),但出现此错误...

线程“AWT-EventQueue-0”中的异常java.lang.NoClassDefFoundError:org/apache/commons/compress/utils/InputStreamStatistics at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.(ZipArchiveThresholdInputStream.java:63) at org. apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:178) 在 org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:104) 在 org.apache.poi.openxml4j.opc .OPCPackage.open(OPCPackage.java:301)在gui.CryptoGUI.selectFileButtonActionPerformed(CryptoGUI.java:645)在gui.CryptoGUI.access$1100(CryptoGUI.java:27)在gui.CryptoGUI$11.actionPerformed(CryptoGUI.java: 321) 在 javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 在 javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) 在 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 在 com.jtattoo.plaf.BaseButtonListener.mouseReleased(BaseButtonListener.java:65)在 java.awt.Component.processMouseEvent(Component.java:6535) 在 javax.swing.JComponent.processMouseEvent(JComponent.java:3324) 在 java.awt.Component.processEvent(Component.java:6300) 在 java.awt。 Container.processEvent(Container.java:2236)在java.awt.Component.dispatchEventImpl(Component.java:4891)在java.awt.Container.dispatchEventImpl(Container.java:2294)在java.awt.Component.dispatchEvent(Component) .java:4713)在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)在java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)在java.awt.Container.dispatchEventImpl(Container.java:2280)在java.awt.Window.dispatchEventImpl(Window.java:2750)在java.awt.Component.dispatchEvent(Component.java:4713)在java.awt。 EventQueue.dispatchEventImpl(EventQueue.java:758) 在 java.awt.EventQueue.access$500(EventQueue.java:97) 在 java.awt.EventQueue$3.run(EventQueue.java:709) 在 java.awt.EventQueue$3。在 java.security.AccessController.doPrivileged(本机方法)处运行(EventQueue.java:703) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain. java:86) 在 java.awt.EventQueue$4.run(EventQueue.java:731) 在 java.awt.EventQueue$4.run(EventQueue.java:729) 在 java.security.AccessController.doPrivileged(Native Method) 在 java .security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 在 java.awt。EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread) .java:101) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我的类路径中有以下库,

在此输入图像描述

这是我收到此错误的代码

private …
Run Code Online (Sandbox Code Playgroud)

java fileinputstream apache-poi java-io apache-poi-4

3
推荐指数
1
解决办法
1万
查看次数

使用Java的FileInputStream

java.io.FileInputStream,有一种方法int read(Byte[] buffer,int offset,int numBytes); 我们如何使用这个功能 - 这个方法有什么区别read(byte[] buffer)吗?

java io stream fileinputstream

2
推荐指数
1
解决办法
7383
查看次数

从Java FileInputStream读取字节时,如何区分-1和0xff?

因此,我不得不参加面试,我需要编写一个执行简单XOR加密的迷你应用程序,并遇到了这个问题.我使用FileInputReader来拉入每个字节,使用密钥执行XOR操作,并将结果推回FileOutputStream.什么让我思考.

FileInputStream返回一个32位有符号类型的int.只接收一个字节时,可以将其转换为"字节"类型.如果FileInputStream达到EOF,它也返回-1.但是,在二进制补码二进制中-1 == 0xff,那么如果读取的字节真的是0xff而不是EOF怎么办?

除了特殊情况(例如EOF)之外,0xff是一个数学上不会返回的字节吗?或者这是您可能需要考虑的情况,具体取决于您正在阅读的数据?

java fileinputstream eof twos-complement

2
推荐指数
1
解决办法
3569
查看次数

在Android中指定XML文件的文件夹路径

我将employeedetailxml放入名为"res\raw"的文件夹中.当我尝试通过指定文件和文件夹名称打开它时,我收到错误"找不到文件".

如何指定文件的路径?

我更希望能够传递' R.raw.employeedetailxml ' FileInputStream来指定它打开该文件.那可能吗?(我尝试过,但得到了一个错误.)

可以FileInputStream将资源ID作为参数吗?

try{
   SAXParserFactory spf=SAXParserFactory.newInstance();
   SAXParser sp=spf.newSAXParser();
   XMLReader xr=sp.getXMLReader();

   EmployeeDetailHandler empDetailHandler=new EmployeeDetailHandler();
   xr.setContentHandler(empDetailHandler);

   xr.parse(new InputSource(new FileInputStream("\\res\\raw\\employeedetailxml.xml")));
}
Run Code Online (Sandbox Code Playgroud)

android fileinputstream

2
推荐指数
2
解决办法
4822
查看次数

将ZipOutputStream转换为FileInputStream

我有一个代码,它接受一个文件,压缩它,并将其存储在数据库中

看起来像这样

          // STEP 1 - Create a ZIP file
          byte[] buffer = new byte[1024];// 18024
          ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream("file.zip"));
          outZip.setLevel(Deflater.DEFAULT_COMPRESSION);
          FileInputStream in = new FileInputStream(c:\file.hex);
          outZip.putNextEntry(new ZipEntry("file.hex"));
          int len;
          while (( len = in.read(buffer)) > 0)
                outZip.write(buffer, 0, len);
          outZip.closeEntry();
          in.close();
          outZip.close();

          // STEP 2 - Insert zip file to DB
          file = new File("file.zip");
          FileInputStream fis = new FileInputStream( file );
Run Code Online (Sandbox Code Playgroud)

我存储在数据库中的fis对象

但我想完全避免文件系统,而不是创建file.zip.我想我需要直接将ZipOutputStream转换为FileInputStream,但我找不到办法.

有一个简单的方法来实现这一目标吗?

  • 当我提取文件时,我也有同样的问题,为了阅读它我必须创建2个不同的临时文件 - file.zip和file.hex

java io fileinputstream zipoutputstream

2
推荐指数
1
解决办法
5351
查看次数

从内部存储中读取ArrayList

我有一个Android应用程序,我想读取和写入ArrayList<MyClass>内部存储.

写作部分工作(我相信,尚未测试它:-)):

ArrayList<MyClass> aList;

    public void saveToInternalStorage() {
    try {
        FileOutputStream fos = ctx.openFileOutput(STORAGE_FILENAME, Context.MODE_PRIVATE);
        fos.write(aList.toString().getBytes());
        fos.close();
    }
    catch (Exception e) {
        Log.e("InternalStorage", e.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

但我现在要做的是从存储中读取整个ArrayList并将其作为ArrayList返回,如下所示:

public ArrayList<MyClass> readFromInternalStorage() {
        ArrayList<MyClass> toReturn;
        FileInputStream fis;
        try {
            fis = ctx.openFileInput(STORAGE_FILENAME);

            //read in the ArrayList
            toReturn = whatever is read in...

            fis.close();
        } catch (FileNotFoundException e) {
            Log.e("InternalStorage", e.getMessage());
        } catch (IOException e) {
            Log.e("InternalStorage", e.getMessage()); 
        }
        return toReturn
}
Run Code Online (Sandbox Code Playgroud)

我以前从未在Android文件中读过,所以我不知道这是否可能.但是我有一种方式可以阅读我的习惯ArrayList吗?

storage android arraylist fileinputstream

2
推荐指数
1
解决办法
3441
查看次数

用Java编写的StringBuilder

我有一个方法从sdcard(Android)加载文件,然后使用StringBuilder读取它.我正在阅读的文本是用我的母语字符写的,例如ąźż... StringBuilder(或FileInputStream)不能正确地读取它们.我如何设置正确的编码?

这是代码:

File file = new File(filePath);
            FileInputStream fis = null;
            StringBuilder builder = new StringBuilder();

            try {
                fis = new FileInputStream(file);
                int content;
                while ((content = fis.read()) != -1) {
                    builder.append((char) content);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            System.out.println("File Contents = " + builder.toString());
            contactService.updateContacts(builder.toString());
Run Code Online (Sandbox Code Playgroud)

stringbuilder encoding file fileinputstream

2
推荐指数
1
解决办法
3万
查看次数

从文件中读取对象

我可以使用以下代码将列表写入文件:

MyList<Integer> l = new MyList<Integer>();
//...

FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream writer = new ObjectOutputStream(fos);

writer.writeObject(l);
writer.close();
Run Code Online (Sandbox Code Playgroud)

现在我想从文件中读取列表,并尝试使用此代码:

MyList<Integer> list = new MyList<Integer>();
//...

FileInputStream fis = new FileInputStream(filename);
ObjectInputStream reader = new ObjectInputStream(fis);

list = (MyList<Integer>) reader.readObject();   
reader.close();
Run Code Online (Sandbox Code Playgroud)

但是现在我SuppressWarnings unchecked从Eclipse 获得了一个,我必须检查ClassNotFoundException.为什么这样,我该如何防止这种情况?

java file fileinputstream fileoutputstream

2
推荐指数
1
解决办法
122
查看次数

第二次传递给java中的函数时,FileInputStream正在关闭

 public static void main(String[] args){
        try{
            FileInputStream fileInputStream=new FileInputStream("sourcefile.txt");
            File file1=new File("copy1.txt");
            File file2=new File("copy2.txt");
            //firstcopy
            FileOutputStream fileOutputStream1=new FileOutputStream(file1);
            fileCopy(fileInputStream, fileOutputStream1);
            fileOutputStream1.close();
            //secondcopy
            FileOutputStream fileOutputStream2=new FileOutputStream(file2);
            fileCopy(fileInputStream, fileOutputStream2); 
            fileOutputStream2.close();

        }catch(FileNotFoundException fileNotFoundException){
            System.err.println(fileNotFoundException.getMessage());
            fileNotFoundException.printStackTrace();
        }catch(IOException ioException){
            ioException.printStackTrace();
        }

    }
    //file copy function
    public static void fileCopy(FileInputStream fileInputStream,FileOutputStream fileOutputStream) throws IOException{
        byte[] buffer = new byte[1024];

        int length;
        //copy the file content in bytes 
        while ((length = fileInputStream.read(buffer)) > 0){

            fileOutputStream.write(buffer, 0, length);

        }

        fileInputStream.close();
        fileOutputStream.close();

        System.out.println("File is copied successful!");
    }
Run Code Online (Sandbox Code Playgroud)
Output: …

java fileinputstream

2
推荐指数
1
解决办法
1031
查看次数

如何对使用Scanner和InputStream的方法进行单元测试?

所以这是代码:

public int foo(InputStream in) {
    int counter = 0;
    Scanner scanner = new Scanner(in);
    while(scanner.hasNextLine()) {
        counter++;
        scanner.nextLine();
    }
    return counter;
}
Run Code Online (Sandbox Code Playgroud)

通常,我会将FileInputStream传递给此方法,但是我希望能够在不访问物理文件的情况下对其进行测试。

我应该模拟File对象吗?如何实施

@Test
public void shouldReturnThree(){

}
Run Code Online (Sandbox Code Playgroud)

java unit-testing inputstream fileinputstream

2
推荐指数
1
解决办法
2391
查看次数