Java by API/java.io/OutputStreamWriter

Материал из Java эксперт
Версия от 14:32, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

new OutputStreamWriter(OutputStream out, String charsetName)

  
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;
public class MainClass {
  public static void Converter(String input, String output) {
    try {
      FileInputStream fis = new FileInputStream(new File(input));
      BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));
      FileOutputStream fos = new FileOutputStream(new File(output));
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
      int len = 80;
      char buf[] = new char[len];
      int numRead;
      while ((numRead = in.read(buf, 0, len)) != -1)
        out.write(buf, 0, numRead);
      out.close();
      in.close();
    } catch (IOException e) {
      System.out.println("An I/O Exception Occurred: " + e);
    }
  }
  public static void main(String args[]) {
    MainClass.Converter(args[0], args[1]);
  }
}





OutputStreamWriter: close()

  
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
  public static void main(String[] args) {
    try {
      char[] chars = new char[2];
      chars[0] = "\u4F60";
      chars[1] = "\u597D";
      String encoding = "GB18030";
      File textFile = new File("C:\\temp\\myFile.txt");
      OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(textFile), encoding);
      writer.write(chars);
      writer.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}





OutputStreamWriter: write(char[] cbuf)

  
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
  public static void main(String[] args) {
    try {
      char[] chars = new char[2];
      chars[0] = "\u4F60";
      chars[1] = "\u597D";
      String encoding = "GB18030";
      File textFile = new File("C:\\temp\\myFile.txt");
      OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(textFile), encoding);
      writer.write(chars);
      writer.close();
    } catch (IOException e) {
      System.out.println(e.toString());
    }
  }
}





OutputStreamWriter: write(char[] cbuf, int off, int len)

  
import java.io.FileWriter;
import java.io.IOException;
public class Main {
  public static void main(String args[]) throws IOException {
    String source = "test";
    char buffer[] = new char[source.length()];
    source.getChars(0, source.length(), buffer, 0);
    FileWriter f0 = new FileWriter("file1.txt");
    for (int i = 0; i < buffer.length; i += 2) {
      f0.write(buffer[i]);
    }
    f0.close();
    FileWriter f1 = new FileWriter("file2.txt");
    f1.write(buffer);
    f1.close();
    FileWriter f2 = new FileWriter("file3.txt");
    f2.write(buffer, buffer.length - buffer.length / 4, buffer.length / 4);
    f2.close();
  }
}