package day20150904;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
* 字符流只用于读写文本数据
* 字符流是高级流,底层本质上还是读写字节
*
* Reader,Writer:字符输入输出流的父类
*/
public class OutputStreamWriterDemo1 {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("t.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
osw.write("hello");
osw.write("中华人民共和国");
osw.write("成立了");
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
package day20150904;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class InputStreamReaderDemo2 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("t.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
int d = -1;
while((d=isr.read())!=-1){
char c = (char)d;
System.out.print(c);
}
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package day20150904;
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;
public class FileCopy3 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("."
+File.separator+"src"
+File.separator+"day0831"
+File.separator+"FileCopy3.java");
FileOutputStream fos = new FileOutputStream("copy.java");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
int d = -1;
while((d=isr.read())!=-1){
osw.write(d);
}
isr.close();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
package day20150904;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
/**
* PrintWriter:
* 缓冲字符输出流,带有自动行刷新
* 可以以行为单位写出字符串
*
* BufferedWriter没有PrintWriter用起来爽
*/
public class PrintWriterDemo4 {
public static void main(String[] args) {
try {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("pw.txt"),"utf-8");
/**
* true:自动行刷新:
* 每当使用println方法(不是print)后,都会自动flush
*
*注意:会增加写的次数,降低写出效率
*/
PrintWriter pw = new PrintWriter(osw,true);
pw.println("你好:");
pw.print("hehe");
pw.println("今天");
pw.println("天气不错");
osw.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
package day20150904;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* BufferedReader:缓冲字符输入流
* 可以以行为单位读取字符串
*/
public class BufferedReader5 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("pw.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
BufferedReader br = new BufferedReader(isr);
/**
* String readLine()
* 读取一行,读取到换行符为止
* 如果返回值为null,说明没数据可读了
*/
String str = null;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package day20150904;
import java.io.BufferedReader;
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.PrintWriter;
import java.io.UnsupportedEncodingException;
public class FileReadWriteDemo6 {
/**
* 文件中内容如下
* hello
* 你好
* 今天天气不错
* 要求操作后文件内容如下
* hello
* 你好
* 今天天气不错
* hello你好今天天气不错
*/
public static void main(String[] args){
try {
FileInputStream fis = new FileInputStream("pw.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String str = null;
while((str=br.readLine())!=null){
sb.append(str);
}
br.close();
FileOutputStream fos = new FileOutputStream("pw.txt",true);
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
PrintWriter pw = new PrintWriter(osw);
pw.println(sb);
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}