为什么就不能输出汉字呢?

来源:互联网 发布:SQL如何删除默认约束 编辑:程序博客网 时间:2024/05/21 05:04

 我想从键盘输入文件的地址,然后对文件进行操作。在输入文件路径时候,我发现,输入任何带有汉字的路径,或者文件时候,它都显示乱码,报错了,我怎么才能让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:/???¨??????

  brooksychen(初晨之阳) ( 四级(中级)) 信誉:100    Blog   加为好友 2007-4-25 19:54:29 得分: 2

函数改成:

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;
}

原创粉丝点击