Java PrintWriter println()不打印出UTF-8编码,尽管有以前的规范

Java PrintWriter println()不打印出UTF-8编码,尽管有以前的规范,java,utf-8,Java,Utf 8,我在printWriter写入allready创建的.txt文件时遇到问题。尽管在创建PrintWriter对象时,我指定要使用UTF-8格式,但在尝试打开输出文件(使用gedit sudo gedit nano)时,我看到的都是不可读的和一些中文字符(我假设它们是中文)。更令人困惑的是,当我将要插入文件的数据打印到标准输出(system.out.println)时,数据打印得很好!此外,在通过终端将输出文件填充到文件名后执行file命令,所有命令返回的都是“file-name.txt:data

我在printWriter写入allready创建的.txt文件时遇到问题。尽管在创建PrintWriter对象时,我指定要使用UTF-8格式,但在尝试打开输出文件(使用gedit sudo gedit nano)时,我看到的都是不可读的和一些中文字符(我假设它们是中文)。更令人困惑的是,当我将要插入文件的数据打印到标准输出(system.out.println)时,数据打印得很好!此外,在通过终端将输出文件填充到文件名后执行file命令,所有命令返回的都是“file-name.txt:data”,而不是我期望看到的UTF-8!我包括了相关的代码,提前感谢您的建议

用于聚合要打印的数据的函数:

public String DumpData(){
    String dump = "\0";
    dump = dump.concat(Long.toString(MessageID)).concat(",").concat(Long.toString(userID)).concat(",").concat(UserScreenName).concat(",").concat(Float.toString(Latitude)).concat(",").concat(Float.toString(Longitude)).concat(",").concat(Location).concat(",").concat(MessageDate).concat(",").concat(Integer.toString(IsFavourite)).concat(",").concat(Integer.toString(FollowersCount)).concat(",").concat(Integer.toString(FollowingsCount)).concat(",").concat(Integer.toString(Mentions)).concat(",").concat(Integer.toString(URLCount)).concat(",").concat(Integer.toString(Anger_CNT)).concat(",").concat(Integer.toString(Disgust_CNT)).concat(",").concat(Integer.toString(Fear_CNT)).concat(",").concat(Integer.toString(Sadness_CNT)).concat(",").concat(Integer.toString(Surprise_CNT)).concat(",").concat(Integer.toString(Joy_CNT)).concat(",").concat(Integer.toString(Exclam)).concat(",").concat(Integer.toString(Questions)).concat(",").concat(Integer.toString(Quotations)).concat(",").concat(Integer.toString(IS_RT)).concat(",").concat(Integer.toString(Caps)).concat(",").concat(Integer.toString(Smalls)).concat(",").concat(Integer.toString(Emotion));
    return dump;
}
以及创建PrintWriter对象并尝试打印到文件和打印到标准输出的部分:

try {
        PrintWriter out = null;
        try {
            //out = new PrintWriter("/home/mark/Desktop/datamining/tweetfiles/annotatedTweets.csv", "UTF-8");
            out = new PrintWriter("/home/mark/Desktop/datamining/tweetfiles/tweetsafterannotation.txt","UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Testingtwitter.class.getName()).log(Level.SEVERE, null, ex);
        }
        for(int i=0; i<mydata.size(); i++){
            System.out.println(mydata.get(i).DumpData());
            out.println(mydata.get(i).DumpData());
        }
        out.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Testingtwitter.class.getName()).log(Level.SEVERE, null, ex);
    }
试试看{
PrintWriter out=null;
试一试{
//out=新的PrintWriter(“/home/mark/Desktop/datamining/tweetfiles/annotatedTweets.csv”,“UTF-8”);
out=新的PrintWriter(“/home/mark/Desktop/datamining/tweetfiles/tweetsafterannotation.txt”,“UTF-8”);
}捕获(不支持DencodingException ex){
Logger.getLogger(Testingtwitter.class.getName()).log(Level.SEVERE,null,ex);
}

for(int i=0;iNot与您的问题直接相关,但不使用
.String.concat()
,您应该使用
StringBuilder
@fge,因为我想怀疑将这两个首个long转换为字符串时有问题,我会按您的方式尝试。谢谢!@fge您的建议奏效了!非常感谢,您不知道我已经排除故障多久了!呃?您的意思是使用
StringBuilder
,而不是f
String
.concat()
?是的,我的意思正是这样。我不知道它为什么起作用,但它确实起作用了,这就是我现在感兴趣的全部:D