java FileNotFoundException:[excel在本地计算机上的位置]文件名、目录名或卷标语法不正确
(我已经尝试了stackoverflow上的所有可用答案,但似乎没有任何帮助) 我在Eclipse(Luna)IDE的两个不同项目中编写了两个脚本。此脚本从excel中读取,然后写入。(注意#我在两个项目中都使用了ExcelReadWrite类)两个脚本几乎相同,但只有一个工作正常,另一个脚本抛出异常。 错误日志
Exception in thread "main" java.io.FileNotFoundException: D:\AutomationMobile\Testing.xls (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at apr_package_mortgage_day4.ExcelReadWrite.save_excel(ExcelReadWrite.java:126)
at apr_package_mortgage_day4.Driver_Age_apr.main(Driver_Age_apr.java:32)
在上面的错误日志中,我单击了本机方法(超链接),这将打开FileOutputStream。班 其中包含“源未找到源附件不包含文件FileOutputStream.class的源” 我点击了一个按钮——“更改附加源”,打开了一个名为“源附加配置”的向导,上面写着“选择包含rt.JAR源的位置(文件夹、JAR或zip)” 我是Java新手,因此我不知道这里需要做什么。请帮忙
//The below script throws error -
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//start_Server();
ExcelReadWrite xl = new ExcelReadWrite("D:\\AutomationMobile\\Testing.xls");
HSSFSheet Sheet1 = xl.Setsheet("Sheet1");
int rowCount = xl.getrowcount(Sheet1);
for(int i = 1; i<=rowCount;i++)
{
String age = xl.Readvalue(Sheet1, i, "Age");
double Final_Age = Double.parseDouble(age);
if(Final_Age>=18)
{
xl.writecell(Sheet1, i, "Status", "Major");
}
else
{
xl.writecell(Sheet1, i, "Status", "Minor");
}
}
xl.save_excel("D:\\AutomationMobile\\Testing.xls");
}
//ExcelReadWrite类
public class ExcelReadWrite {
FileInputStream fis;
HSSFWorkbook wb;
public ExcelReadWrite(String xlPath) throws IOException
{
fis= new FileInputStream(xlPath);
//workbook object
wb= new HSSFWorkbook(fis);
}
public HSSFSheet Setsheet(String sheetname)
{
HSSFSheet Sheet = wb.getSheet(sheetname);
return Sheet;
}
public int getrowcount(HSSFSheet Sheet)
{
int lastRowNum = Sheet.getLastRowNum();
return lastRowNum;
}
public int getcolcount(HSSFSheet Sheet,int rowIndex)
{
int lastcolnum = Sheet.getRow(rowIndex).getLastCellNum();
return lastcolnum;
}
public String Readvalue(HSSFSheet Sheet,int rowIndex, int cellnum)
{
HSSFCell cell = Sheet.getRow(rowIndex).getCell(cellnum);
String celltext=null;
if(cell==null)
celltext="";
else if(cell.getCellType()==cell.CELL_TYPE_STRING)
celltext=cell.getStringCellValue();
else if(cell.getCellType()==cell.CELL_TYPE_NUMERIC)
celltext=String.valueOf(cell.getNumericCellValue());
else if(cell.getCellType()==cell.CELL_TYPE_BLANK)
celltext="";
return celltext;
}
public String Readvalue(HSSFSheet Sheet,int rowIndex, String colname)
{
int colindex = 0;
for(int i=0;i<getcolcount(Sheet,0);i++){
//System.out.println(row.getCell(i).getStringCellValue().trim());
if(Sheet.getRow(0).getCell(i).getStringCellValue().trim().equalsIgnoreCase(colname))
colindex=i;
}
return Readvalue(Sheet,rowIndex, colindex);
}
public void writecell(HSSFSheet Sheet,int rowIndex,int cellnum, String wvalue )
{
//writing the cell
HSSFCell writecell = Sheet.getRow(rowIndex).getCell(cellnum);
if(writecell==null)
{
writecell=Sheet.getRow(rowIndex).createCell(cellnum);
}
writecell.setCellValue(wvalue);
}
public void writecell(HSSFSheet Sheet,int rowIndex,String colname, String wvalue ){
int colindex = 0;
for(int i=0;i<getcolcount(Sheet,0);i++){
//System.out.println(row.getCell(i).getStringCellValue().trim());
if(Sheet.getRow(0).getCell(i).getStringCellValue().trim().equalsIgnoreCase(colname))
colindex=i;
}
writecell(Sheet,rowIndex,colindex, wvalue );
}
public void save_excel(String xlPath) throws IOException
{
fis.close();
FileOutputStream fos= new FileOutputStream(xlPath);
wb.write(fos);
fos.close();
}
共 (0) 个答案