总结一下今天回顾的JavaIO的一些基本内容

总结一下今天回顾的JavaIO的一些基本内容_第1张图片
1.看java编程思想Io这一部分的时候遇到一个题目,需要用到字符串的反转,所以记录下来

package com.jww.china.app.test.all.io;

/** 
* @ClassName: StringReverseUtil 
* @Description: 自己实现java字符串反转类 
* @author 维维 
* @date 2015年8月29日 下午7:35:11 
*  
*/
public class StringReverseUtil {

	public static void main(String[] args) {
		String s=" I love java";
		
		System.out.println(reverseString(s));
	}

	protected static String reverseString(String s) {
		char[] c=s.toCharArray();
		int begin=0;
		int end=c.length-1;
//		System.out.println(swapChar(c, begin, end));
		swapChar(c, begin, end);
		for(int i=0;i


2.基本文件输出

package com.jww.china.app.test.all.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

/** 
* @ClassName: BasicFileOutput 
* @Description: 从一个文件里面读取,然后写到另一个文件中去 ,基本的文件输出
* @author 维维 
* @date 2015年8月29日 下午8:51:53 
*  
*/
public class BasicFileOutput {
	public static void main(String[] args) throws IOException {
		writeFile();
	}
	
	private static void writeFile() throws IOException{
		BufferedReader in=new BufferedReader(new StringReader(readFile()));
		PrintWriter printWriter=new PrintWriter(new File("d:/b.txt"));
		String s;
		int lineCount=0;
		while(( s=in.readLine())!=null){
			printWriter.println(++lineCount+":"+s);
		}
		printWriter.close();
			
	}
    
	private static String readFile() throws IOException{
		BufferedReader b=new BufferedReader(new FileReader("d:/a.txt"));
		StringBuilder sb=new StringBuilder();
		String str;
		while((str=b.readLine())!=null){
			sb.append(str+"\n");
		}
		b.close();
		return sb.toString();
	}
}

3.缓冲输入文件

package com.jww.china.app.test.all.io;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;


/** 
* @ClassName: BufferedIputFile 
* @Description: 缓冲输入文件 ,基本的文件输入
* @author 维维 
* @date 2015年8月29日 下午5:57:47 
*  
*/
public class BasicFileInput {

	public static void main(String[] args) throws IOException {
		
		reverserString();
//		reverseStr();
//		System.out.println(bufferedInputFile("d:/a.txt"));
		bufferedReader("d:/a.txt");

	}
    /** 
    * @Title: reverseStr 
    * @Description: 无空格jdk自带方法反转String 
    * @param     
    * @return void    返回类型 
    * 
    */
    private static void reverseStr(){
    	Scanner s=new Scanner(System.in);
    	String str=s.nextLine();
    	
//    	System.out.println(new StringBuilder(str).reverse().toString());
    	System.out.println(new StringReverseUtil().reverseString(str).toString());
    }
	/** 
	* @Title: reverserString 
	* @Description: 有空格String,反转分词,jdk自带方法 
	* @param     
	* @return void    返回类型 
	* 
	*/
	private static void reverserString() {
		
		Scanner s=new Scanner(System.in);
		
		String string=s.nextLine();
		
		String[] str=string.split(" ");
	
		
		List list=new ArrayList();
		
		list=Arrays.asList(str);
		
		Collections.reverse(list);
		
		for(String l : list){
			System.out.print(l+" ");
		}
	}
	
	/** 
	* @Title: bufferedInputFile 
	* @Description: 利用bufferedreader 来输入一个文件
	* @param @param fileName
	* @param @return
	* @param @throws FileNotFoundException    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	private static String bufferedInputFile(String fileName) throws FileNotFoundException{
		
		BufferedReader read =new BufferedReader(new FileReader(fileName));
		String s;
		StringBuilder sb=new StringBuilder();
		try {
			while((s=read.readLine())!=null){
				sb.append(s+"\n");
			}
			read.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	/** 
	* @Title: bufferedReader 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param fileName
	* @param @throws IOException    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	private static void bufferedReader(String fileName) throws IOException{
		 
		BufferedReader read =new BufferedReader(new FileReader(fileName));
         
		 LinkedList l=new LinkedList();
		 
		 String s;
		 String s1;
		 String s2 = null;
		 while((s=read.readLine())!=null){
			 s1=s;
			 l.add(s1);
		 }
		 read.close();
		 for (int i = 0; i < l.size(); i++) {
			System.out.println(l.get(i).toString());
			System.out.println(new StringBuilder(l.get(i)).reverse().toString()); 
		}
	}
	
	

}

4.序列化与反序列化一个java pojo


package com.jww.china.app.test.all.serializable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 测试序列化代码
 * @author Administrator
 *
 */
public class TestSerializable {
     
	 public static void main(String[] args) throws FileNotFoundException {
		 
		 Person p1=new Person();
		 Person p2=new Person();
		 p1.setAge(12);
		 p1.setName("ww");
		 p2.setAge(13);
		 p2.setName("zy");
		 writeObject(p1, p2);
		 System.out.println(readObject("d:/a.txt"));
		 
	}

	/**
	 * @param p1
	 * @param p2
	 * @throws FileNotFoundException
	 */
	private static void writeObject(Person p1, Person p2)throws FileNotFoundException {
		
		//序列化相对于程序来说是输出到txt中所以是out
		 FileOutputStream out =new FileOutputStream(new File("d:/a.txt"));
		 
		 try {
		//创建写入指定的out中的objectoutputstream
		ObjectOutputStream objectOutputStream=new ObjectOutputStream(out);
		
		objectOutputStream.writeObject(p1);
		objectOutputStream.writeObject(p2);
		
		objectOutputStream.flush();
		out.flush();
		
		objectOutputStream.close();
		out.close();
		
		System.out.println("序列化成功!!!");
		
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static Object readObject(String path){
		try {
			FileInputStream inputStream=new FileInputStream(path);
			ObjectInputStream inputStream2=new ObjectInputStream(inputStream);
//		    String	temp=inputStream2.readObject().toString();
		    Object temp=  inputStream2.readObject();
		    inputStream.close();
		    inputStream2.close();
			if (temp!=null) {
				System.out.println("反序列化成功!!");
				return temp;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	    
}

















你可能感兴趣的:(java基础)