JAVA IO流相关代码(InputStreamReader类OutputStreamWriter类DataInputStream类DataOutputStream类)

Posted 小乖乖的臭坏坏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA IO流相关代码(InputStreamReader类OutputStreamWriter类DataInputStream类DataOutputStream类)相关的知识,希望对你有一定的参考价值。

与编码相关的InputStreamReader类和OutputStreamWriter类

示例代码

import java.io.*;

class EncodeStream 

	public static void main(String[] args) throws IOException 
	
			//writeText();
			readText();
	

	public static void readText()throws IOException 
	
		InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");

		char[] buf = new char[10];
		int len = isr.read(buf);

		String str = new String(buf,0,len);

		System.out.println(str);

		isr.close();
	
	public static void writeText()throws IOException 
	
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");

		osw.write("你好");

		osw.close();
	


与字符串编码解码相关的代码

示例代码

import java.util.*;
class  EncodeDemo

	public static void main(String[] args)throws Exception 
	
		String s = "哈哈";

		byte[] b1 = s.getBytes("GBK");

		System.out.println(Arrays.toString(b1));
		String s1 = new String(b1,"utf-8");
		System.out.println("s1="+s1);

		//对s1进行iso8859-1编码。
		byte[] b2 = s1.getBytes("utf-8");
		System.out.println(Arrays.toString(b2));

		String s2 = new String(b2,"gbk");

		System.out.println("s2="+s2);

		

	

DataInputStream与DataOutputStream

可以用于操作基本数据类型的数据的流对象。
示例代码

import java.io.*;
class DataStreamDemo 

	public static void main(String[] args) throws IOException
	
		//writeData();
		//readData();

		//writeUTFDemo();

//		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk");
//
//		osw.write("你好");
//		osw.close();

//		readUTFDemo();

	
	public static void readUTFDemo()throws IOException
	
		DataInputStream dis = new DataInputStream(new FileInputStream("utf.txt"));

		String s = dis.readUTF();

		System.out.println(s);
		dis.close();
	



	public static void writeUTFDemo()throws IOException
	
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("utfdate.txt"));

		dos.writeUTF("你好");

		dos.close();
	

	public static void readData()throws IOException
	
		DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));

		int num = dis.readInt();
		boolean b = dis.readBoolean();
		double d = dis.readDouble();

		System.out.println("num="+num);
		System.out.println("b="+b);
		System.out.println("d="+d);

		dis.close();
	
	public static void writeData()throws IOException
	
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));

		dos.writeInt(234);
		dos.writeBoolean(true);
		dos.writeDouble(9887.543);

		dos.close();

		ObjectOutputStream oos = null;
		oos.writeObject(new O());

		
	

以上是关于JAVA IO流相关代码(InputStreamReader类OutputStreamWriter类DataInputStream类DataOutputStream类)的主要内容,如果未能解决你的问题,请参考以下文章

JAVA IO流相关代码(Serializable接口,管道流PipedInputStream类,RandomAccessFile类相关代码)

JAVA IO流相关代码(ByteArrayInputStream类和ByteArrayOutputStream类相关代码)

JAVA IO流相关代码(ByteArrayInputStream类和ByteArrayOutputStream类相关代码)

JAVA IO流相关代码(打印流 和 文件拼接)

JAVA IO流相关代码(打印流 和 文件拼接)

JAVA IO流相关代码(字符流:FileWriter类,FileReader类,BufferedReader类,BufferedWriter类)