Vector 的方法都是synchronized的
Arraylist 快 不安全
I/O
输入/输出 方向
字节/字符 单位
FileInputStream 读文件
Read()返回及一个整数,表示的你读出的字节的值,
当读了返回-1
Read(int b[])读满一个字节数组
fileOutputStream 写文件
import java.io.*;
public class TestOutputStream {
public static void main(String[] args){
OutputStream os=null;
try {
os=new FileOutputStream("2.txt");
String str="Hello Kettas";
byte[] bs=str.getBytes();//将字符串数组转换成字节数组
os.write(bs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (os!=null) os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.*;
public class TestListFiles {
public static void main(String[] args) {
File dir=new File("D:\\0804corejava");
print(dir);
}
static void print(File dir){
File[] fs=dir.listFiles();
for(int i=0;i<fs.length;i++){
if (fs[i].isFile()) //判断是否是一个文件
System.out.println(fs[i].getAbsolutePath());
else print(fs[i]);
}
}
}
import java.io.*;
public class TestPipedStream {
public static void main(String[] args) throws Exception{
PipedInputStream fis=new PipedInputStream();
PipedOutputStream fos=new PipedOutputStream();
fis.connect(fos);//连接
Thread t=new ThreadA(fos);
t.start();
System.out.println(fis.read());
fis.close();
}
}
class ThreadA extends Thread{
PipedOutputStream pos;
public ThreadA(PipedOutputStream pos) {
super();
this.pos = pos;
}
public void run(){
int result=0;
try {
for(int i=1;i<=10;i++){
result+=i;
Thread.sleep(500);
}
pos.write(result);
pos.close();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class TestDataStream {
public static void main(String[] args) throws Exception{
long l=1234567890123456L;
FileOutputStream fos=new FileOutputStream("1.dat");
DataOutputStream out=new DataOutputStream(fos);
out.writeLong(l);
out.close();
FileInputStream fis=new FileInputStream("1.dat");
DataInputStream in=new DataInputStream(fis);
long l2=in.readLong();
in.close();关闭外层就可以
System.out.println(l2);
}
}
import java.io.*;
public class TestBufferedStream {
public static void main(String[] args) throws Exception{
String str="Hello Kettas";
byte[] bs=str.getBytes();
FileOutputStream fos=new FileOutputStream("kettas.txt");
BufferedOutputStream out=new BufferedOutputStream(fos);
//DataOutputStream dout=new DataOutputStream(out);
out.write(bs);
out.flush();//刷新输出流
}
}
原子操作 synchronzined
ObjectOutputStream 能读八种基本的数据,带在缓冲。
序列化必须,Serializable
Transient 修辞的属性是临时属性,不参加序列化。
import java.io.*;
public class TestObjectStream {
public static void main(String[] args) throws Exception{
Student s1=new Student("Liucy",30,80);
Student s2=new Student("Huxz",18,100);
FileOutputStream fos=new FileOutputStream("students.dat");
ObjectOutputStream out=new ObjectOutputStream(fos);
out.writeObject(s1);
out.writeObject(s2);
out.close();
FileInputStream fis=new FileInputStream("students.dat");
ObjectInputStream in=new ObjectInputStream(fis);
Object o1=in.readObject();
Object o2=in.readObject();
in.close();
System.out.println(o1);
System.out.println(o2);
}
}
实现Serializable,序列化
class Student implements Serializable{
String name;
int age;
transient double score;//临时属性
public Student(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public String toString(){
return "Student name="+name+" age="+age+" score="+score;
}
}
import java.io.*;
import java.util.*;
public class TestPoemInverse {
public static void main(String[] args) throws Exception{
Stack s=new Stack();
FileInputStream fis=new FileInputStream("1.txt");
InputStreamReader ir=new InputStreamReader(fis);// 创建一个使用默认字符集的InputStreamReader
BufferedReader in=new BufferedReader(ir);
String str;
//readLine()读取一个文本行。
while((str=in.readLine())!=null){
s.push(str);
}
in.close();
FileOutputStream fos=new FileOutputStream("2.txt");
PrintWriter out=new PrintWriter(fos);
int size=s.size();
for(int i=0;i<size;i++){
str=s.pop();
out.println(str);
}
out.close();
}
}
class Stack {
private LinkedList list=new LinkedList();
public void push(String str){
list.addFirst(str);
}
public String pop(){
return (String)list.removeFirst();
}
public int size(){
return list.size();
}
}
import java.io.*;
public class TestReaderWriter {
public static void main(String[] args) throws Exception{
FileOutputStream fos=new FileOutputStream("poem.txt");
//Writer ow=new OutputStreamWriter(fos,"GBK");
//PrintWriter out=new PrintWriter(ow);
PrintWriter out=new PrintWriter(fos);
out.println("葡萄美酒夜光杯");
out.println("欲饮琵琶马上催");
out.println("醉卧沙场君莫笑");
out.println("我是流氓我怕谁");
out.close();
FileInputStream fis=new FileInputStream("poem.txt");
Reader ir=new InputStreamReader(fis,"Big5");
BufferedReader in=new BufferedReader(ir);
String str;
while((str=in.readLine())!=null){
System.out.println(str);
}
in.close();
}
}
public class TestString {
public static void main(String[] args) throws Exception{
String str="1232131 ";
byte[] bs1=str.getBytes("GBK");//编码
String str2=new String(bs1,"Big5");//解码
System.out.println(str);
System.out.println(str2);
byte[] bs2=str2.getBytes("Big5");
String str3=new String(bs2,"GBK");
System.out.println(str3);
<