IO流与异常

来源:互联网 发布:adobe家族软件 编辑:程序博客网 时间:2024/05/21 10:13
文件的读入:
package cn.java.hp;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileCopy {public static void main(String[] args) throws IOException {File file = new File("D:\\HelloWorld.java");if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {System.out.println("文件创建失败。");e.printStackTrace();}}File file1 = new File("D:\\HW.java");if (!file1.exists()) {try {file.createNewFile();} catch (IOException e) {System.out.println("文件1创建失败。");e.printStackTrace();}} else {FileInputStream in = new FileInputStream(file);FileOutputStream out = new FileOutputStream(file1);byte[] bb = new byte[6];int cc = 0;while ((cc = in.read(bb)) != -1) {System.out.println(new String(bb, 0, cc));out.write(bb);}in.close();out.close();}}}
文件的读取:
package cn.java.hp;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileCopy {public static void main(String[] args) throws IOException {File file = new File("D:\\HelloWorld.java");if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {System.out.println("文件创建失败。");e.printStackTrace();}}File file1 = new File("D:\\HW.java");if (!file1.exists()) {try {file.createNewFile();} catch (IOException e) {System.out.println("文件1创建失败。");e.printStackTrace();}} else {FileInputStream in = new FileInputStream(file);FileOutputStream out = new FileOutputStream(file1);byte[] bb = new byte[6];int cc = 0;while ((cc = in.read(bb)) != -1) {System.out.println(new String(bb, 0, cc));out.write(bb);}in.close();out.close();}}}

0 0