不能小看编码引发的问题,迷迷糊糊害死人


一般系统能够根据第一个字节来判断文件是使用什么编码格式,最好推荐使用是UTF8没有BOM格式的编码,(ISO8859-1不能显示中文,但是别的国家用的特别多)编码问题特别蛋疼,还有全角半角之分,(我特别讨厌全角符号和全角英文,设计者在这一方面很蠢,都不知道多少人因为这个踩过坑,因为不小心按了组合键,变成全角的,然后代码死活编译失败,查了很久找不出有啥错误)总是很容易忽视细节问题,比如window下的换行是\r\n而Linux下是\n

推荐用户使用目录路径为C:/programe file/ 而不是C:\programe file\这样的路径,window支持/路径

因为后者要转义成C:\\programe file\\比较麻烦,而且很多时候很纠结要不要这样写



package com.copycat.test;
import java.io.File;  
import java.io.InputStreamReader;  
import java.io.BufferedReader;    
import java.io.FileInputStream;  
public class ReadTest {  
    public static void main(String args[]) 
    {  
        try {   
            /* 读入TXT文件 */  
            String pathname = "G:\\input.txt";  
            File filename = new File(pathname);  
            InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));  
            BufferedReader br = new BufferedReader(reader);  
            String line = "";  
            line = br.readLine();
            String  str=line;
            String str2="\\n";   //验证\n在内存里的形式
            String str3="\n";    //结果输出为:the same to the str2
            if(str.equals(str2))
            {
            	System.out.print("the same to the str2");
            }
            else if(str.equals(str3))
            {
            	System.out.println("the same to the str3");
            }
            else
            {
            	System.out.println("unkown error");
            }
            br.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值