`

IO 基本

    博客分类:
  • IO
阅读更多
package iotest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class IOTest {

	public IOTest() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		// writeObject1();
		//deepClone();
		//getASStream();
		writeFile2();

	}

	public static void readFile1() throws IOException {
		/*
		 * 中文编码问题
		 */
		FileReader fr = new FileReader("iotest/mytest.txt");

		BufferedReader br = new BufferedReader(fr);

		String text = "";
		while ((text = br.readLine()) != null) {
			System.out.println(text);
		}
	}

	public static void readFile2() throws IOException {
		/*
		 * 中文编码问题 用FileInputStream最大的好处就是可以改变编码
		 */
		FileInputStream fos = new FileInputStream("iotest/mytest.txt");

		/*
		 * 指定编码
		 */
		InputStreamReader isr = new InputStreamReader(fos, "UTF-8");
		// OutputStreamWriter
		BufferedReader br = new BufferedReader(isr);

		String text = "";
		while ((text = br.readLine()) != null) {
			System.out.println(text);
		}
	}

	public static void readFile3() throws IOException {
		/*
		 * 中文编码问题
		 */
		FileInputStream fos = new FileInputStream("iotest/mytest.txt");

		byte[] bytes = new byte[1024];

		/*
		 * 明显是不对的,byte你读1024个,不一定正好是字符串
		 */
		while (fos.read(bytes) > 0) {
			System.out.println(new String(bytes, "UTF-8"));
		}
	}

	public static void readFile4() throws IOException {

		/*
		 * 中文编码问题
		 */
		FileInputStream fos = new FileInputStream("iotest/mytest.txt"); // 文本本是UTF-8的

		InputStreamReader isr = new InputStreamReader(fos, "UTF-8"); // 所以在这里用UTF-8表示

		BufferedReader br = new BufferedReader(isr);

		char[] chars = new char[1];
		/*
		 * 一个一个读字符
		 */
		while (br.read(chars) > 0) {
			System.out.println(new String(chars));
			if ("我".toCharArray()[0] == chars[0]) {
				System.out.println("我拉拉~~~~~~~");// 汉字问题是,system.out.println的问题,InputStreamReader编码改称GBK
													// ,不行
			}
		}
	}

	public static void writeFile1() throws IOException {
		FileWriter fw = new FileWriter("iotest/mytest.txt", true);

		fw.write("Hello ,Teast你好");
		fw.flush();
		fw.close();
	}

	public static void writeFile2() throws IOException {
		/*
		 * 代码本身就是UTF-8编码
		 */
		FileOutputStream fos = new FileOutputStream("iotest/mytest.txt", true);

		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

		BufferedWriter bw = new BufferedWriter(osw);

		bw.write("Hello ,Teast你好\r\n");
		bw.flush();
		bw.close();

	}

	public static void writeObject1() throws IOException {

		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("1", "ok");
		hm.put("2", "wrong");

		FileOutputStream fos = new FileOutputStream("iotest/object.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);

		oos.writeObject(hm);  
		oos.flush();
		oos.close();

	}

	public static void readObject1() throws Exception {

		FileInputStream fis = new FileInputStream("iotest/object.txt");

		ObjectInputStream ois = new ObjectInputStream(fis);

		HashMap<String, String> hm = (HashMap<String, String>) ois.readObject();

		Iterator<Map.Entry<String, String>> iter = hm.entrySet().iterator();

		while (iter.hasNext()) {
			Map.Entry entry = iter.next();
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}

	}

	public static void deepClone() throws Exception {
		
		/*
		 * 先用ByteArrayOutputStream
		 * 因为序列化是现有输出,才有输入的
		 */
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("1", "ok");
		hm.put("2", "wrong");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);

		oos.writeObject(hm);

		oos.flush();
		oos.close();

		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

		ObjectInputStream ois = new ObjectInputStream(bais);

		HashMap hm2 = (HashMap) ois.readObject(); 

		hm.put("1","test");
		
		
		Iterator<Map.Entry<String, String>> iter2 = hm2.entrySet().iterator();
		
		while (iter2.hasNext()) {
			Map.Entry entry = iter2.next();
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
		
		System.out.println("========================");
		
		Iterator<Map.Entry<String, String>> iter = hm.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = iter.next();
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
	}
	
	public static void getASStream () throws IOException{
		//当前类的相对路径
		//InputStream is = IOTest.class.getResourceAsStream("/iotest/mytest.txt");
		//InputStream is = IOTest.class.getResourceAsStream("mytest.txt");
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("iotest/mytest.txt");
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(is ,"UTF-8"));
		
		String text;
		while ( (text=bf.readLine())!=null ){
			System.out.println(text);
		}
	}
}

  

====================================================================

jchardet

java.io.InputStream 抽象类 :  read()抽象方法,其他两个read()方法也依赖
=======================

FileInputStream fis = new FileInputStream(inFile);
BufferedReader in = new BufferedReader(new InputStreamReader(fis,"UTF-8"));//编码

BufferedReader.readline()!!

======
 FileReader fr = new FileReader("iotest/mytest.txt");

  BufferedReader br = new BufferedReader(fr);

-------------
FileReader 用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。要自己指定这些值,可以先在 FileInputStream 上构造一个 InputStreamReader。

FileReader 用于读取字符流。要读取原始字节流,请考虑使用 FileInputStream。

编码很好很好....

-----------------
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff = new BufferedInputStream(input);

作为另一种输入流,BufferedInputStream 为添加了功能,即缓冲输入和支持 mark 和 reset 方法的能力。创建 BufferedInputStream 时即创建了一个内部缓冲区数组。读取或跳过流中的各字节时,必要时可根据所包含的输入流再次填充该内部缓冲区,一次填充多个字节。mark 操作记录输入流中的某个点,reset 操作导致在从所包含的输入流中获取新的字节前,再次读取自最后一次 mark 操作以来所读取的所有字节。

FileInputStream也可以了!!!最大的好处是可以编码!!

包装类
==这两个是FilterInputStream,增强功能,构造函数参数是InputStream()
java.io.BufferedInputStream
java.io.DataInputStream (implements java.io.DataInput)

==============
 int read()
          从此输入流中读取下一个数据字节。    返回的就是下一个字节,返回的就是那个读到的字节
 int read(byte[] b, int off, int len)
          将最多 len 个数据字节从此输入流读入字节数组。   返回的是一共读得字节个数,读到的字节在byte数组里面。
有两个版本
1个是没有参数的,返回就是读入的东西 readObject(),readLong().....
1个是有参数的,参数数组就是读入的东西,返回的数组的长度

 

两个版本的循环条件:

(text=bf.readLine())!=null

br.read(chars) > 0

=====================
void write(byte[])
只有一个版本,参数就是要输出的内容

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics