转换流
由于字节流操作中文不是特别方便,所以java中提供了转换流
编码表:由现实世界的字符和对应的数值组成的一张表
编码:把看得懂的变成看不懂的(String-------byte[])
解码:把看不懂的变成看得懂的(byte[]-------String)
- String-----bytes[]
import java.util.Arrays;
import java.io.UnsupportedEncodingException;
public class StringDemo{
public static void main(String[] args)throws UnsupportedEncodingException{
String s = "你好";
//String-----bytes[]
/*
byte[] bys = s.getBytes();//将字符串转换成字节数组
System.out.println(Arrays.toString(bys));//输出结果:[-28, -67, -96, -27, -91, -67]
*/
/*
byte[] bys = s.getBytes("GBK");//指定编码方式
System.out.println(Arrays.toString(bys));//输出结果:[-60, -29, -70, -61]
*/
byte[] bys = s.getBytes("utf-8");
System.out.println(Arrays.toString(bys));//输出结果:[-28, -67, -96, -27, -91, -67]
}
}
- bytes[]-----String
import java.io.UnsupportedEncodingException;
public class StringDemo{
public static void main(String[] args)throws UnsupportedEncodingException{
String s = "你好";
// byte[] bys = s.getBytes();//默认是utf-8编码
byte[] bys = s.getBytes("utf-8");
//byte[] ---Sttring
// String ss = new String(bys);//utf-8编码的,utf-8解码的
// System.out.println(ss);//转换成字符串;输出结果是你好
String ss = new String(bys,"GBK");//utf-8编的,GBK解码的
System.out.println(ss);//输出结果是:浣犲ソ
}
}
OutputStreamWriter
OutputStreamWriter(OutputStream out) :使用默认编码把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out, String charseName):根据指定编码把字节流数据转换成字符流
OutputStreamWriter(或FileWriter)写数据
public void write(int c):写一个字符
public void write(char[] cbuf):写一个字符数组
public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
public void write(String str):写一个字符串
public void write(String str,int off,int len):写一个字符串的一部分
代码练习
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
public class OutputStreamWriteDemo{
public static void main(String[] args)throws IOException{
//创建对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("/home/hadoop/a.txt"));
/*
//写数据
//public void write(int c):写一个字符
osw.write("
a
");
osw.write(87);
*/
//public void write(char[] cbuf):写一个字符数组
char[] chs = {'a','b','c','d'};//不能使用双引号?
// osw.write(chs);
//public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
// osw.write(chs,1,3);
//public void write(String str):
// osw.write("自学java");
//public void write(String str,int off, int len):写一个字符串的一部分
osw.write("邬家栋自学java",3,6);
osw.flush();
osw.close();
}
}
InputStreamReader
InputStreamReader(InputStream in) :用默认的编码读取数据
InputStreamReader(InputStream in, String charsetName) :用指定的编码读取数据
InputStreamReader(或FileReader)读取数据
int read():一次读取一个字符
int read(char[] chs):一次读取一个字符数组
代码练习
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
public class InputStreamReadDemo{
public static void main(String[] args)throws IOException{
//创建对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("/home/hadoop/javatest/IO/OutputStreamWriteDemo.java"));
/*
int ch = 0;
while((ch = isr.read())!= -1){
System.out.print((char)ch);
}
*/
//一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while((len = isr.read(chs)) != -1){
System.out.print(new String(chs,0,len));
}
isr.close();
}
}
字符流读写数据(复制文本文件)
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileDemo{
public static void main(String[] args)throws IOException{
//封装数据源
InputStreamReader isr = new InputStreamReader(new FileInputStream("/home/hadoop/a.txt"));
//封装目的地
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("/home/hadoop/b.txt"));
/*
//方式一
int ch = 0;
while((ch = isr.read()) != -1){
osw.write(ch);
}
*/
//方式二
char[] chs = new char[1024];
int len = 0;
while((len = isr.read(chs)) != -1){
osw.write(chs,0,len);
osw.flush();
}
isr.close();
osw.close();
}
}
字符缓冲流BufferedWriter写数据
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class BufferedWriterDemo{
public static void main(String[] args)throws IOException{
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("bw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
bw.write("hello
");
bw.write("wujiadong
");
bw.close();
}
}
字符缓冲流BufferedReader读数据
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderDemo{
public static void main(String[] args)throws IOException{
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
/*
//方式1
int ch = 0;
while((ch = br.read()) != -1){
System.out.print((char) ch);
}
*/
//方式2
char[] chs = new char[1024];
int len = 0;
while((len = br.read(chs)) != -1){
System.out.print(new String(chs,0,len));
}
br.close();
}
}
字符缓存流复制文件
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyFileDemo{
public static void main(String[] args)throws IOException{
//
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
//方式1
//方式2
char[] chs = new char[1024];
int len = 0;
while((len = br.read(chs)) != -1){
bw.write(chs,0,len);
}
br.close();
bw.close();
}
}
特殊功能(一次读一行)
- readLine(): 无换行符
- newline(): 换行
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
public class BufferedDemo{
public static void main(String[] args)throws IOException{
// write();
read();
}
public static void read()throws IOException{
BufferedReader br = new BufferedReader(new FileReader("bw2.txt"));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
}
public static void write() throws IOException{
BufferedWriter bw = new BufferedWriter (new FileWriter("bw2.txt"));
for(int i=0;i<10;i++){
bw.write("wujiadong");
bw.newLine(); //换行
bw.flush();
}
bw.close();
}
}
复制文件
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
public class CopyFileDemo1{
public static void main(String[] args)throws IOException{
//封装数据源
BufferedReader br = new BufferedReader(new FileReader("CopyFileDemo.java"));
//封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("bw3.txt"));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
练习1:复制文本文件
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
public class CopyFileDemo{
public static void main(String[] args)throws IOException{
String srcString = "bw3.txt";
String desString = "wu.txt";
// method1(srcString,desString);
// method2(srcString,desString);
// method3(srcString,desString);
// method4(srcString,desString);
method5(srcString,desString);
}
//字符缓存流一次读写一个字符串
private static void method5(String srcString,String desString)throws IOException{
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(desString));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
//字符缓存流一次读写一个字符数组
private static void method4(String srcString,String desString)throws IOException{
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(desString));
char[] ch = new char[1024];
int len = 0;
while((len = br.read(ch)) != -1){
bw.write(ch);
}
br.close();
bw.close();
}
//字符缓冲流一次读写一个字符
private static void method3(String srcString,String desString)throws IOException{
BufferedReader fr = new BufferedReader(new FileReader(srcString));
BufferedWriter fw = new BufferedWriter(new FileWriter(desString));
int ch = 0;
while((ch = fr.read()) != -1){
fw.write(ch);
}
fr.close();
fw.close();
}
//基本字符流一次读取一个字符数组
private static void method2(String srcString,String desString)throws IOException{
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(desString);
char[] ch = new char[1024];
int len = 0;
while((len = fr.read(ch)) != -1){
fw.write(ch, 0 ,len);
}
fr.close();
fw.close();
}
//基本字符流一次读写一个字符
private static void method1(String srcString,String desString)throws IOException{
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(desString);
int ch = 0;
while((ch = fr.read()) != -1){
fw.write(ch);
}
fr.close();
fw.close();
}
}
练习2:复制非文本文件的4种方法
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class CopyImageDemo{
public static void main(String[] args)throws IOException{
//使用字符串作为路径
// String srcFile = "1.jpg";
// String desFile = "/home/hadoop/javatest/1.jpg";
//使用File对象作为参数
File srcFile = new File("/home/hadoop/1.jpg");
File desFile = new File("/home/hadoop/javatest/1.jpg");
// method1(srcFile,desFile);
// method2(srcFile,desFile);
// method3(srcFile,desFile);
method4(srcFile,desFile);
}
//基本字节流一次读取一个字节
public static void method1(File srcFile,File desFile)throws IOException{
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(desFile);
int by = 0;
while((by = fis.read()) != -1){
fos.write(by);
}
fis.close();
fos.close();
}
//基本字节流一次读写一个字节数组
public static void method2(File srcFile,File desFile)throws IOException{
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(desFile);
byte[] by = new byte[1024];
int len = 0;
while((len = fis.read(by)) != -1){
fos.write(len);
fos.flush();
}
fis.close();
fos.close();
}
public static void method3(File srcFile,File desFile)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));
int by = 0;
while((by = bis.read()) != -1){
bos.write(by);
}
bis.close();
bos.close();
}
//字节缓冲流一次读取一个字节数组
public static void method4(File srcFile,File desFile)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));
byte[] by = new byte[1024];
int len = 0;
while((len = bis.read()) != -1){
bos.write(by);
bos.flush();
}
bis.close();
bos.close();
}
}
练习3:将集合中的数据存储到文本文件
/*
* 需求:将ArrayList集合中的字符串数据存储到文本文件
*
*/
import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileWriter;
public class ArrayListToFileDemo{
public static void main(String[] args)throws IOException{
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
array.add("java");
array.add("python");
array.add("scala");
//封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("/home/hadoop/wu.txt"));
//遍历集合
for(String s:array){
bw.write(s);
bw.newLine();
}
bw.close();
}
}
练习4:把文本文件的数据存储到集合
/*
* 需求:从文本文件中读取数据(一行为一个字符串数据)到集合中,并遍历集合
*
*
*/
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileToArrayListDemo{
public static void main(String[] args)throws IOException{
//封装数据源
BufferedReader br = new BufferedReader(new FileReader("/home/hadoop/wu.txt"));
//封装目的地
ArrayList<String> array = new ArrayList<String>();
//读取数据存储到集合中
String line = null;
while((line = br.readLine()) != null){
array.add(line);
}
br.close();
//遍历集合
for(String s:array){
System.out.println(s);
}
}
}
练习5:
/*
* 需求:有一个文本文件存储了几个名称,写一个程序实现随机获取一个人的名字
*
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class GetName{
public static void main(String[] args)throws IOException{
//把文本文件的数据存储到集合中
BufferedReader br = new BufferedReader(new FileReader("/home/hadoop/wu.txt"));
ArrayList<String> array = new ArrayList<String>();
String line = null;
while((line = br.readLine()) != null){
array.add(line);
}
br.close();
//产生一个随机索引
Random r = new Random();
int index = r.nextInt(array.size());
//根据该索引获取一个值
String name = array.get(index);
System.out.println("幸运者是:"+name);
}
}
练习6:复制单级文件夹
import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyFolderDemo{
public static void main(String[] args)throws IOException{
//封装目录
File srcFolder = new File("/home/hadoop/javatest/IO");
File desFolder = new File("/home/hadoop/wu_test");
//如果目的地文件夹不存在就创建
if(!desFolder.exists()){
desFolder.mkdir();
}
//获取该目录下的所有文本的File数组
File[] fileArray = srcFolder.listFiles();
//遍历该File数组,得到么一个File对象
for(File file:fileArray){
//System.out.println(file);
//数据源:/home/hadoop/javatest
//目的地:/home/hadoop/wu_test
String name = file.getName();
File newFile = new File(desFolder,name);
copyFile(file,newFile);
}
}
private static void copyFile(File file,File newFile)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
byte[] by = new byte[1024];
int len = 0;
while((len = bis.read(by)) != -1){
bos.write(by,0,len);
}
bos.close();
bis.close();
}
}