首页 > 学院 > 开发设计 > 正文

io流,入门例子代码

2019-11-11 01:46:48
字体:
来源:转载
供稿:网友
package com.mashensoft.homework;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PRintWriter;import java.io.Reader;import java.io.Writer;import java.util.Scanner;/** * 读取文本文件,读取图片,读取视频 * @author zongxing * */public class HomeWork {/*** 读取文本文件,读取图片,读取视频* @param sourceName 源文件* @param dest 目标文件*/public static void copyFile(String sourceName,String dest) {System.out.println("开始copy文件,源文件是:"+sourceName+",目标文件是:"+dest);//1:先读取一个文件//2:写入一个文件try {FileInputStream fis = new FileInputStream(sourceName);FileOutputStream fos = new FileOutputStream(dest);int a = 0;//当还未到达文件尾时,循环读取while((a=fis.read())!=-1){//每读取一个字节过来,我们就写入到另一个文件里去fos.write(a);//这里只会正常写入数据,不会把-1写进去}fos.flush();//把缓冲区里的数据强制写入到文件中fos.close();//关闭输出流fis.close();//关闭输入流} catch (IOException e) {e.printStackTrace();}}/*** 由于读取速度太慢,所以要学习新的方法,让我们更快一点*/public static void test2() {byte[] myArray = new byte[3];try {InputStream is = new FileInputStream("a.txt");//如果到达文件尾,无法满足while的条件,不会执行while语句的内容while(is.read(myArray)!=-1){for (int i = 0; i < myArray.length; i++) {System.out.print((char)myArray[i]);}}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 又遇到一个问题:当使用数组的时候,如果文件里的字符的长度不是数组的倍数的时候,拿到的数据会重复* */public static void test3() {byte[] myArray = new byte[3];int len;try {InputStream is = new FileInputStream("a.txt");//如果到达文件尾,无法满足while的条件,不会执行while语句的内容while((len=is.read(myArray))!=-1){for (int i = 0; i < len; i++) {System.out.print((char)myArray[i]);}}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 试试使用数组输出的会不会快一点* 自定义了一个要输出的数组*/public static void test4() {System.out.println("读取数据中");byte[] myArray = new byte[1024];int len;try {InputStream is = new FileInputStream("1-1输入输出流.wmv");FileOutputStream os = new FileOutputStream("22.wmv");//如果到达文件尾,无法满足while的条件,不会执行while语句的内容while((len=is.read(myArray))!=-1){//定义一个新的数组,这个数据要防止有多条的数据byte descArray[] = new byte[len];for (int i = 0; i < len; i++) {descArray[i]=myArray[i];}os.write(descArray);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("成功");}/*** 试试使用数组输出的会不会快一点* 使用jdk提供的方法,效率会更高一些*/public static void test5() {System.out.println("读取数据中");byte[] myArray = new byte[1024];int len;try {InputStream is = new FileInputStream("1-1输入输出流.wmv");FileOutputStream os = new FileOutputStream("33.wmv");//如果到达文件尾,无法满足while的条件,不会执行while语句的内容while((len=is.read(myArray))!=-1){//最后一次读取数据的时候,只把从数组的第0个长度开始到数组的指定len(我们从流中最后一次实际读出的数据长度)长度os.write(myArray,0,len);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("成功");}/*** 使用缓冲区,进一步提供效率*/public static void test6() {try {InputStream is = new FileInputStream("1-1输入输出流.wmv");//把原来的流装进一个类里BufferedInputStream bis = new BufferedInputStream(is);FileOutputStream os = new FileOutputStream("34.wmv");byte myArray[] = new byte[1024];int len;while((len = bis.read(myArray))!=-1){os.write(myArray, 0, len);}} catch ( IOException e) {e.printStackTrace();}}/*** 使用输出的缓冲区,进一步提供效率*/public static void test7() {try {InputStream is = new FileInputStream("1-1输入输出流.wmv");//把原来的流装进一个类里BufferedInputStream bis = new BufferedInputStream(is);FileOutputStream os = new FileOutputStream("35.wmv");BufferedOutputStream bos = new BufferedOutputStream(os);byte myArray[] = new byte[1024];int len;while((len = bis.read(myArray))!=-1){bos.write(myArray, 0, len);}bos.flush();bos.close();os.close();bis.close();is.close();} catch ( IOException e) {e.printStackTrace();}}/*** 当我们没有文件的时候,如果使用流*/public static void test8() {String content = "wangzongxing";ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());int a = 0;while((a = is.read())!=-1){System.out.print((char)a);}}/*** 新的问题:当有中文的时候,可能会乱码*/public static void test9() {String content = "你";byte myArray[] = content.getBytes();for (int i = 0; i < myArray.length; i++) {System.out.print((char)myArray[i]);}}/*** 新的问题:当有中文的时候,可能会乱码,当有了流的时候。*/public static void test10() {String content = "你";ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());byte a = 0;while((a = (byte)is.read())!=-1){System.out.print((char)a);}}/*** 新的问题:当有中文的时候,可能会乱码,当有了流的时候。*/public static void test11() {String content = "你";ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());byte a[] = new byte[1];try {is.read(a);System.out.println(new String(a));} catch (IOException e) {e.printStackTrace();}}public static void test12() {try {InputStream is = new FileInputStream("a.txt");byte myArray[] = new byte[12];int a = 0;while((a = is.read(myArray))!=-1){System.out.println(new String(myArray));}} catch (IOException e) {e.printStackTrace();}}/*** 引处字符流,来解决中文问题* Reader抽象类,InputStreamReader实现类*/public static void test13() {try {Reader reader = new InputStreamReader(new FileInputStream("a.txt"));int a = reader.read();System.out.println((char)a);reader.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 引处字符流,来解决中文问题,读取到数组中,一次可以读取多个字符* Reader抽象类,InputStreamReader实现类*/public static void test14() {try {Reader reader = new InputStreamReader(new FileInputStream("a.txt"));char myArray[] = new char[4];int a = 0;;while((a=reader.read(myArray))!=-1){System.out.println(new String(myArray));}reader.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 用字符流写入数据到文件里*/public static void test15() {try {Writer writer = new OutputStreamWriter(new FileOutputStream("c.txt",true));writer.write("星哥哥");writer.flush();writer.close();// StringWriter} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 读取字符流的时候,读取的效率更高一些。*/public static void test16() {try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));while(reader.ready()){System.out.println(reader.readLine());}reader.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 让写入字符流更高效一些*/public static void test17() {try {BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d.txt")));bw.write("hello,星哥!123412341234");bw.flush();bw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 为了能够输出txt数据的时候更方便,我们要引入printWriter.字符流*/public static void test18() {try {PrintWriter pw = new PrintWriter("e.txt");pw.print("I am 星哥!我");pw.print(18);pw.println("岁");pw.flush();pw.close();} catch (FileNotFoundException e) {e.printStackTrace();}}/*** 读取txt文件的时候要更简单一些*/public static void test19() {Scanner sc;try {sc = new Scanner(new FileInputStream("e.txt"));while(sc.hasNextLine()){System.out.println(sc.nextLine());}sc.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 新建一个文件*/public static void test20() {File file = new File("m.txt");try {System.out.println(file.createNewFile());} catch (IOException e) {e.printStackTrace();}}/*** 新建一个文件夹* @param args*/public static void test21() {File file = new File("mmm");System.out.println(file.mkdir());}/*** 删除一个文件/文件夹*/public static void test22() {File file = new File("mmm");System.out.println(file.delete());}/*** 得命名一个文件/文件夹*/public static void test23() {File file = new File("nnn");System.out.println(file.renameTo(new File("abc")));}/*** 查看文件的权限* @param args*//*** 得命名一个文件/文件夹*/public static void test24() {File file = new File("abc");System.out.println(file.canRead());System.out.println(file.canWrite());System.out.println(file.canExecute());}/*** 修改文件/文件夹的权限*/public static void test25() {File file = new File("abc");System.out.println(file.setExecutable(false));System.out.println(file.canExecute());}/*** 搜索文件/文件夹的权限*/public static void test26() {File file = new File("./");String[] files = file.list();for (int i = 0; i < files.length; i++) {File temp = new File(files[i]);//if(files[i].contains(".jpg")){if(temp.isDirectory()){System.out.println("d"+files[i]+",是一个文件夹");}else{System.out.println("-"+files[i]+",是一个文件");}//}}}/*** 用系统提供的方法过滤文件* @param args*/public static void test27() {File file = new File("./");File files[] = file.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {if(pathname.getName().endsWith("doc")){return true;}return false;}});for (int i = 0; i < files.length; i++) {System.out.println(files[i].getName());}}public static void main(String[] args) {test27();}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表