package com.lwh.j2se.io;
import java.io.*;
import java.util.Scanner;
public class TestFileIO {
public static void main(String[] args) {
File file = new File("D:\\meberList.txt");
try {
OutputStream os = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(os, "GBK");
PrintWriter pw = new PrintWriter(osw);
pw.println("蚂蚁大战大象");
pw.println("蚂蚁赢了");
pw.close();
FileReader fr = new FileReader(file);
Scanner sc = new Scanner(fr);
String data = null;
while(sc.hasNextLine()){
data = sc.nextLine();
System.out.println(data);
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}
测试通过,没有出现乱码
import java.io.*;
import java.util.Scanner;
public class TestFileIO {
public static void main(String[] args) {
File file = new File("D:\\meberList.txt");
try {
OutputStream os = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(os, "GBK");
PrintWriter pw = new PrintWriter(osw);
pw.println("蚂蚁大战大象");
pw.println("蚂蚁赢了");
pw.close();
FileReader fr = new FileReader(file);
Scanner sc = new Scanner(fr);
String data = null;
while(sc.hasNextLine()){
data = sc.nextLine();
System.out.println(data);
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}
测试通过,没有出现乱码