第7章Java的输入/输出流 H 例74 Import java. 10 public class File copy i public static void main( String args[ throws IOException FileInputStream fI FileOutputStream f2 f]=new FilelnputStream("FileCopy. java") f2=new FileOutputStream("acopy of java file")
第7章 Java的输入/输出流 例7.4 import java.io.*; public class FileCopy { public static void main(String args[]) throws IOException{ FileInputStream f1; FileOutputStream f2; f1=new FileInputStream("FileCopy.java"); f2=new FileOutputStream("acopy_of_java_file");
第7章Java的输亼/输出流 H Int temp, while((temp=fl readO)=-1) f2. write(temp) fl close; fl close public File copyoi) 在该例中,我们利用字节流将本程序拷贝至另 个文件 acopy of java_fle中,如果指定的文件不存在, 则创建一个新文件,否则原文件的内容会被新写入的 内容覆盖。当程序运行后,将生成一个与原程序相同 的副本
第7章 Java的输入/输出流 int temp; while((temp=f1.read())!=-1) f2.write(temp); f1.close(); f1.close(); } public FileCopy() { } } 在该例中,我们利用字节流将本程序拷贝至另一 个文件acopy_of_java_file中,如果指定的文件不存在, 则创建一个新文件,否则原文件的内容会被新写入的 内容覆盖。当程序运行后,将生成一个与原程序相同 的副本
第7章Java的输入/输出流 H 2. Character流(字符流)文件的读取 该类如图72所示,输入/输出类的父类为 Reader、 Writer,其基本的方法有 eader int reado int read( char buld int read ( char bull, int offset, int length) closed
第7章 Java的输入/输出流 2.Character流(字符流)文件的读取 该类如图7.2所示,输入/输出类的父类为Reader、 Writer,其基本的方法有: ● Reader int read() int read(char buf[]) int read(char buf[],int offset,int length) close()
第7章Java的输入/输出流 H ● Writer int write(int c) int write(char buf[D int write(char bufll, int offset, int length) closel 读者可与字节流进行比较,注意二者方法的区别 下面我们用字符流来改写例74:
第7章 Java的输入/输出流 ● Writer int write(int c) int write(char buf[]) int write(char buf[],int offset,int length) close() 读者可与字节流进行比较,注意二者方法的区别。 下面我们用字符流来改写例7.4:
第7章Java的输入/输出流 H 例7.5 import Java. 1o. public class FileCopy public static void main( String args throws IOException File Reader fl File writer f2 fl=new FileReader("Filecopy. java f2 =new File Writer ("acopy of java file)
第7章 Java的输入/输出流 例7.5 import java.io.*; public class FileCopy { public static void main(String args[]) throws IOException{ FileReader f1; FileWriter f2; f1=new FileReader("FileCopy.java"); f2=new FileWriter("acopy_of_java_file");