This object can both read and write files. You can specify the operation mode (r, rw) when instantiating objects)Note: When the object is instantiated, if the file to be operated does not exist, it will be automatically created. If the file exists and the write data is not specified, it will be written from the beginning, that is, overwrite the original content. It can be used to download data in multiple threads or write data to files simultaneously.
========================================================== ========================================================== =============== 1. Stream applicationsimport java.io.*;class StreamTest{public static void main(String[] args) throws Exception{/*int data;while((data=System.in.read())!=-1){System.out.write(data);}*//*FileOutputStream fos=new FileOutputStream("1.txt");//fos.write("http://www.mybole.com.cn".getBytes());//fos.close();BufferedOutputStream bos=new BufferedOutputStream(fos);//bos.write("http://www.mybole.com.cn".getBytes());//bos.flush();//bos.close();DataOutputStream dos=new DataOutputStream(bos);byte b=3;int i=78;char ch='a';float f=4.5f;dos.writeByte(b);dos.writeInt(i);dos.writeChar(ch);dos.writeFloat(f);dos.close();FileInputStream fis=new FileInputStream("1.txt");BufferedInputStream bis=new BufferedInputStream(fis);*//*byte[] buf=new byte[100];//int len=fis.read(buf);int len=bis.read(buf);System.out.println(new String(buf,0,len));//fis.close();bis.close();*//*DataInputStream dis=new DataInputStream(bis);System.out.println(dis.readByte());System.out.println(dis.readInt());System.out.println(dis.readChar());System.out.println(dis.readFloat());dis.close();*//*FileOutputStream fos=new FileOutputStream("1.txt");OutputStreamWriter osw=new OutputStreamWriter(fos);BufferedWriter bw=new BufferedWriter(osw);bw.write("http://www.mybole.com.cn");bw.close();FileInputStream fis=new FileInputStream("1.txt");InputStreamReader isr=new InputStreamReader(fis);BufferedReader br=new BufferedReader(isr);System.out.println(br.readLine());br.close();*/InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);String strLine;while((strLine=br.readLine())!=null){System.out.println(strLine);}br.close();}}
========================================================== ========================================================== ================================= 2. PipedStream applicationsimport java.io.*;class PipedStreamTest{public static void main(String[] args){PipedOutputStream pos=new PipedOutputStream();PipedInputStream pis=new PipedInputStream();try{pos.connect(pis);new Producer(pos).start();new Consumer(pis).start();}catch(Exception e){e.printStackTrace();}}}class Producer extends Thread{private PipedOutputStream pos;public Producer(PipedOutputStream pos){this.pos=pos;}public void run(){try{pos.write("Hello,welcome you!".getBytes());pos.close();}catch(Exception e){e.printStackTrace();}}}class Consumer extends Thread{private PipedInputStream pis;public Consumer(PipedInputStream pis){this.pis=pis;}public void run(){try{byte[] buf=new byte[100];int len=pis.read(buf);System.out.println(new String(buf,0,len));pis.close();}catch(Exception e){e.printStackTrace();}}}
========================================================== ========================================================= 3. ObjectSerialStreamimport java.io.*;class ObjectSerialTest{public static void main(String[] args) throws Exception{Employee e1=new Employee("zhangsan",25,3000.50);Employee e2=new Employee("lisi",24,3200.40);Employee e3=new Employee("wangwu",27,3800.55);FileOutputStream fos=new FileOutputStream("employee.txt");ObjectOutputStream oos=new ObjectOutputStream(fos);oos.writeObject(e1);oos.writeObject(e2);oos.writeObject(e3);oos.close();FileInputStream fis=new FileInputStream("employee.txt");ObjectInputStream ois=new ObjectInputStream(fis);Employee e;for(int i=0;i<3;i++){e=(Employee)ois.readObject();System.out.println(e.name+":"+e.age+":"+e.salary);}ois.close();}}class Employee implements Serializable{String name;int age;double salary;transient Thread t=new Thread();public Employee(String name,int age,double salary){this.name=name;this.age=age;this.salary=salary;}private void writeObject(java.io.ObjectOutputStream oos) throws IOException{oos.writeInt(age);oos.writeUTF(name);System.out.println("Write Object");}private void readObject(java.io.ObjectInputStream ois) throws IOException{age=ois.readInt();name=ois.readUTF();System.out.println("Read Object");}}
========================================================== =============================================================== 4, RandomFIleimport java.io.*;class RandomFileTest{public static void main(String[] args) throws Exception{Student s1=new Student(1,"zhangsan",98.5);Student s2=new Student(2,"lisi",96.5);Student s3=new Student(3,"wangwu",78.5);RandomAccessFile raf=new RandomAccessFile("student.txt","rw");s1.writeStudent(raf);s2.writeStudent(raf);s3.writeStudent(raf);Student s=new Student();raf.seek(0);for(long i=0;i
=========================================================================5、File
import java.io.*;class FileTest{public static void main(String[] args) throws Exception{//File f=new File("1.txt");//f.createNewFile();//f.mkdir();//File f=new File("E:\\JavaLesson\\Lesson7\\1.txt");//f.createNewFile();/*File fDir=new File(File.separator);String strFile="JavaLesson"+File.separator+"Lesson7"+File.separator+"1.txt";File f=new File(fDir,strFile);f.createNewFile();//f.delete();f.deleteOnExit();Thread.sleep(3000);*//*for(int i=0;i<5;i++){File f=File.createTempFile("winsun",".tmp");f.deleteOnExit();}Thread.sleep(3000);*/File fDir=new File(File.separator);String strFile="JavaLesson"+File.separator+"Lesson6";File f=new File(fDir,strFile);String[] names=f.list(new FilenameFilter(){public boolean accept(File dir,String name){return name.indexOf(".java")!=-1;}});for(int i=0;i
=====================================================================================6、
import java.util.*;import java.nio.charset.*;class CharsetTest{public static void main(String[] args) throws Exception{/*Map m=Charset.availableCharsets();Set names=m.keySet();Iterator it=names.iterator();while(it.hasNext()){System.out.println(it.next());}*/Properties pps=System.getProperties();//pps.list(System.out);pps.put("file.encoding","ISO-8859-1");int data;byte[] buf=new byte[100];int i=0;while((data=System.in.read())!='q'){buf[i]=(byte)data;i++;}String str=new String(buf,0,i);//System.out.println(str);String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");System.out.println(strGBK);}}