安卓无法读取文件
我在我的项目中使用这段代码。我需要阅读我放在原始文件夹中的 .xls。 ReadExcel 测试 = new ReadExcel();
test.setInputFile(BitmapFactory.decodeStream(getClass().getResourceAsStream(("/SPPDashProject/res/raw/aging_busket_key.xls"))).toString());
try {
test.read();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
我应该给出什么路径
public class ReadExcel {
private String inputFile;
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 (cell.getType() == CellType.LABEL) {
System.out.println("I got a label: "
+ cell.getContents());
}
if (cell.getType() == CellType.NUMBER) {
System.out.println("I got a number "
+ cell.getContents());
}
}
}
} catch (BiffException e) {
e.printStackTrace();
}
}
}
,因为我的主要读取类采用字符串格式的路径。请建议
I am using this code in my project. I need to read .xls which i have placed in my raw folder.
ReadExcel test = new ReadExcel();
test.setInputFile(BitmapFactory.decodeStream(getClass().getResourceAsStream(("/SPPDashProject/res/raw/aging_busket_key.xls"))).toString());
try {
test.read();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
public class ReadExcel {
private String inputFile;
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 (cell.getType() == CellType.LABEL) {
System.out.println("I got a label: "
+ cell.getContents());
}
if (cell.getType() == CellType.NUMBER) {
System.out.println("I got a number "
+ cell.getContents());
}
}
}
} catch (BiffException e) {
e.printStackTrace();
}
}
}
what path should i give as my main read class takes path in string format.Plz suggest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下代码:
You can use the following code:
您应该使用以下代码打开 /res/raw
中的文件
getResources().openRawResource(R.raw.yourfilename)
您可以将文件放在资产文件夹中并使用 AssetManager 来访问它。
You should use the following code to open file in the /res/raw
getResources().openRawResource(R.raw.yourfilename)
You can put your file in the assets folder and use AssetManager to acess it.