java BufferedReader到BufferedWriter生成空文件

时间:2018-04-20 02:03:18

标签: java character-encoding

我正在尝试将ansii(latin-5)文本文件转换为目录中的utf-8文本文件。我做了一个小机制来理解文件是ansii还是utf-8然而当我尝试将ansii文件更改为utf-8程序时删除文本中的所有值。我在哪里做错了?

提前致谢。

这是我的代码:

package altyazi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;

public class operation{
    public static int howmany =0;
    public static int howmanysmalli=0;
    public static double ratio;
    File myFile;
    public static void koddegıstır(String myfile) throws IOException{
        File file = new File(myfile);
        byte[] bytesArray = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(bytesArray);
        fis.close();
        int[] freqs = new int[256];
          for(byte b: bytesArray){
              freqs[b&0x0ff]++;
          }
          howmany = freqs[107]+freqs[75];
          howmanysmalli=freqs[253];
          System.out.println("Character \"k\" appears " + howmany +" times in the text "+myfile);
          ratio = (double)howmany/(double)bytesArray.length;
          System.out.println("How many: "+howmany);
          System.out.println("Length: "+bytesArray.length);
          System.out.println("Ratio: "+ratio);
          //Cp1254
          if(ratio<0.01){
              System.out.println("Text file is probably not turkish");
          }else{
              System.out.println("Text file is probably turkish");
              if(howmanysmalli>20){
                  String line;
                  BufferedReader br = new BufferedReader(new InputStreamReader(
                            new FileInputStream(myfile),
                            "ISO-8859-9"));
                  Writer out = new BufferedWriter(
                            new OutputStreamWriter(new FileOutputStream(
                                    myfile), "UTF-8"));
                  try {     
                        while ((line = br.readLine()) != null) {

                            out.write(line);
                            out.write("\n");

                        }

                    } finally {

                        br.close();
                        out.close();

                    }
              }else{
                  System.out.println("Passed as utf-8");
              }
          }
    }
}

1 个答案:

答案 0 :(得分:3)

创建FileOutputStream时,您将覆盖该文件。这会创建一个空文件。您需要写入 new 文件,并删除旧文件并在完成后重命名新文件。