자바 중 8 가지 IO 조작

6157 단어 자바C++cF#C#
자바 중 8 가지 IO 조작
package com.mengya.TestIO;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;

public class TestIO {

 /**
  * Java  8 IO  
  */
 /*
  *     
  */
 public void CreateFile(){
  File f=new File("e:\\io.txt");
  if(!f.exists()){
   try {
    f.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /*
  * FileOutputStream      
  */
 public void set1(){
  try {
   FileOutputStream f=new FileOutputStream("e:\\io.txt");
   String str="       ";
   byte[] b=str.getBytes();
   try {
    f.write(b);
    f.flush();
    f.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }  
 }
 /*
  * FileInputStream      
  */
 public void get1(){
  try {
   FileInputStream f=new FileInputStream("e:\\io.txt");
   byte[] b=new byte[200];
   try {
    int n=f.read(b);
    String str=new String(b,0,n);
    System.out.println(str);
   } catch (IOException e) {
    e.printStackTrace();
   }
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }  
 }
 /*
  *
  */
 public void set2(){
  try {
   FileOutputStream f=new FileOutputStream("e:\\io.txt",true);//   true        ,  true        
   BufferedOutputStream ff=new BufferedOutputStream(f);
   String str=new String("         ");
   try {
    ff.write(str.getBytes());
    ff.flush();
    ff.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  
 }
 
 public void get2(){
  try {
   FileInputStream f=new FileInputStream("e:\\io.txt");
   BufferedInputStream ff=new BufferedInputStream(f);
   byte[] b=new byte[200];
   try {
    int n = ff.read(b);
    String str=new String(b,0,n);
    System.out.println(str);
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 
 /*
  *    
  */
 public void set3(){
  try {
   FileWriter f=new FileWriter("e:\\io.txt",true);
   f.write("\r
");//\r
f.flush(); f.close(); } catch (IOException e) { e.printStackTrace(); } } public void get3(){ try { FileReader f=new FileReader("e:\\io.txt"); char[] c=new char[200]; try { int n = f.read(c); String str=new String(c,0,n); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } public void set4(){ try { FileWriter f=new FileWriter("e:\\io.txt",true); BufferedWriter ff=new BufferedWriter(f); ff.write(" "); ff.flush(); ff.close(); } catch (IOException e) { e.printStackTrace(); } } public void get4(){ try { FileReader f=new FileReader("e:\\io.txt"); BufferedReader ff=new BufferedReader(f); char[] c=new char[200]; int n; try { n = ff.read(c); String str=new String(c,0,n); System.out.println(str); ff.close(); f.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } /* * */ public void h1(){ try { FileOutputStream f=new FileOutputStream("e:\\io.txt",true); OutputStreamWriter ff=new OutputStreamWriter(f); try { ff.write("\r
"); ff.flush(); ff.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } public void h2(){ try { FileInputStream f=new FileInputStream("e:\\io.txt"); InputStreamReader ff=new InputStreamReader(f); char[] c=new char[200]; int n; try { n = ff.read(c); String str=new String(c,0,n); System.out.println(str); ff.close(); f.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } /* * */ public void hh(){ try { FileReader f=new FileReader("e:\\io.txt"); BufferedReader ff=new BufferedReader(f); while(ff.ready()){ System.out.println(ff.readLine()); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { TestIO test=new TestIO(); test.CreateFile(); //test.set1(); //test.get1(); //test.set2(); //test.get2(); //test.set3(); //test.get3(); //test.set4(); //test.get4(); //test.h1(); //test.h2(); test.hh(); } }

좋은 웹페이지 즐겨찾기