日期:2014-05-19  浏览次数:20998 次

sun.io.MalformedInputException
在windows平台运行没错,但是放到linux平台就出错...
报的错:
sun.io.MalformedInputException
at sun.io.ByteToCharUTF8.convert(ByteToCharUTF8.java:262)
at sun.nio.cs.StreamDecoder$ConverterSD.convertInto(StreamDecoder.java:316)
at sun.nio.cs.StreamDecoder$ConverterSD.implRead(StreamDecoder.java:366)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:252)
at java.io.InputStreamReader.read(InputStreamReader.java:212)
at java.io.BufferedReader.fill(BufferedReader.java:157)
at java.io.BufferedReader.readLine(BufferedReader.java:320)
at java.io.BufferedReader.readLine(BufferedReader.java:383)
at app.util.ReadAndInsert1.copy(ReadAndInsert1.java:64)
at app.util.ToGetFile.run_ylcard(ToGetFile.java:116)
at app.util.ToGetFile.run(ToGetFile.java:27)
at java.util.TimerThread.mainLoop(Timer.java:537)
at java.util.TimerThread.run(Timer.java:487)
程序思路:读取一个文件a,并拷贝到b,然后把b中的数据插入到数据库中
public static String read(String path) {
StringBuffer res = new StringBuffer();
String line = null;
File file = new File(path);

String result = "";
StringBuffer sBuffer=new StringBuffer();
try{
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
String tempStr;
byte[] b = new byte[1024];
int i = 0;
while ((i = bis.read(b)) > 0) {
tempStr=new String(b,0,i);
sBuffer.append(tempStr);
}
result = sBuffer.toString();
}catch(Exception e){
e.printStackTrace();
}
return result;
}

public static boolean copy(String path,String path1) {
boolean flage = false;
File file = new File(path);
File file1 = new File(path1);
int mu = 0;
try {
String s = read(path);
InputStreamReader read = new InputStreamReader(new FileInputStream(
file), "UTF-8");
BufferedReader bw = new BufferedReader(read);

while (bw.readLine() != null) { mu++;
}
bw.close();
OutputStreamWriter write = new OutputStreamWriter(
new FileOutputStream(file1), "UTF-8");
BufferedWriter bwr = new BufferedWriter(write);

bwr.write(s);
bwr.flush();
bwr.close();
flage = tree(path1, mu);
} catch (Exception e) {
e.printStackTrace();
}
return flage;
}

public synchronized static boolean tree(String path, int linenum) {
File file = new File(path);
String[] str = null;

boolean flage = false;
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(
file), "UTF-8");
BufferedReader bw = new BufferedReader(read);
String line = bw.readLine();
String[] list = new String[linenum];
int m = 0;
String sql = "";
String selectfile = path.substring(path.length() - 14, path
.length() - 13);
while (line != null) {
str = stringAnalytical(line, ';');

。。。。。。

现在是一运行到 红字部分就报错,求各位高手帮助!

------解决方案--------------------
while (bw.readLine() != null)改成 while (null != bw.readLine())
------解决方案--------------------
windows平台用的编码格式是 gbk的 
而linux用的编码格式是 utf-8的

所以会出现这样的错误、
 
不知楼主是否有node++ 这个文本编辑器?

在windows把你要的文件编码格式改成无bom 的utf-8试试。