java中的非法字符错误

java中的非法字符错误,java,apache-poi,Java,Apache Poi,我正在尝试使用java读取excel文件。编译程序时,我发现错误为非法字符。帮我解决这个问题。这是密码 import java.io.IOException; import java.io.*; import java.util.Iterator; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.

我正在尝试使用java读取excel文件。编译程序时,我发现错误为非法字符。帮我解决这个问题。这是密码

import java.io.IOException;
import java.io.*;
import java.util.Iterator;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;



public class Readingexcel {

public static void main(String[] args) {
    try {
         System.out.println("before reading");
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("input.xlsx"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheet("first");

        HSSFRow row;
        HSSFCell cell;
        String s;

        int rows; // No of rows
        rows = sheet.getPhysicalNumberOfRows();
         System.out.println(rows);
         System.out.println(sheet.getRow(1).getPhysicalNumberOfCells());
        int cols = 0; // No of columns
        int tmp = 0;

// This trick ensures that we get the data properly even if it doesn’t start from first few rows
        for (int i = 0; i < 10 || i < rows; i++) {
            row = sheet.getRow(i);

            if (row != null) {
                tmp = sheet.getRow(i).getPhysicalNumberOfCells();


                if (tmp > cols) {
                    cols = tmp;

                }
            }
        }

        for (int r = 0; r < rows; r++) {
            row = sheet.getRow(r);
            if (row != null) {
                for (int c = 0; c < cols; c++) {
                    cell = row.getCell((short) c);
                    if (cell != null) {
// Your code here
// s = cell.getData();
                       System.out.println(cell.getStringCellValue());


                    }
                }
            }
        }
    } catch (Exception ioe) {
        ioe.printStackTrace();
    }

}
}

谁能帮我解决这个问题。

你用什么程序编辑源代码?它看起来像是在插入一个javac不喜欢的。应该可以在编辑器的设置中禁用BOM表。

我很确定这就是BOM表()。确保使用正确的编码。可能在其他编辑器中打开文件并重新保存,以便删除BOM表。例如,Nodepad++可以帮助您

祝你好运,
Max

看起来您有BOM问题。在十六进制编辑器中打开文件,删除第一个看起来奇怪的字节,这些字节位于第一个导入行之前。

文件开头有一些不可见的字符。如果你懒得修复它们,只需将源代码复制到一个新文件中,然后删除旧文件。看起来你得到了
∩╗┐在您的源代码中。。。在记事本中打开.java文件时会发生什么?@L7ColWinters这是一个编译时错误,与input.xlsx无关。是否剪切并粘贴了导入?导入前可能存在无法打印的字符串。请在其他地方重新键入导入内容并删除此行。@克里斯蒂安,我试过了,但仍然出现错误
Microsoft Windows XP [Version 5.1.2600]
 (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\vino>cd C:\Program Files\jdk1.6.0_10\bin

C:\Program Files\jdk1.6.0_10\bin>javac Readingexcel.java
Readingexcel.java:1: illegal character: \187
import java.io.IOException;
^
Readingexcel.java:1: illegal character: \191
import java.io.IOException;
^
2 errors