java I/O

準備

byte:計算機存儲的基本單位,1byte=8bit,咱們看到的字符不管是何種編碼最終都將以byte的形式保存在計算機上。java

char:表明一個字符,能夠是一個漢字,或者一個字母,java意義上的char,通常狀況下1 char=2 byte;json

java內部編碼:java內部編碼採用UTF16,UTF16並不是爲定長編碼,而是不定長編碼,通常爲二字節,或者四字節。數組

String:就是char的數組集合。app

ps:關於字符編碼若是你們想要深刻了解,能夠去肖國棟的i自留地看他的博客,應該說這是我在網上看到的講的最好的關於這方面的內容。ui

package json712.study_javaio;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
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.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Scanner;

import org.junit.Test;

public class IOUtilsTest {
	// @Test
	public void readAndWriteFileByStream() {
		String file = "d:" + File.separator + "inputstream.txt";
		String des = "d:" + File.separator + "outstream.txt";
		System.out.println(System.getProperty("file.encoding"));
		try {
			InputStream fileInputStrem = new FileInputStream(file);
			OutputStream fileOutputStream = new FileOutputStream(des);
			byte[] b = new byte[fileInputStrem.available()];
			int temp = 0;
			int len = 0;
			while ((temp = fileInputStrem.read()) != -1) {
				fileOutputStream.write(temp);
				b[len] = (byte) temp;
				len++;
			}
			fileInputStrem.close();
			fileOutputStream.close();
			System.out.println(new String(b, "gbk"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void readAndWriteFileByBuffStream() {
		String file = "d:" + File.separator + "inputstream.txt";
		String des = "d:" + File.separator + "outstream.txt";
		try {
			BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(des));
			byte[] b = new byte[bufferedInputStream.available()];
			int len = 0;
			int temp = 0;
			while ((temp = bufferedInputStream.read()) != -1) {
				bufferedOutputStream.write(temp);
				b[len] = (byte) temp;
				len++;
			}
			bufferedInputStream.close();
			bufferedOutputStream.close();
			System.out.println(new String(b, "gbk"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// @Test
	public void readAndWriteFileByStremReader() {
		String file = "d:" + File.separator + "inputstream.txt";
		String des = "d:" + File.separator + "outstream.txt";
		int temp = 0;
		StringBuilder sb = new StringBuilder();
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),
					Charset.forName("GBK"));
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(des),
					Charset.forName("GBK"));
			while ((temp = inputStreamReader.read()) != -1) {
				outputStreamWriter.write(temp);
				sb.append((char) temp);
			}
			System.out.println(sb.toString());
			inputStreamReader.close();
			outputStreamWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// @Test
	public void reaAndWritedFileByReader() {
		String file = "d:" + File.separator + "inputstream.txt";
		String des = "d:" + File.separator + "outstream.txt";
		StringBuilder sb = new StringBuilder();
		int temp = 0;
		try {
			FileReader fileReader = new FileReader(file);
			FileWriter fileWriter = new FileWriter(des);
			while ((temp = fileReader.read()) != -1) {
				fileWriter.write(temp);
				sb.append((char) temp);
			}
			fileReader.close();
			fileWriter.close();
			System.out.println(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void readAndWriteFileByBuffReader() {
		String file = "d:" + File.separator + "inputstream.txt";
		String des = "d:" + File.separator + "outstream.txt";
		String text = null;
		try {
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")));
			BufferedWriter bufferedWriter = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(des), Charset.forName("GBK")));
			while ((text = bufferedReader.readLine()) != null) {
				bufferedWriter.write(text);
				System.out.println(text);
			}
			bufferedReader.close();
			bufferedWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void userScanner() {
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNextLine()) {
			String string = scanner.nextLine();
			System.out.println(string);
			if ("exit".equals(string)) {
				return;

			}

		}
	}

}

以上是對文件的一些基本的輸入輸出編碼

InputStream→BufferedInputStream→InputStreamReader→FileReader→BufferedReader.net

OutputStream→BufferedOutputStream→OutputStreamWriter→FileWriter→BufferedWritercode

java中的in與out是基於內存爲標準定義的, out:內存到磁盤是輸出 in:磁盤到內存內存

相關文章
相關標籤/搜索