gpt4 book ai didi

java BufferedReader 到 BufferedWriter 生成空文件

转载 作者:行者123 更新时间:2023-12-01 19:54:54 25 4
gpt4 key购买 nike

我正在尝试将目录中的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");
}
}
}
}

最佳答案

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

关于java BufferedReader 到 BufferedWriter 生成空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49932803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com