格式读取和写入文本

格式读取和写入文本

本文介绍了以 ANSI 格式读取和写入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码

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+";"+"
");
             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);
    }
}

我正在尝试读取 CSV 文件,编辑代码中的文本,然后将其写回 .txt 文件.我的问题是,我有葡萄牙语单词,所以应该使用 ANSI 格式读取和写入文件.现在一些葡萄牙语单词在输出文件中被符号替换.

I am trying to read a CSV file, edit the text in code, and write it back to a .txt file. My issue is, I have Portuguese words, so the file should be read and write using ANSI format. Right now some Portuguese words are replaced with symbols in the output file.

如何在 Java 中读取和写入 ANSI 格式的文本数据?

How can I read and write text data into a file in ANSI format in Java?

推荐答案

要读取具有特定编码的文本文件,您可以将 FileInputStreamInputStreamReader 结合使用.Windows ANSI 的正确 Java 编码是 Cp1252.

To read a text file with a specific encoding you can use a FileInputStream in conjunction with a InputStreamReader. The right Java encoding for Windows ANSI is Cp1252.

reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), "Cp1252"));

要编写具有特定字符编码的文本文件,您可以将 FileOutputStreamOutputStreamWriter 一起使用.

To write a text file with a specific character encoding you can use a FileOutputStream together with a OutputStreamWriter.

writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "Cp1252"));

InputStreamReaderOutputStreamWriter 类在面向字节的流和具有特定字符编码的文本之间进行转换.

The classes InputStreamReader and OutputStreamWriter translate between byte oriented streams and text with a specific character encoding.

这篇关于以 ANSI 格式读取和写入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:50