1. Transformation flows
- InputStreamReader
- OutputStreamWriter
2. InputStreamReader class
Transformation flows Java. IO. InputStreamReader is a subclass of Reader, from byte stream to flow characters of Bridges. It reads bytes and decodes them into characters using the specified character set. Its character set can be specified by name or it can accept the platform’s default character set.
A constructor
InputStreamReader(InputStream in)
: Creates a character stream that uses the default character set.InputStreamReader(InputStream in, String charsetName)
: Creates a stream of characters from the specified character set.
3. The OutputStreamWriter class
Transformation flows Java. IO. OutputStreamWriter, is a subclass of Writer, from the characters flow to byte streams. Encodes characters into bytes using the specified character set. Its character set can be specified by name or it can accept the platform’s default character set. [Access to information]
A constructor
OutputStreamWriter(OutputStream in)
: Creates a character stream that uses the default character set.OutputStreamWriter(OutputStream in, String charsetName)
: Creates a stream of characters from the specified character set.
4. Read the file
InputStreamReader isr = new InputStreamReader(new FileInputStream(” a.putXt “),”UTF-8”);
package com.se.file;
import java.io.*;
public class Demo06_Fis_The code {
public static void main(String[] args) {
BufferedReader bfr = null;
try{add Java development exchange jun sample:484138291InputStreamReader ISr =new InputStreamReader(new FileInputStream("F:\\Demo1\\w.txt"), "UTF-8");
bfr = new BufferedReader(isr);
String str = "";
while ((str = bfr.readLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bfr ! = null) bfr.close(); }catch(IOException e) { e.printStackTrace(); }}}}Copy the code
5. Write the file
Set the read encoding to UTF-8 using the transformation stream
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(“F:\Demo1\a.txt”), “UTF-8”);
package com.se.file;
import java.io.*;
public class Demo06_Fis_The code {
public static void main(String[] args) {
BufferedWriter bfw = null;
try{add Java development exchange jun sample:484138291OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream("F:\\Demo1\\a.txt"), "UTF-8");
bfw = new BufferedWriter(osw);
bfw.write("Hello");
bfw.write("\r\n");
bfw.write("abc");
bfw.write("\r\n");
bfw.flush();
bfw.write("Ha ha ha.");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bfw ! = null) bfw.close(); }catch(IOException e) { e.printStackTrace(); }}}}Copy the code
Classification: Java
The latest 2021 collation collection of a lot of dry goods, including mysql, Netty, Spring, threads, Spring Cloud, JVM, source code, algorithms and other detailed explanation. [Access to information]