实训作业 I/O流

  1. 文件输出流的应用。
    定义如下字符串:
    String str = “12345abcdef@#%&*软件工程”;
    编写程序将该字符串写入文件”data.txt”。
  2. 文件输入流的应用。
    修改第1题中的程序,读文件”data.txt”,将读到的数据输出在控制台。
  3. 谈一谈本次实训的体会。

package haonan;
import java.io.*;
public class OutPut {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
           File f=new File("data.txt");
           FileWriter f1=new FileWriter(f);
           BufferedWriter b=new BufferedWriter(f1);
           b.write("123456abcdef@#$%软件工程455678678786786");
           b.close();
           f1.close();
    }

}
package haonan;
import java.io.*;
public class InPut {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
       File f=new File("data2.txt");
       FileReader fr=new FileReader(f);
       BufferedReader br=new BufferedReader(fr);
       String s;
       while((s=br.readLine())!=null){
           System.out.println(s);
       }
       br.close();
       fr.close();
    }

}

实训心得:本次实训掌握了I/O流的输入与输出,理解了输入输出流在Java中的作用,以后就能将文件中的内容输入或输出了。实训过程中对于输入与输出的的概念会混淆,不知道对于计算机本身输入与输出是以什么为对象,最后还是解决了这个问题。总的来说,I/O流的应用是很多的,所以需要掌握好。

posted @ 2019-07-08 16:03  何浩南  阅读(181)  评论(0编辑  收藏  举报