• java 文件读写的有用工具


    java 文件读写的有用工具

    package org.rui.io.util;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.TreeSet;
    /**
     * 文件读写的有用工具
     * @author lenovo
     *
     */
    public class TextFile  extends ArrayList<String>{
    	
    	//读取一个文件,并返回字符串
    	public static String read(String fileName)
    	{
    		StringBuilder sb=new StringBuilder();
    		
    		try {
    			BufferedReader in=new BufferedReader(
    					new FileReader(
    							new File(fileName).getAbsoluteFile())// 返回此抽象路径名的绝对路径名形式。
    					);
    			
    			try {
    				String s;
    				while((s=in.readLine())!=null)
    					sb.append(s);
    					sb.append("
    ");
    				
    			} finally
    			{
    				in.close();
    			}
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    		return sb.toString();
    	}
    	// write a single
    	public static void write(String fileName,String text)
    	{
    		try {
    			PrintWriter out=new PrintWriter(
    					new File(fileName).getAbsoluteFile()
    					);
    			try {
    				out.print(text);
    				
    			}finally
    			{
    				out.close();
    			}
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    		
    		
    	}
    	
    	// read a file
    	public TextFile(String fileName,String splitter)
    	{
    		//放入集合,
    		super(Arrays.asList(read(fileName).split(splitter)));
    		if(get(0).equals("")) remove(0);
    	}
    
    	public TextFile(String fileName)
    	{
    		//重载当前构造器
    		this(fileName,"
    ");
    	}
    	
    	public void write(String fileName)
    	{
    		try {
    			PrintWriter out=new PrintWriter(
    					new File(fileName).getAbsoluteFile()
    					);
    			try {
    				for(String item:this){
    					//System.out.println(item);
    					 out.println(item);
    				}
    					
    				
    			} finally
    			{
    				out.close();
    			}
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    		
    	}
    	/////simple test
    	public static void main(String[] args) {
    		//路径依据自已的改
    		String path="D:\Users\liangrui\workspace\thinking\src\org\rui\io\util/";
    		
    		
    		String fileText =read(path+"TextFile.java");//读取当前java文本
    		//写入文件
    		write(path+"newText.txt",fileText);
    		//对象 写
    		TextFile tf=new TextFile(path+"newText.txt");//再次读取写入的新文本
    		tf.write(path+"newText2.txt");//再次写入
    		
    		//读取文本并放入TreeSet集合中
    		TreeSet<String> words=new TreeSet<String>(
    				new TextFile(path+"TextFile.java","\W+")//W词字符
    				);
    		//headSet 返回此 set 的部分视图,其元素严格小于  toElement。
    		System.out.println(words.headSet("a"));
    		
    	}
    }
    /**
     * output:
    [0, ArrayList, Arrays, BufferedReader, D, Exception, File,
    FileReader, PrintWriter, RuntimeException, String, StringBuilder, 
    System, TextFile, TreeSet, Users, W]
     */
    

    package org.rui.io.util;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    /**
     * 读取二进制文件
     * @author lenovo
     *
     */
    public class BinaryFile {
    	
    	public static byte[] read(File bFile) throws IOException
    	{
    		BufferedInputStream bis=new BufferedInputStream(
    				new FileInputStream(bFile)
    				);
    		try {
    			//产生恰当数组尺寸
    			byte[] data=new byte[bis.available()];
    			bis.read(data);
    			return data;
    		} finally
    		{
    			bis.close();
    		}
    	}
    	
    	public static byte[] read(String bF) throws IOException
    	{
    		//重载
    		return read(new File(bF).getAbsoluteFile());// getAbsoluteFile 返回此抽象路径名的绝对路径名形式。
    	}
    
    }
    


  • 相关阅读:
    luogu_1339 [USACO09OCT]热浪Heat Wave
    luogu_1341 无序字母对
    luogu_1330 封锁阳光大学
    luogu_3383 【模板】线性筛素数
    luogu_1095 守望者的逃离
    luogu_1373 小a和uim之大逃离
    查看寄存器
    Assembly oth
    非常详细的端口表汇总
    公式证明
  • 原文地址:https://www.cnblogs.com/llguanli/p/6874182.html
Copyright © 2020-2023  润新知