자바 의 IO 정리

98488 단어 자바

: , , GBK Unicode , + 。 , , , , 。 : java IO ,java IO , , , IO , 。 , , java , 。 , 。 。 【 1】
import java.io.*; class hello{ public static void main(String[] args) { File f=new File("D:\\hello.txt"); try{ f.createNewFile(); }catch (Exception e) { e.printStackTrace(); } } } 【 】: , d hello.txt 。 【 2】File import java.io.*; class hello{ public static void main(String[] args) { System.out.println(File.separator); System.out.println(File.pathSeparator); } } 【 】: \ ; : , windows \ ? 。 linux \ 。 , , , , , 。 、 File : import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); try{ f.createNewFile(); }catch (Exception e) { e.printStackTrace(); } } } , , 。 File 。 /** * * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); if(f.exists()){ f.delete(); }else{ System.out.println(" "); } } } /** * * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator+"hello"; File f=new File(fileName); f.mkdir(); } } 【 】: D hello ( ): /** * list * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); String[] str=f.list(); for (int i = 0; i < str.length; i++) { System.out.println(str[i]); } } } 【 】: $RECYCLE.BIN 360 360Downloads 360Rec 360SoftMove Config.Msi da Downloads DriversBackup eclipse java web Lenovo MSOCache Program Program Files python RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19} System Volume Information Tomcat6 var vod_cache_data ( , ) list String ,。 , , listFiles. File ( ): /** * listFiles * listFiles * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); File[] str=f.listFiles(); for (int i = 0; i < str.length; i++) { System.out.println(str[i]); } } } 【 】: D:\$RECYCLE.BIN D:\360 D:\360Downloads D:\360Rec D:\360SoftMove D:\Config.Msi D:\da D:\Downloads D:\DriversBackup D:\eclipse D:\java web D:\Lenovo D:\MSOCache D:\Program D:\Program Files D:\python D:\RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19} D:\System Volume Information D:\Tomcat6 D:\var D:\vod_cache_data D:\ , listFiles 、 /** * isDirectory * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); if(f.isDirectory()){ System.out.println("YES"); }else{ System.out.println("NO"); } } } 【 】:YES /** * * */ import java.io.*; class hello{ public static void main(String[] args) { String fileName="D:"+File.separator; File f=new File(fileName); print(f); } public static void print(File f){ if(f!=null){ if(f.isDirectory()){ File[] fileArray=f.listFiles(); if(fileArray!=null){ for (int i = 0; i < fileArray.length; i++) { // print(fileArray[i]); } } } else{ System.out.println(f); } } } } 【 】: D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\framepages\web4welcome_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\help_005fhome_jsp.
class D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\help_005fhome_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\home_jsp.
class D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\home_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\index_jsp.
class D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\index_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\login_jsp.
class D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\login_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\modify_005fuser_005finfo_jsp.
class D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\modify_005fuser_005finfo_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\register_005fnotify_jsp.
class D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\register_005fnotify_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\sign_005fup_jsp.
class D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\sign_005fup_jsp.java D:\Tomcat6\work\Catalina\localhost
evel\org\apache\jsp\transit_jsp.
class …… 【 RandomAccessFile 】 /** * RandomAccessFile * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); RandomAccessFile demo=new RandomAccessFile(f,"rw"); demo.writeBytes("asdsad"); demo.writeInt(12); demo.writeBoolean(true); demo.writeChar('A'); demo.writeFloat(1.21f); demo.writeDouble(12.123); demo.close(); } } hello。txt , 。 【 】 /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); OutputStream out =new FileOutputStream(f); String str=" "; byte[] b=str.getBytes(); out.write(b); out.close(); } } hello.txt “ ” 。 /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); OutputStream out =new FileOutputStream(f); String str=" "; byte[] b=str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } out.close(); } } :“ ” : /** * * : * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); OutputStream out =new FileOutputStream(f,true); String str="Rollen"; //String str="\r
Rollen";
byte[] b=str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } out.close(); } } 【 】: Rollen 【 】 /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; in.read(b); in.close(); System.out.println(new String(b)); } } 【 】 Rollen Rollen_ , in.read(b); 。 : /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int len=in.read(b); in.close(); System.out.println(" :"+len); System.out.println(new String(b,0,len)); } } 【 】: :18 Rollen Rollen , , , , , , : /** * * , * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[(int)f.length()]; in.read(b); System.out.println(" :"+f.length()); in.close(); System.out.println(new String(b)); } } :18 Rollen Rollen : /** * * , * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[(int)f.length()]; for (int i = 0; i < b.length; i++) { b[i]=(byte)in.read(); } in.close(); System.out.println(new String(b)); } } 。 , , , , , 。 /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int count =0; int temp=0; while((temp=in.read())!=(-1)){ b[count++]=(byte)temp; } in.close(); System.out.println(new String(b)); } } 【 】 Rollen Rollen_ , -1. -1 【 】 /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); Writer out =new FileWriter(f); String str="hello"; out.write(str); out.close(); } } hello。txt , hello , , 。 , out : Writer out =new FileWriter(f,true); , , : hellohello , “\r
” str String str
="\r
hello"; str 。 : /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); char[] ch=new char[100]; Reader read=new FileReader(f); int count=read.read(ch); read.close(); System.out.println(" :"+count); System.out.println(" "+new String(ch,0,count)); } } 【 】: :17 hellohello hello , 。 /** * * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); char[] ch=new char[100]; Reader read=new FileReader(f); int temp=0; int count=0; while((temp=read.read())!=(-1)){ ch[count++]=(char)temp; } read.close(); System.out.println(" "+new String(ch,0,count)); } } : hellohello hello , , , 。 , 。 , , , , 。 ? 。 , 。 , , 。 DOS , d hello.txt d rollen.txt , : copy d:\hello.txt d:\rollen.txt d hello.txt., ,( ) 。 , , 。、 : /** * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { if(args.length!=2){ System.out.println(" , "); System.exit(1); } File file1=new File(args[0]); File file2=new File(args[1]); if(!file1.exists()){ System.out.println(" "); System.exit(1); } InputStream input=new FileInputStream(file1); OutputStream output=new FileOutputStream(file2); if((input!=null)&&(output!=null)){ int temp=0; while((temp=input.read())!=(-1)){ output.write(temp); } } input.close(); output.close(); } } javac hello.java java hello d:\hello.txt d:\rollen.txt d rollen。txt , OutputStreramWriter InputStreamReader IO 。 OutputStreramWriter InputStreamReader , 。 /** * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Writer out=new OutputStreamWriter(new FileOutputStream(file)); out.write("hello"); out.close(); } } : :hello /** * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Reader read=new InputStreamReader(new FileInputStream(file)); char[] b=new char[100]; int len=read.read(b); System.out.println(new String(b,0,len)); read.close(); } } 【 】:hello , , ByteArrayInputStream ByteArrayOutputStream /** * * */ import java.io.*; class hello{ public static void main(String[] args) throws IOException { String str="ROLLENHOLT"; ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream output=new ByteArrayOutputStream(); int temp=0; while((temp=input.read())!=-1){ char ch=(char)temp; output.write(Character.toLowerCase(ch)); } String outStr=output.toString(); input.close(); output.close(); System.out.println(outStr); } } 【 】: rollenholt , 。 。 PipedOutputStream PipedInputStream /** * * */ import java.io.*; /** * * */ class Send implements Runnable{ private PipedOutputStream out=null; public Send() { out=new PipedOutputStream(); } public PipedOutputStream getOut(){ return this.out; } public void run(){ String message="hello , Rollen"; try{ out.write(message.getBytes()); }catch (Exception e) { e.printStackTrace(); }try{ out.close(); }catch (Exception e) { e.printStackTrace(); } } } /** * * */ class Recive implements Runnable{ private PipedInputStream input=null; public Recive(){ this.input=new PipedInputStream(); } public PipedInputStream getInput(){ return this.input; } public void run(){ byte[] b=new byte[1000]; int len=0; try{ len=this.input.read(b); }catch (Exception e) { e.printStackTrace(); }try{ input.close(); }catch (Exception e) { e.printStackTrace(); } System.out.println(" "+(new String(b,0,len))); } } /** * * */ class hello{ public static void main(String[] args) throws IOException { Send send=new Send(); Recive recive=new Recive(); try{ // send.getOut().connect(recive.getInput()); }catch (Exception e) { e.printStackTrace(); } new Thread(send).start(); new Thread(recive).start(); } } 【 】: hello , Rollen /** * PrintStream * */ import java.io.*; class hello { public static void main(String[] args) throws IOException { PrintStream print = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "hello.txt"))); print.println(true); print.println("Rollen"); print.close(); } } 【 】: true Rollen /** * PrintStream * * */ import java.io.*; class hello { public static void main(String[] args) throws IOException { PrintStream print = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "hello.txt"))); String name="Rollen"; int age=20; print.printf(" :%s. :%d.",name,age); print.close(); } } 【 】: :Rollen. :20. OutputStream /** * OutputStream * */ import java.io.*; class hello { public static void main(String[] args) throws IOException { OutputStream out=System.out; try{ out.write("hello".getBytes()); }catch (Exception e) { e.printStackTrace(); } try{ out.close(); }catch (Exception e) { e.printStackTrace(); } } } 【 】: hello import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; /** * System.out.println() * */ public class systemDemo{ public static void main(String[] args){ // System.out.println("hello"); File file = new File("d:" + File.separator + "hello.txt"); try{ System.setOut(new PrintStream(new FileOutputStream(file))); }catch(FileNotFoundException e){ e.printStackTrace(); } System.out.println(" !"); } } 【 】: eclipse hello。 d hello.txt , : ! import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; /** * System.err * */ public class systemErr{ public static void main(String[] args){ File file = new File("d:" + File.separator + "hello.txt"); System.err.println(" "); try{ System.setErr(new PrintStream(new FileOutputStream(file))); }catch(FileNotFoundException e){ e.printStackTrace(); } System.err.println(" !"); } } 【 】: eclipse :“ ”, d hello.txt : ! ? import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** * System.in * */ public class systemIn{ public static void main(String[] args){ File file = new File("d:" + File.separator + "hello.txt"); if(!file.exists()){ return; }else{ try{ System.setIn(new FileInputStream(file)); }catch(FileNotFoundException e){ e.printStackTrace(); } byte[] bytes = new byte[1024]; int len = 0; try{ len = System.in.read(bytes); }catch(IOException e){ e.printStackTrace(); } System.out.println(" :" + new String(bytes, 0, len)); } } } 【 】: d hello.txt :“ !”, , : : ! BufferedReader : BufferedReader , , System.in , : ? BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); : import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * */ public class BufferedReaderDemo{ public static void main(String[] args){ BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); String str = null; System.out.println(" "); try{ str = buf.readLine(); }catch(IOException e){ e.printStackTrace(); } System.out.println(" :" + str); } } : dasdas :dasdas Scanner Scanner , Scanner import java.util.Scanner; /** * Scanner , * */ public class ScannerDemo{ public static void main(String[] args){ Scanner sca = new Scanner(System.in); // int temp = sca.nextInt(); System.out.println(temp); // float flo=sca.nextFloat(); System.out.println(flo); // //... , , 。 } } Scanner Scanner import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * Scanner , * */ public class ScannerDemo{ public static void main(String[] args){ File file = new File("d:" + File.separator + "hello.txt"); Scanner sca = null; try{ sca = new Scanner(file); }catch(FileNotFoundException e){ e.printStackTrace(); } String str = sca.next(); System.out.println(" :" + str); } } 【 】: : ! DataOutputStream、DataInputStream import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamDemo{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.txt"); char[] ch = { 'A', 'B', 'C' }; DataOutputStream out = null; out = new DataOutputStream(new FileOutputStream(file)); for(char temp : ch){ out.writeChar(temp); } out.close(); } } A B C , DataInputStream import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class DataOutputStreamDemo{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.txt"); DataInputStream input = new DataInputStream(new FileInputStream(file)); char[] ch = new char[10]; int count = 0; char temp; while((temp = input.readChar()) != 'C'){ ch[count++] = temp; } System.out.println(ch); } } 【 】: AB SequenceInputStream SequenceInputStream 2 , txt txt。 : import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.SequenceInputStream; /** * * */ public class SequenceInputStreamDemo{ public static void main(String[] args) throws IOException{ File file1 = new File("d:" + File.separator + "hello1.txt"); File file2 = new File("d:" + File.separator + "hello2.txt"); File file3 = new File("d:" + File.separator + "hello.txt"); InputStream input1 = new FileInputStream(file1); InputStream input2 = new FileInputStream(file2); OutputStream output = new FileOutputStream(file3); // SequenceInputStream sis = new SequenceInputStream(input1, input2); int temp = 0; while((temp = sis.read()) != -1){ output.write(temp); } input1.close(); input2.close(); output.close(); sis.close(); } } 【 】 hello.txt hello1.txt hello2.txt 。 ZipOutputStream : import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipOutputStreamDemo1{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.txt"); File zipFile = new File("d:" + File.separator + "hello.zip"); InputStream input = new FileInputStream(file); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream( zipFile)); zipOut.putNextEntry(new ZipEntry(file.getName())); // zipOut.setComment("hello"); int temp = 0; while((temp = input.read()) != -1){ zipOut.write(temp); } input.close(); zipOut.close(); } } 【 】 , hello.txt , 56 , hello.zip , 175 , 。 , 。 , 。 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * * */ public class ZipOutputStreamDemo2{ public static void main(String[] args) throws IOException{ // File file = new File("d:" + File.separator + "temp"); File zipFile = new File("d:" + File.separator + "zipFile.zip"); InputStream input = null; ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream( zipFile)); zipOut.setComment("hello"); if(file.isDirectory()){ File[] files = file.listFiles(); for(int i = 0; i < files.length; ++i){ input = new FileInputStream(files[i]); zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName())); int temp = 0; while((temp = input.read()) != -1){ zipOut.write(temp); } input.close(); } } zipOut.close(); } } 【 】 : : , , , , ZipFile , 。java ZipFile import java.io.File; import java.io.IOException; import java.util.zip.ZipFile; /** * ZipFile * */ public class ZipFileDemo{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.zip"); ZipFile zipFile = new ZipFile(file); System.out.println(" :" + zipFile.getName()); } } 【 】: :d:\hello.zip , , ( ), hello.zip import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /** * ( ) * */ public class ZipFileDemo2{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.zip"); File outFile = new File("d:" + File.separator + "unZipFile.txt"); ZipFile zipFile = new ZipFile(file); ZipEntry entry = zipFile.getEntry("hello.txt"); InputStream input = zipFile.getInputStream(entry); OutputStream output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close(); } } 【 】: : 175 : 56 , 。 ZipInputStream ,ZipEntry , , ZipInputStream import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; /** * * */ public class ZipFileDemo3{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "zipFile.zip"); File outFile = null; ZipFile zipFile = new ZipFile(file); ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file)); ZipEntry entry = null; InputStream input = null; OutputStream output = null; while((entry = zipInput.getNextEntry()) != null){ System.out.println(" " + entry.getName() + " "); outFile = new File("d:" + File.separator + entry.getName()); if(!outFile.getParentFile().exists()){ outFile.getParentFile().mkdir(); } if(!outFile.exists()){ outFile.createNewFile(); } input = zipFile.getInputStream(entry); output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close(); } } } 【 】: : D temp , : PushBackInputStream import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PushbackInputStream; /** * * */ public class PushBackInputStreamDemo{ public static void main(String[] args) throws IOException{ String str = "hello,rollenholt"; PushbackInputStream push = null; ByteArrayInputStream bat = null; bat = new ByteArrayInputStream(str.getBytes()); push = new PushbackInputStream(bat); int temp = 0; while((temp = push.read()) != -1){ if(temp == ','){ push.unread(temp); temp = push.read(); System.out.print("( " + (char) temp + ") "); }else{ System.out.print((char) temp); } } } } 【 】: hello( ,) rollenholt /** * * */ public class CharSetDemo{ public static void main(String[] args){ System.out.println(" :" + System.getProperty("file.encoding")); } } 【 】: :GBK : import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * * */ public class CharSetDemo2{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.txt"); OutputStream out = new FileOutputStream(file); byte[] bytes = " ".getBytes("ISO8859-1"); out.write(bytes); out.close(); } } 【 】: ?? , 。 。 , java.io.Serializable 。 , cloneable 。 , 。 : import java.io.*; /** * * */ public class SerializableDemo implements Serializable{ public SerializableDemo(){ } public SerializableDemo(String name, int age){ this.name=name; this.age=age; } @Override public String toString(){ return " :"+name+" :"+age; } private String name; private int age; } , , ObjectInputStream ObjectOutputStream ObjectOutputStream : import java.io.Serializable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; /** * * */ public class Person implements Serializable{ public Person(){ } public Person(String name, int age){ this.name = name; this.age = age; } @Override public String toString(){ return " :" + name + " :" + age; } private String name; private int age; } /** * ObjectOutputStream * */ public class ObjectOutputStreamDemo{ public static void main(String[] args) throws IOException{ File file = new File("d:" + File.separator + "hello.txt"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( file)); oos.writeObject(new Person("rollen", 20)); oos.close(); } } 【 】: hello.txt , , 。 。 , ObjectInputStream : import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream; /** * ObjectInputStream * */ public class ObjectInputStreamDemo{ public static void main(String[] args) throws Exception{ File file = new File("d:" + File.separator + "hello.txt"); ObjectInputStream input = new ObjectInputStream(new FileInputStream( file)); Object obj = input.readObject(); input.close(); System.out.println(obj); } } 【 】 :rollen :20 ? 。 Externalizable Serializable , , Externalizable 。 Externalizable , , , , 。 : package IO; import java.io.Externalizable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; /** * * */ public class ExternalizableDemo{ public static void main(String[] args) throws Exception{ ser(); // dser(); // } public static void ser() throws Exception{ File file = new File("d:" + File.separator + "hello.txt"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream( file)); out.writeObject(new Person("rollen", 20)); out.close(); } public static void dser() throws Exception{ File file = new File("d:" + File.separator + "hello.txt"); ObjectInputStream input = new ObjectInputStream(new FileInputStream( file)); Object obj = input.readObject(); input.close(); System.out.println(obj); } } class Person implements Externalizable{ public Person(){ } public Person(String name, int age){ this.name = name; this.age = age; } @Override public String toString(){ return " :" + name + " :" + age; } // , , @Override public void writeExternal(ObjectOutput out) throws IOException{ out.writeObject(this.name); out.writeInt(age); } // @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException{ this.name = (String) in.readObject(); this.age = in.readInt(); } private String name; private int age; } 【 】: :rollen :20 , , Serializable , Externalizable , , Serializable , , transient : : package IO; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * * */ public class serDemo{ public static void main(String[] args) throws Exception{ ser(); // dser(); // } public static void ser() throws Exception{ File file = new File("d:" + File.separator + "hello.txt"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream( file)); out.writeObject(new Person1("rollen", 20)); out.close(); } public static void dser() throws Exception{ File file = new File("d:" + File.separator + "hello.txt"); ObjectInputStream input = new ObjectInputStream(new FileInputStream( file)); Object obj = input.readObject(); input.close(); System.out.println(obj); } } class Person1 implements Serializable{ public Person1(){ } public Person1(String name, int age){ this.name = name; this.age = age; } @Override public String toString(){ return " :" + name + " :" + age; } // private transient String name; private int age; } 【 】: :null :20 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * * */ public class SerDemo1{ public static void main(String[] args) throws Exception{ Student[] stu = { new Student("hello", 20), new Student("world", 30), new Student("rollen", 40) }; ser(stu); Object[] obj = dser(); for(int i = 0; i < obj.length; ++i){ Student s = (Student) obj[i]; System.out.println(s); } } // public static void ser(Object[] obj) throws Exception{ File file = new File("d:" + File.separator + "hello.txt"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream( file)); out.writeObject(obj); out.close(); } // public static Object[] dser() throws Exception{ File file = new File("d:" + File.separator + "hello.txt"); ObjectInputStream input = new ObjectInputStream(new FileInputStream( file)); Object[] obj = (Object[]) input.readObject(); input.close(); return obj; } } class Student implements Serializable{ public Student(){ } public Student(String name, int age){ this.name = name; this.age = age; } @Override public String toString(){ return " : " + name + " :" + age; } private String name; private int age; } 【 】: : hello :20 : world :30 : rollen :40 : java.nio, , , , 。

좋은 웹페이지 즐겨찾기