Java 以 ANSI 格式读取和写入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18556104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read and Write Text in ANSI format
提问by Lemon Juice
Please have a look at the following code
请看下面的代码
import java.io.*;
public class CSVConverter
{
private File csvFile;
private BufferedReader reader;
private StringBuffer strBuffer;
private BufferedWriter writer;
int startNumber = 0;
private String strString[];
public CSVConverter(String location, int startNumber)
{
csvFile = new File(location);
strBuffer = new StringBuffer("");
this.startNumber = startNumber;
//Read
try
{
reader = new BufferedReader(new FileReader(csvFile));
String line = "";
while((line=reader.readLine())!=null)
{
String[] array = line.split(",");
String inputQuery = "insertQuery["+startNumber+"] = \"insert into WordList_Table ('Engl','Port','EnglishH','PortugueseH','Numbe','NumberOf','NumberOfTime','NumberOfTimesPor')values('"+array[0]+"','"+array[2]+"','"+array[1]+"','"+array[3]+"',0,0,0,0)\"";
strBuffer.append(inputQuery+";"+"\r\n");
startNumber++;
}
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(strBuffer.toString());
//Write
try
{
File file = new File("C:/Users/list.txt");
FileWriter filewrite = new FileWriter(file);
if(!file.exists())
{
file.createNewFile();
}
writer = new BufferedWriter(filewrite);
writer.write(strBuffer.toString());
writer.flush();
writer.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[]args)
{
new CSVConverter("C:/Users/list.csv",90);
}
}
I am trying to read a CSV file, edit the text in code, and write it back to a .txtfile. My issue is, I have Portuguese words, so the file should be read and write using ANSIformat. Right now some Portuguese words are replaced with symbols in the output file.
我正在尝试读取 CSV 文件,编辑代码中的文本,然后将其写回.txt文件。我的问题是,我有葡萄牙语单词,所以应该使用ANSI格式读取和写入文件。现在一些葡萄牙语单词被输出文件中的符号替换。
How can I read and write text data into a file in ANSI format in Java?
如何在 Java 中以 ANSI 格式读取和写入文本数据?
回答by vanje
To read a text file with a specific encoding you can use a FileInputStreamin conjunction with a InputStreamReader. The right Java encoding for Windows ANSI is Cp1252.
要读取具有特定编码的文本文件,您可以将 aFileInputStream与InputStreamReader. Windows ANSI 的正确 Java 编码是Cp1252.
reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), "Cp1252"));
To write a text file with a specific character encoding you can use a FileOutputStreamtogether with a OutputStreamWriter.
要编写具有特定字符编码的文本文件,您可以FileOutputStream将OutputStreamWriter.
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "Cp1252"));
The classes InputStreamReaderand OutputStreamWritertranslate between byte oriented streams and text with a specific character encoding.
面向字节的流和具有特定字符编码的文本之间的类InputStreamReader和OutputStreamWriter转换。

