2015-12-30 29 views
0

我在我的資產文件夾中有一個xls文件,我想使用jexcelapi庫打開它。我已經將文件路徑設置爲(「file:///android_asset/jxlrwtest.xls」),甚至嘗試使用New-> File在根文件夾中創建具有不同名稱的xls文件並直接訪問它,但仍然是我每次運行模擬器時都會得到錯誤java.io.FileNotFoundException「(沒有這樣的文件或目錄)」。我也確保每次嘗試移動文件時都會刷新路徑。我的閱讀Excel文件整個類是資產文件目錄給出java.io.FileNotFoundException eclipse

package com.example.kirikedictionary; 

import java.io.File; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 

import jxl.Cell; 
import jxl.CellType; 
import jxl.Sheet; 
import jxl.Workbook; 
import jxl.read.biff.BiffException; 

public class ReadExcel { 

    private String inputFile; 
    protected Context context; 

    public ReadExcel(Context context){ 
     this.context = context.getApplicationContext(); 
    } 


    public void setInputFile(String inputFile) { 
    this.inputFile = inputFile; 
    } 

    public void read() throws IOException { 
    File inputWorkbook = new File(inputFile); 
    Workbook w; 
    try { 
     w = Workbook.getWorkbook(inputWorkbook); 
     // Get the first sheet 
     Sheet sheet = w.getSheet(0); 
     // Loop over first 10 column and lines 

     for (int j = 0; j < sheet.getColumns(); j++) { 
     for (int i = 0; i < sheet.getRows(); i++) { 
      Cell cell = sheet.getCell(j, i); 
      CellType type = cell.getType(); 
      if (type == CellType.LABEL) { 
      System.out.println("I got a label " 
       + cell.getContents()); 
      } 

      if (type == CellType.NUMBER) { 
      System.out.println("I got a number " 
       + cell.getContents()); 
      } 

     } 
     } 
    } catch (BiffException e) { 
     e.printStackTrace(); 
    } 
    } 

    public void main(String[] args) throws IOException { 
    ReadExcel test = new ReadExcel(context); 
    test.setInputFile("file:///android_asset/jxlrwtest.xls"); 
    test.read(); 
    } 

} 

,我不斷收到錯誤

1月12日至30日:36:59.386:W/System.err的(1851):java.io .FileNotFoundException: file:/android_asset/jxlrwtest.xls:打開失敗:ENOENT(沒有這樣的文件 或目錄)12-30 01:36:59.393:W/System.err(1851):at libcore.io.IoBridge .open(IoBridge.java:456)12-30 01:36:59.393: W/System.err(1851):at java.io.FileInputStream。(FileInputStream.java:76)12-30W/System.err(1851):at jxl.Workbook.getWorkbook(Workbook.java:213)12-30 01:36:59.394: W/System.err(1851):at jxl.Workbook.getWorkbook(Workbook.java:198) 12-30 01:36:59.394:W/System.err(1851):at com.example.kirikedictionary.ReadExcel.read(ReadExcel.java:33)12 -30 01:36:59.394:W/System.err(1851):at com.example.kirikedictionary.ReadExcel.main(ReadExcel.java:62)12-30 01:36:59.394:W/System。 err(1851):at com.example.kirikedictionary.MainActivity $ 3.onItemClick(MainActivity.java:148) 12-30 01:36:59.394:W/System.err(1851):at android.widget.AdapterView .performItemClick(AdapterView.java:300) 12-30 01:36:59.395:W/Syst em.err(1851):at android.widget.AbsListView.performItemClick(AbsListView.java:1143) 12-30 01:36:59.416:W/System.err(1851):at android.widget.AbsListView $ PerformClick.run(AbsListView.java:3044) 12-30 01:36:59.416:W/System.err(1851):at android.widget.AbsListView $ 3.run(AbsListView.java:3833)12-30 01:36:59.416:W/System.err(1851):at android.os.Handler.handleCallback(Handler.java:739)12-30 01:36:59.417:W/System.err(1851): at android.os.Handler.dispatchMessage(Handler.java:95)12-30 01:36:59.432:W/System.err(1851):at android.os.Looper.loop(Looper.java:135 )12-30 01:36:59.432: W/System.err(1851):在 android.app.ActivityThread.main(ActivityThread.java:5221)12-30 01:36:59.432:W/System.err(1851):at java.lang.reflect.Method.invoke(Native Method)12 -30 01:36:59.432: W/System.err(1851):at java.lang.reflect.Method.invoke(Method.java:372)12-30 01:36:59.433: W/System。 err(1851):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 12-30 01:36:59.433:W/System.err(1851):at com。 android.internal.os.ZygoteInit.main(ZygoteInit.java:694)12-30 01:36:59.433:W/System.err(1851):引起: android.system.ErrnoException:打開失敗:ENOENT(沒有這樣的文件或 目錄)12-30 01:36:59.434:W/System.err(1851):at libcore.io.Posix.open(Native Method)12-30 01:36:59.435: W/System.err(1851):at libcore.io.BlockGuardOs.open(BlockGuardO .java:186)12-30 01:36:59.451:W/System.err(1851):at libcore.io.IoBridge。開(IoBridge.java:442)1月12日至30日:36:59.452: W/System.err的(1851):... 18多個

請幫助,因爲我不希望有使用外部SD卡文件,我在這裏呆了一段時間。

+0

請將您的項目導入到Android Studio。 Eclipse已被棄用,它會產生更多像這樣的問題 – piotrek1543

回答

1

您只能將file:/// android_asset/someName用作WebView的URL。 它不是由文件系統識別的文件名。

使用Context.getAssets獲取AssetManger實例,並使用AssetManager.open獲取資產文件的InputStream。

如果您需要提供文件實例,請將文件從資產複製到應用程序的專用目錄中。

+0

請關於如何做到這一點的任何示例? –

+0

所以我看到這個解決方案從資產文件夾複製到應用程序目錄,現在我可以在那裏編輯文件http://stackoverflow.com/a/16984117/5728859 –

相關問題