日期:2014-05-20  浏览次数:21094 次

为什么就不能输出汉字呢?
我想从键盘输入文件的地址,然后对文件进行操作。在输入文件路径时候,我发现,输入任何带有汉字的路径,或者文件时候,它都显示乱码,报错了,我怎么才能让inputadd()函数返回一个正确的字符串呢?

import   java.io.*;

public   class   Try  
{
static   String   inputadd()   throws   IOException
{
String   inadd   = " ";
char   ch;
System.out.print( "输入地址: ");
while((ch=(char)System.in.read())!= '\n ')
inadd=inadd+ch;
return(inadd.trim()); //去掉两边的空格

}
public   static   void   main(String[]   args)   throws   IOException  
{
System.out.print(inputadd());

}

}

显示:
输入地址:c:\新建文件夹
c:\???¨??????

------解决方案--------------------
函数改成:

static String inputadd() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputadd = " ";
try {
inputadd = br.readLine().trim();
} catch (IOException e) {
e.printStackTrace();
}
return inputadd;
}