Java中出现“错误: 编码GBK的不可映射字符”的解决方法

我的java文件里出现中文,是这样一个文件:

import java.io.*;
public class Test {
	public static void main(String[] args) {
		String separator = File.separator;
		String filename = "myfile.txt";
		String directory = "mydir1" + separator + "mydir2";
		File f = new File(directory , filename);
		if(f.exists()) {
			System.out.println("文件名称:" + f.getAbsolutePath());
			System.out.println("文件大小:" + f.length());
		} else {
			f.getParentFile().mkdirs();
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

在编译时出现错误:


这是由于代码中出现了utf-8字符。解决方式例如以下:

用notepad打开该java文件,另存为,编码为ANSI


在此编译。没有问题:


posted @ 2016-02-01 21:44  blfshiye  阅读(572)  评论(0编辑  收藏  举报