Training job --- I / O stream

  • Application file output stream. String is defined as follows: String str = "12345abcdef @ #% & * Software Engineering"; programming the string to the file "data.txt".
  • Application file input stream. Modify the program title in the first, reading a file "data.txt", the read data output in the console.
  • To talk about the experience of this training.
    package lolol;
        import java.io.*;
        import java.io.File;
        public class eee {
            public static void main(String[] args){
                String red="12345abcdef@#%&*软件工程";
                File file=new File("C:\\data.txt");
                try{
                    FileWriter fw=new FileWriter(file);
                    BufferedWriter  bufw=new BufferedWriter(fw);
                  
                    bufw.write(red);
                    bufw.newLine();                          
                    bufw.close();
                    fw.close();
                    
                }catch(Exception e){
                    e.printStackTrace();
                }
            try{
              FileReader fr=new FileReader(file);
              BufferedReader  bufr=new BufferedReader(fr);
                  String str=null;
                  int i=0;
              while((str=bufr.readLine())!=null){
                  i++; 
                  System.out.print(str);              
              }
              bufr.close();
              fr.close();                 
            }catch(Exception e){
                e.printStackTrace();         
            }
            }
        }
            
        

 

 We are free to write to a file, and read out. To this end I learned File can create a text document, for which, FileReader to read and write by FileWriter, and FileOutStream, FileInputStream only provides a method for reading bytes or byte array. So the former is better. BufferedWriter, BufferedReader have internal cache mechanism, in units of input and output. Each end must be closed using the close. Through this I have learned a lot of progress.

Guess you like

Origin www.cnblogs.com/luoxianglong/p/11125822.html