BufferedWriter将String转换为txt文件中的符号

时间:2016-03-29 10:20:37

标签: java bufferedreader bufferedwriter

我想在我的txt中写一些字符串,但总是在txt文件中写一个这样的符号:

  

这是编写txt文件的代码:

public static void writeToTxtTargetData(String cry) throws Exception {
    String target = "0";

    //check the label for target
    if(cry.equals("Neh")){
        target = "0";
    } else if(cry.equals("Owh")){
        target = "1";
    } else if(cry.equals("Eh")){
        target = "2";
    } else if(cry.equals("Eairh")){
        target = "3";
    } else if(cry.equals("Heh")){
        target = "4";
    } 

    //System.out.println(target);
    //write target to .txt
    try {
        File file = new File("C:/Users/test/Desktop/test/initialTarget.txt");
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(target + "\n");
        bw.close();
        System.out.println("Done");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

甚至当我手动将文件修复为正常数字(如'0')时,我使用BufferedReader读取它,它总是将其读作这样的符号:

  

ÿþ0

这是我用来读取文件的代码:

private void DefineCluster() throws NumberFormatException, IOException{
    BufferedReader in = new BufferedReader(new FileReader("C:/Users/test/Desktop/test/initialTarget.txt"));
    target = new ArrayList<Integer>();
    String line = "";
    while((line = in.readLine()) != null) {
        target.add(Integer.parseInt(line));
    }
    in.close(); 
}

0 个答案:

没有答案