(20160111)
【扣丁学堂】
Test13 缓存流
package practice06;
//缓存流
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class Test13 {
public static void charStreamOut(){
try {
Writer out=new FileWriter("f:\\a.txt");
BufferedWriter bw=new BufferedWriter(out);
bw.write("让编程更简单");
bw.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void charStreamIn(){
try {
Reader in =new FileReader("f:\\a.txt");
BufferedReader br=new BufferedReader(in);
String info=br.readLine();
System.out.println(info);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void byteStreamOut(){
try {
OutputStream out=new FileOutputStream("f:\\a.txt");
//让字节输出流具备缓冲功能
BufferedOutputStream bos=new BufferedOutputStream(out);
bos.write("让编程更简单".getBytes());
bos.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void byteStreamIn(){
try {
InputStream in =new FileInputStream("f:\\a.txt");
BufferedInputStream bis=new BufferedInputStream(in);
byte[]bytes=new byte[1024];
int len=-1;
StringBuffer buf=new StringBuffer();
while((len=bis.read(bytes))!=-1){
buf.append(new String(bytes,0,len));
}
bis.close();
in.close();
System.out.println(buf);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// byteStreamIn();
// byteStreamOut();
charStreamIn();
charStreamOut();
}
}
Test14 打印流
package practice06;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
//打印流
public class Test14 {
public static void printStream(){
//
}
public static void charStreamOut(){
try {
Writer out=new FileWriter("f:\\a.txt");
BufferedWriter bw=new BufferedWriter(out);
PrintWriter pw=new PrintWriter(bw);
pw.println(97);
pw.println("abc");
pw.println(true);
// bw.write("让编程更简单");
bw.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void byteStreamOut(){
try {
OutputStream out=new FileOutputStream("f:\\a.txt");
//让字节输出流具备缓冲功能
BufferedOutputStream bos=new BufferedOutputStream(out);
PrintStream ps=new PrintStream(bos);
// bos.write("让编程更简单".getBytes());
ps.println(123);
ps.println("abc");
ps.println(true);
bos.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
byteStreamOut();
charStreamOut();
}
}
Test15 字节数组流 数据流 字符串流
package practice06;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import org.omg.CORBA.PUBLIC_MEMBER;
public class Test15 {
//字节数组流,没有操作文件(了解)
public static void byteArraysStream(){
String info="让编程更简单";
ByteArrayInputStream bis=new ByteArrayInputStream(info.getBytes());
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int i=-1;
while((i=bis.read())!=-1){
bos.write(i);
}
System.out.println(bos.toString());
}
//数据流(掌握)
public static void DataOut() {
try {
OutputStream out = new FileOutputStream("f:\\a.txt");
DataOutputStream dos=new DataOutputStream(out);
dos.writeInt(10);//写入4个字节
dos.writeUTF("学堂");
dos.writeLong(1232434556);
dos.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void DataIn(){
try {
InputStream in = new FileInputStream("f:\\a.txt");
DataInputStream dis=new DataInputStream(in);
int a=dis.readInt();
String s=dis.readUTF();
long b=dis.readLong();
System.out.println(a+s+b);
dis.close();
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//字符串流StringReader StringWriter
public static void stringStream(){
String info="让编程更简单";
StringReader reader=new StringReader(info);
}
public static void main(String[] args) {
// byteArraysStream();
DataOut();
DataIn();
}
}
Test16 RandomAccessFile
package practice06;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test16 {
public static void main(String[] args) {
try {
File file=new File("f:\\a.txt");
RandomAccessFile raf=new RandomAccessFile(file, "rw");
raf.writeInt(18);
raf.writeUTF("小弱");
raf.writeBoolean(false);
raf.writeChar('女');
raf.seek(0);//定位到文件的开始位置
int age=raf.readInt();
String name=raf.readUTF();
// raf.seek(1);
raf.skipBytes(1);//跳过一个字节
char sex=raf.readChar();
System.out.println("age="+age+" name="+name+" sex="+sex);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Test17 字体转换
package practice06;
import java.io.UnsupportedEncodingException;
public class Test17 {
public static void main(String[] args) {
String name = "让编程更简单";
try {
String name1=new String(name.getBytes("GBK"),"iso-8859-1");
System.out.println(name1);
String name2=new String(name1.getBytes("iso-8859-1"),"GBK");
System.out.println(name2);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}