首页 > 代码库 > I/O输入输出流的练习

I/O输入输出流的练习

1.编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。

public class Test01 {    public static void main(String[] args) {        //输出流        String filename="f:/test/TextRw.txt";        //追加方式        try         {     //追加输入(字节流输入)                FileOutputStream out = new FileOutputStream(filename,true);                String str="20142200101 毕研椿";                //写入数据                out.write(str.getBytes());                //释放资源                out.close();                System.out.println("写入成功");                //输入流                FileInputStream in =new FileInputStream(filename);                //容器byte[]                byte[] b =new byte[1024];                //长度                int i =0;                //内容                StringBuilder chu =new StringBuilder();                //循环读取                while((i=in.read(b))>0)                {                    //组装数据                    chu.append(str);                }                System.out.println("文件写入内容"+str);                                                //字符流输入                //输出流                FileWriter fw = new FileWriter("f:/test/TextRw.txt",true);                fw.write("20142200101毕研椿");                fw.close();                System.out.println("写入成功");                //输入流                FileReader fr = new FileReader("f:/test/TextRw.txt");                char[]c =new char[1024];                int j=0;                StringBuilder shur = new StringBuilder();                while((j=fr.read(c))>0)                {   //终止读取完单个数组

 

的长度                    shur.append(new String(c,0,j));                }                System.out.println("读取:"+shur);                fr.close();        }         catch (Exception e) {            // TODO 自动生成的 catch 块            e.printStackTrace();        }                }}

 技术分享

2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

public class Test02 {    public static void main(String[] args) {            File file = new File("f:/text.txt");                //输出流写入一个文件        try {                FileOutputStream out = new FileOutputStream("f:/text.txt");                //写入内容                String nr = "太难了";               //兼容任何类型数据                byte[] b =nr.getBytes();                out.write(b);                out.close();                System.out.println("输出流写入成功");                //输入流   读                  FileInputStream in  = new FileInputStream("f:/text.txt");                // 容器                byte[] b1 = new byte[1024];                int i =-1;                StringBuilder  str = new StringBuilder();                while(( i =in.read(b1))>0)                {                    //组装数据                    str.append(new String(b1,0,i));                 }                System.out.println("读出的信息是:"+str);                in.close();                //本质:把原有文件以新的文件名和路径进行复制,然后删除源文件                file.renameTo(new File("f:/iodemo.txt"));                System.out.println("修改成功"+file);                               }                 catch (Exception e) {                        e.printStackTrace();        }    }}

技术分享

技术分享

3.编写BinIoDemo.java的Java应用程序,程序完成的功能是:完成1.doc文件的复制,复制以后的文件的名称为自己的学号姓名.doc。

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class Test03 {    public static void main(String[] args) {        //文件复制        //边读边写                        try {                //输入流 读                FileInputStream in = new FileInputStream("f:/个人简历.docx");                //byte[]容器                byte[] b = new byte[1024];                //长度                int i =-1;                //写  输出流                FileOutputStream out = new FileOutputStream("f:/学号姓名.docx");                //边读边写                while((i=in.read(b))>0)                {                    //                    out.write(b,0,i);                }                out.close();                in.close();                System.out.println("复制成功");              }         catch (Exception e) {            // TODO 自动生成的 catch 块            e.printStackTrace();        }    }}

技术分享

技术分享

 

I/O输入输出流的练习