Java中的几种I/O策略简述

1.Reading/Writing

FileReader inputStream = null;
FileWriter outputStream = null;
try {
    inputStream=new FileReader(“in.txt");
    outputStream = new FileWriter("output.txt");
    int c;
    while ((c = inputStream.read()) != -1)
        outputStream.write(c);
} finally {
    if (inputStream != null)
        inputStream.close();
    if (outputStream != null)
        outputStream.close();
}

2.BufferedReader/Writer

String sentence=null;
FileReader reader=null;
BufferedReader bReader = null;
StringBuilder sb = null;
try{
    reader = new FileReader(file);
    bReader = new BufferedReader(reader);
    sb = new StringBuilder();
    String s = "";
    while((s = bReader.readLine())!=null){
	  sb.append(s+" ");
    }
    bReader.close();
    sentence = sb.toString();
}catch(IOException e){
    System.out.println("no such file exists");
}finally {  
    try { 
	if(reader!=null){
            reader.close();  
        }
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    try {     if(bReader!=null){		    		    bReader.close();  
	}
    } catch (IOException e) {  
        e.printStackTrace();  
    }   
}  
return sentence;		
public void strategy(String filepath,String word) {
		BufferedWriter writer = null;
		try {
			File file = new File(filepath);  
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(word);
            writer.flush(); 
        }catch (Exception e){
            e.printStackTrace();
        }finally {  
		    try { 
		    	if(writer!=null){
		    		writer.close();  
		    	}
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    }    
		}  
	}

3.StreamReader/Writer

public String strategy(String file) {
		String content=null;
		BufferedInputStream bis = null;
		try{
			bis = new BufferedInputStream(new FileInputStream(file));
	         //自己定义一个缓冲区
	        byte[] buffer=new byte[2048];
	        int flag=0;
	        StringBuffer buf = new StringBuffer();
	        while((flag=bis.read(buffer))!=-1){
	        	buf.append(new String(buffer, 0, flag));
	        }
	        content=buf.toString();
	        bis.close();
		}catch(IOException e){
			System.out.println("no such file exists");
		}finally {  
		    try { 
		    	if(bis!=null){
		    		bis.close();  
		    	}
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    }    
		}  
        return content;
	}
public void strategy(String filepath, String word) {
		OutputStreamWriter osr = null;
		try{
			OutputStream out = new FileOutputStream(filepath);
			osr = new OutputStreamWriter(out);
			osr.write(word);  
	        osr.flush();  
	        osr.close(); 
		}catch (IOException e){
            e.printStackTrace();
        }finally {  
		    try { 
		    	if(osr!=null){
		    		osr.close();  
		    	}
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    }    
		}  
	}

4.Scanner/Printer

public String strategy(String file) {
		String line=null;
		try{
			Scanner inputStream = null;
			inputStream = new Scanner(new FileInputStream(file));
			StringBuffer buf = new StringBuffer();
			while(inputStream.hasNextLine())  
		    {  
				buf.append(inputStream.nextLine());
		    }  
			line=buf.toString();
	        inputStream.close();
		}catch(IOException e){
			System.out.println("no such file exists");
		}
        return line;
	}

public void strategy(String filepath, String word) {
		try{
			File file=new File(filepath);//文件对象   
		    PrintWriter out = new PrintWriter(file);  
		    out.println(word);  
		    out.close();  //关闭写出器  
		}catch (Exception e){
            e.printStackTrace();
        }
	}

5.Nio

public class NioCat {
    public static void main(String[] args) throws IOException {
        ByteBuffer buf = ByteBuffer.allocate(512);
        try (FileChannel ch = FileChannel.open(Paths.get(args[0]),
StandardOpenOption.READ)) {
            int n;
            while ((n = ch.read(buf)) > -1) {
                System.out.print(new String(buf.array(), 0, n));
                buf.clear();
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_39523768/article/details/80774025