JAVA I/O

时间:2019-06-12
本文章向大家介绍JAVA I/O,主要包括JAVA I/O使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 文件输出流的应用。

定义如下字符串:

String str = “12345abcdef@#%&*软件工程”;

编写程序将该字符串写入文件”data.txt”。

package bbb;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class demo {
    public static void main(String[]args) throws IOException{
        String str = "12345abcdef@#%&*软件工程";
        File file = new File("D://data.txt");
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("白敬亭牛皮");
        bw.newLine();
        bw.write(str);
        bw.newLine();
        bw.write("若无相欠,怎会相见");
        
        bw.close();    
        fw.close();      
    }
}

1. 文件输入流的应用。

修改第1题中的程序,读文件”data.txt”,将读到的数据输出在控制台。

package bbb;

//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileReader;
//import java.io.IOException;
import java.io.*;
public class demo {
    public static void main(String[]args) throws IOException{
        File file = new File("D://data.txt");
        FileReader fw = new FileReader(file);
        BufferedReader bw = new BufferedReader(fw);
       String str;
       while((str = bw.readLine()) != null){
           System.out.println(str);
       }
        bw.close();    
        fw.close();      
    }
}

感觉理解有点困难,是根据ppt,然后才勉强弄出来的。

原文地址:https://www.cnblogs.com/baijingtingfuren/p/11009807.html