zoukankan      html  css  js  c++  java
  • Java 对文件的读取操作

      

    package pack;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    
    public class InOutFileStream {
    
    	public static void main(String[] args) throws Exception {
    		
    	//字符流操作 
    		
    		//读取
    		File file = new File ("E:\\open_back16-2-26.sql");
    		
    		FileReader reader = new FileReader(file);
    		
    		int r = 0;
    		char []rchar = new char[1024];
    		
    		String text = "";
    		
    		 while ((r = reader.read(rchar)) != -1) {
    			 text +=new String(rchar,0,r);
    		 }
    		 reader.close();
    		
    		System.out.println(text);
    		
    		File outfile = new File ("E:\\out.txt");
    		if (!outfile.exists()) {
    			outfile.createNewFile();
    		}
    		
    		//写入
    		FileWriter fileWriter = new FileWriter(outfile);
    		
    		fileWriter.write(text);
    		fileWriter.flush();
    		fileWriter.close();
    		
    		
    		//字节流操作
    		
    		//读取文件
    		FileInputStream fis = new FileInputStream(new File ("E:\\back.sql"));
    		
    		File outFile = new File("E:\\out1.txt");
    		if (!outFile.exists()) {
    			outFile.createNewFile();
    		}
    		
    		
    		//将读入的back.sql 写入到 e:out1.txt 文件中
    		FileOutputStream fos = new FileOutputStream(outFile);
    		
    		int rr = 0;
    		
    		byte[]fchar = new byte[1024];
    		
    		while ((rr = fis.read(fchar)) != -1) {
    			
    			fos.write(fchar,0,rr);
    		}
    		fos.flush();
    		fos.close();
    		fis.close();
    	}
    }
    

      

  • 相关阅读:
    USACO3.2.5Magic Squares
    USACO3.1.4Shaping Regions
    USACO3.2.3Spinning Wheels
    USACO3.1.3Humble Numbers
    USACO3.1.6Stamps
    USACO3.1.1AgriNet
    USACO3.3.5A Game
    USACO3.2.4Feed Ratios
    USACO3.2.2Stringsobits
    USACO3.2.6Sweet Butter
  • 原文地址:https://www.cnblogs.com/hjwbla/p/5604535.html
Copyright © 2011-2022 走看看