更改编码时出错

时间:2015-03-21 17:32:27

标签: java encoding coding-style

我必须将编码站点从文件(txt)更改为另一个,我在该行中遇到意外故障:

out.write(线);

以下是我可以注意到的语句:令牌“line”上的语法错误,此令牌后面的VariableDeclaratorId

以下是整个代码:

package snippet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class Snippet {

     FileInputStream fis = new FileInputStream("C:/Users/Michal/Desktop/1.txt");

        InputStreamReader isr = new InputStreamReader(fis, "iso8859-2");

        BufferedReader in = new BufferedReader(isr);

        FileOutputStream fos = new FileOutputStream(("C:/Users/Michal/Desktop/2.txt"));
        OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
        BufferedWriter out = new BufferedWriter(osw);

        String line = in.readLine();
        out.write(line);
}

请告诉我,我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要将代码放在main方法中运行。同时在main方法中拖出IOException并关闭BufferedWriter以保存文件数据。

  public class Snippet {

    public static void main(String... arg) throws IOException{

      FileInputStream fis = new FileInputStream("C:/Users/Michal/Desktop/1.txt");
      InputStreamReader isr = new InputStreamReader(fis, "iso8859-2");
      BufferedReader in = new BufferedReader(isr);
      FileOutputStream fos = new FileOutputStream(("C:/Users/Michal/Desktop/2.txt"));
      OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
      BufferedWriter out = new BufferedWriter(osw);
      String line = in.readLine();
      out.write(line);
      out.close();// Close BufferedWriter
   }
 }