案例九:
package com.JavaBasicsDemo9; import java.io.FileInputStream; import java.io.IOException; public class Demo1Test9 { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("E:\\demo\\haha.txt"); //调用字节数人流对象数据方法;return // int read(); 1 int num =fis.read(); System.out.println(num); //2 int num1 =fis.read(); System.out.println(num1); //3 int num2 =fis.read(); System.out.println(num2); //4 int num3 =fis.read(); System.out.println(num3); } }
案例十:
package com.JavaBasicsDemo9; import java.io.FileInputStream; import java.io.IOException; public class Demo1Test10 { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("E:\\demo\\haha.txt"); //调用字节数人流对象数据方法;return int by; while((by=fis.read())!=-1) { System.out.print((char)by); //释放资源 //fis.close(); } } }
案例十一:
package com.JavaBasicsDemo9; import java.io.FileOutputStream; import java.io.IOException; public class Demo1Test11 { public static void main(String[] args) throws IOException { // FileOutputStream(String name) FileOutputStream fos =new FileOutputStream("E:\\demo\\herrllo.text" ,true); // void write (int b) 写入数据 fos.write(121); fos.write(11); fos.write(111); fos.write(101); fos.write(111); fos.close(); // 2数组 void write(byte[] by) 将byte数组写入数组中 byte[] by = {34,56,78}; fos.write(by); //写一句话 String str = "我爱你"; byte [] by1=str.getBytes(); fos.write(by1); fos.close(); // byte[] by2 = {34,56,78}; fos.write(by2,2,1); } }
案例十二:
package com.JavaBasicsDemo9; import java.io.FileOutputStream; import java.io.IOException; public class Demo1Test12 { public static void main(String[] args) { FileOutputStream fos=null; try { //创建 fos =new FileOutputStream("E:\\demo\\haha.txt" ,true); for (int i = 0; i <=10; i++) { fos.write("I love you in the end ".getBytes()); fos.write("\r\n".getBytes()); } } catch (Exception e) { e.printStackTrace(); }finally { //释放资源 try { if(fos!=null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
案例十三:
package com.JavaBasicsDemo9; import java.io.*; public class BufferStreamDemo1 { /** *第一种方式 FileInputStream fis= new FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg"); FileOutputStream fos =new FileOutputStream("D:\\haha.jpg" ,true); byte[] by =new byte[1024]; //int read(byte[]) by) int num=0; while((num=fis.read(by))!=-1) { //reading fos.write(by, 0, num); } //关闭流 fis.close(); fos.close(); //基本字节读一个 字节/数组/字节/一个字节数组 * @param args */ public static void main(String[] args)throws IOException { //获取时间 long startTime1 =System.currentTimeMillis(); //调用方法 method1(); long endTime1 =System.currentTimeMillis(); System.out.println("方法1:"+(endTime1-startTime1)); long startTime2 =System.currentTimeMillis(); //调用方法 method1(); long endTime2 =System.currentTimeMillis(); System.out.println("方法2:"+(endTime2-startTime2)); long startTime3 =System.currentTimeMillis(); //调用方法 method1(); long endTime3 =System.currentTimeMillis(); System.out.println("方法3:"+(endTime3-startTime3)); long startTime4 =System.currentTimeMillis(); //调用方法 method1(); long endTime4 =System.currentTimeMillis(); System.out.println("方法4:"+(endTime4-startTime4)); } public static void method1() throws IOException { FileInputStream fis= new FileInputStream("C:\\Users\\MZFAITHDREAM\\Pictures\\Saved Pictures\\A-1 (4)\\黑白上色\\01.jpg"); FileOutputStream fos =new FileOutputStream("D:\\01.jpg" ,true); byte[] by =new byte[1024]; //int read(byte[]) by) int num=0; while((num=fis.read(by))!=-1) { //reading fos.write(by, 0, num); } //关闭流 fis.close(); fos.close(); } public static void method2() throws IOException { FileInputStream fis= new FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg"); FileOutputStream fos =new FileOutputStream("D:\\method2.jpg" ,true); byte[] by =new byte[1024]; //int read(byte[]) by) int num=0; while((num=fis.read(by))!=-1) { //reading fos.write(by, 0, num); } //关闭流 fis.close(); fos.close(); } public static void method3() throws IOException { FileInputStream fis= new FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg"); //创建字节输入缓冲流 BufferedInputStream bis =new BufferedInputStream(fis); FileOutputStream fos =new FileOutputStream("D:\\method3.jpg" ,true); BufferedOutputStream bos =new BufferedOutputStream(fos); //int read(byte[]) by) int num=0; while((num=fis.read())!=-1) { //reading fos.write( num); } //关闭流 fis.close(); fos.close(); } public static void method4() throws IOException { FileInputStream fis= new FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg"); //创建字节输入缓冲流 BufferedInputStream bis =new BufferedInputStream(fis); FileOutputStream fos =new FileOutputStream("D:\\method4.jpg" ,true); BufferedOutputStream bos =new BufferedOutputStream(fos); byte[] by=new byte [1024]; //int read(byte[]) by) int num=0; while((num=fis.read())!=-1) { //reading fos.write( by,0,num); } //关闭流 fis.close(); fos.close(); } }
案例十四:
package com.JavaBasicsDemo9; public class CodeDemo1 { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String str="我爱距离100km江西南字符昌学lookforyou江西区的学院"; //将字符串转成编码 byte [] by =str.getBytes("utf-8"); //将数据给用户看 String str1 =new String(by,"utf-8"); System.out.println(str1); //网页定义utf-8 String str2="我爱距离100km江西南区的学院"; //将字符串转成编码 byte [] by1 =str1.getBytes("gbk"); //将数据给用户看 String str3 =new String(by,"gbk"); System.out.println(str2); System.out.println(str.getBytes()); System.out.println(str); System.out.println(str2); } }
案例十五:
package com.JavaBasicsDemo9; import java.io.*; public class CopeFile { public static void main(String[] args) throws IOException { //创建字节输入 FileInputStream fis= new FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\Javaday1_9.zip"); //创建字节输出 FileOutputStream fos =new FileOutputStream("D:\\Day"); //字符输入 InputStreamReader isr=new InputStreamReader(fis,"utf-8"); //字符输出 OutputStreamWriter osr=new OutputStreamWriter(fos,"utf-8"); //创建一个数组去放入取到的数据 char[] cr=new char[1024]; int num=0; //while while((num=isr.read(cr))!=-1) { //reading osr.write( cr,0,num); } isr.close(); osr.close(); } }
案例十六:
package com.JavaBasicsDemo9; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Copy1 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub FileInputStream fis= new FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg"); FileOutputStream fos =new FileOutputStream("D:\\胡滨.jpg" ,true); byte[] by =new byte[1024]; //int read(byte[]) by) int num=0; while((num=fis.read(by))!=-1) { //reading fos.write(by, 0, num); } //关闭流 fis.close(); fos.close(); } }
十六个小案例带你走进java的IO流