package IOcontrolCeshi;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class ZijieLiuToZifuLiu {
/**
* 输入输出流中的字节流转化成字符流
* 使用方法 InputStreamReader 与 OutputStreamWriter
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个文件作为存储和读取的载体
File file = new File("ByteToChar.txt"); //使用相对路径默认路径
if (!file.exists()) {
try {
System.out.println("开始创建文件ByteToChar.txt");
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (file.exists()) {
System.out.println("文件创建成功!");
}
}else {
System.out.println("文件已经存在!");
}
System.out.println("文件的路径为"+file.getAbsolutePath());
//从控制台输入数字选择两个功能: 1.写入文件 2.读取文件
System.out.println("请输入你要操作的序号: 1.写入文件 2.读取文件");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
//用文件输出流写入信息数据 用OutputStreamWriter将输出字节流转换成输出字符流,并使用方法指定格式:GBK UTF-8 gb2312 等格式
case 1://使用输出流写入文件
//声明需要用到的几个输出流
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//输出缓存字节流 转化成 输出缓存字符流
try {
outputStreamWriter = new OutputStreamWriter(bufferedOutputStream, "GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bufferedWriter = new BufferedWriter(outputStreamWriter);
System.out.println("请输入你要录入的信息:");
String inString = scanner.next();
try {
bufferedWriter.write(inString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("录入信息成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//最后声明的流最先关闭 依次关闭所有的流
if (bufferedWriter!=null) {
try {
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (outputStreamWriter!=null) {
try {
outputStreamWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bufferedOutputStream!=null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (fileOutputStream!=null) {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
break;
//用文件输入流读取信息数据 用InputStreamReader将输入字节流转换成输入字符流,并使用方法指定格式:GBK UTF-8 gb2312 等格式 进行读取
case 2://读取文件
//声明几个要用的输入流
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
System.out.println("开始读取文件<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
//使用InputStreamReader 方法 将输入字节流 转换成 输入字符流
try {
inputStreamReader = new InputStreamReader(bufferedInputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bufferedReader = new BufferedReader(inputStreamReader);
char[] out = new char[1024];
int len = 0;
try {
while ((len = bufferedReader.read(out))!=-1) {
System.out.println("你要读取的信息为:"+new String(out, 0, len));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//最后声明的流最先关闭 依次关闭所有的流
if (bufferedReader!=null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (inputStreamReader!=null) {
try {
inputStreamReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bufferedInputStream!=null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (fileInputStream!=null) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
break;
default:
System.out.println("你的输入有误,请重新输入!");
break;
}
}
}