标签:ack while 换行 on() ast dem sage 异常 copy
InputStream和OutputStream是字节流的顶级父类, 所有的字节输入流继承自InputStream, 所有的字节输出流继承自OutputStream
1 package deom03; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 6 public class FileOutputStreamDemo { 7 public static void main(String[] args) throws IOException { 8 FileOutputStream out = new FileOutputStream("c:\\out.txt"); 9 //void write(int b) 向输出流写入一个字节 d 10 out.write(100); 11 out.close(); 12 13 byte[] bytes = {65,66,67,68}; 14 FileOutputStream out1 = new FileOutputStream("c:\\out1.txt"); 15 //void write(byte[] b) 写入字节数组 ABCD 16 out1.write(bytes); 17 out1.close(); 18 19 FileOutputStream out2 = new FileOutputStream("c:\\out2.txt"); 20 //向文件中写入一个字符串 21 out2.write("hello".getBytes()); 22 out2.close(); 23 24 //追加写入 25 FileOutputStream out3 = new FileOutputStream("c:\\out3.txt", true); 26 //向文件中写入一个字符串 27 out3.write("hello".getBytes()); 28 out3.write("world".getBytes()); 29 out3.close(); 30 31 //追加 换行写入 32 FileOutputStream out4 = new FileOutputStream("c:\\out4.txt", true); 33 //向文件中写入一个字符串 34 out4.write("hello".getBytes()); 35 out4.write("\r\n".getBytes()); 36 out4.write("world".getBytes()); 37 out4.close(); 38 39 //异常处理 40 MyException(); 41 } 42 43 //异常处理 44 public static void MyException() { 45 FileOutputStream out5 = null; 46 try { 47 out5 = new FileOutputStream("c:\\out5.txt"); 48 out5.write(100); 49 } catch (IOException e) { 50 System.out.println(e.getMessage()); 51 throw new RuntimeException("文件写入失败"); 52 } finally { 53 try { 54 if (out5 != null) { 55 out5.close(); 56 } 57 } catch (IOException e2) { 58 System.out.println(e2.getMessage()); 59 throw new RuntimeException("关闭资源失败"); 60 } 61 62 } 63 } 64 }
1 package deom03; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 6 //input1.txt 内容 abc 7 public class FileInputStreamDemo { 8 public static void main(String[] args) throws IOException { 9 FileInputStream in = new FileInputStream("c:\\input1.txt"); 10 11 //int read() 读取一个字节转换为整数并返回这一整数 12 int i = 0; 13 while ((i = in.read()) != -1) { 14 System.out.println(i); 15 } 16 in.close(); 17 18 //int read(byte[] b) 读取若干字节并保存到字节数组中 19 FileInputStream in1 = new FileInputStream("c:\\input1.txt"); 20 byte[] b = new byte[3]; 21 int len = in1.read(b); 22 System.out.println(new String(b)); //abc 23 System.out.println(len); //3 24 in1.close(); 25 } 26 }
第一种方式
1 package deom03; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 7 //文件复制-效率低 8 public class Copy { 9 public static void main(String[] args) { 10 FileInputStream in = null; 11 FileOutputStream out = null; 12 try { 13 in = new FileInputStream("c:\\xampp安装包.zip"); 14 out = new FileOutputStream("c:\\11.zip"); 15 16 int len = 0; 17 while ((len = in.read()) != -1) { 18 out.write(len); 19 } 20 } catch (IOException e) { 21 System.out.println(e); 22 System.out.println("文件复制失败"); 23 } finally { 24 try { 25 if (out != null) { 26 out.close(); 27 } 28 } catch (IOException e1) { 29 throw new RuntimeException("释放资源失败"); 30 } finally { 31 try { 32 if (in != null) { 33 in.close(); 34 } 35 } catch (IOException e2) { 36 throw new RuntimeException("释放资源失败"); 37 } 38 } 39 } 40 } 41 }
第二种方式
1 package deom03; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 7 //文件复制-效率高 8 public class CopyFast { 9 public static void main(String[] args) { 10 FileInputStream in = null; 11 FileOutputStream out = null; 12 13 try { 14 in = new FileInputStream("c:\\xampp安装包.zip"); 15 out = new FileOutputStream("c:\\11.zip"); 16 byte[] bytes = new byte[1024]; 17 int len = 0; 18 while ((len = in.read(bytes)) != -1) { 19 out.write(bytes, 0, len); 20 } 21 } catch (IOException e) { 22 System.out.println(e); 23 System.out.println("文件复制失败"); 24 } finally { 25 try { 26 if (out != null) { 27 out.close(); 28 } 29 } catch (IOException e1) { 30 throw new RuntimeException("释放资源失败"); 31 } finally { 32 try { 33 if (in != null) { 34 in.close(); 35 } 36 } catch (IOException e2) { 37 throw new RuntimeException("释放资源失败"); 38 } 39 } 40 } 41 } 42 }
Reader和Writer是字符流的顶级父类, 所有的字符输入流继承自Reader, 所有的字符输出流继承自Writer
1 package deom03; 2 3 import java.io.FileReader; 4 import java.io.IOException; 5 6 public class ReaderDemo { 7 public static void main(String[] args) throws IOException { 8 FileReader fr = new FileReader("c:\\1.txt"); 9 int len = 0; 10 while ((len = fr.read()) != -1) { 11 System.out.print((char)len); 12 } 13 fr.close(); 14 15 FileReader fr1 = new FileReader("c:\\1.txt"); 16 char[] ch = new char[1024]; 17 int len1 = 0; 18 while ((len1 = fr1.read(ch)) != -1) { 19 System.out.println(new String(ch, 0, len1)); 20 } 21 } 22 }
1 package deom03; 2 3 import java.io.FileWriter; 4 import java.io.IOException; 5 6 public class WriterDemo { 7 public static void main(String[] args) throws IOException { 8 FileWriter fw = new FileWriter("c:\\1.txt"); 9 10 //写一个字符 11 fw.write(100); 12 fw.flush(); 13 14 //写一个字符数组 15 char[] c = {‘a‘,‘b‘,‘c‘}; 16 fw.write(c); 17 fw.flush(); 18 19 //写一个字符数组一部分 20 fw.write(c,0,2); 21 fw.flush(); 22 23 //写一个字符串 24 fw.write("你好"); 25 fw.flush(); 26 27 fw.close(); 28 } 29 }
标签:ack while 换行 on() ast dem sage 异常 copy
原文地址:http://www.cnblogs.com/lwn007/p/6919848.html